it's output is wrong!help me please..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluethugz09
    New Member
    • Jan 2010
    • 1

    it's output is wrong!help me please..

    Code:
    import java.util.*;
     public class lawAy
     {
    	 public static void main(String args[])
    	 {
    		 String input=null;
    		 Scanner in=new Scanner(System.in);
    		 System.out.println("Cno law-ay?");
    		 input=in.next();
    		 if(input=="ryka")
    		 {
    		 	System.out.printf("Correct");
    		 }
    		 else
    		 {
    			 System.out.printf("Wrong");
    		 }
    	}
    }
    if you type ryka the output should b correct..
    Last edited by Markus; Jan 9 '10, 08:48 PM. Reason: Added [code] tags.
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    Code:
    if(input=="ryka")
    Try

    Code:
    if(input.equals("ryka"))
    equals() is true if two strings are made up of the same sequence of characters, like the 10th and 14th and 24th words of this sentence. Of course they are different words - none of them could be left out - but they are equal() in terms of the characters that they are made up out of. == on the other hand is only true when the objects are identical: the very same object.

    Usually you will use equals() rather than ==.

    (When you post code, select the code and use the little # button at the top of the panel to wrap it in code tags. That way it will be better formatted when it appears here.)

    Comment

    Working...