#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    String seems not to be equal with another


    Hi, I'm new to these forums, and I need some help.
    I got 2 strings to compare and I printed them, they're exactly the same, but doesn't seem to work.
    Here's the code:

    Code:
    import java.util.Scanner;
    
    public class Apples {
    	static int max = 30;
    	static String stuff[] = new String[max];
    	static commands Cmd = new commands();
    	static String cmds[] = {"check", "add", "stop",};
    	static int wE = 0;
    	
    	public static void main(String args[]){
    		boolean running = true;
    		Scanner s = new Scanner(System.in);
    		while(running){
    			boolean pass = false;
    			String answ = "";
    			while(pass==false){
    				String ss = s.next()+ "                                 ";
    				for (String c : cmds) {
    					System.out.println(c+", "+ss.toLowerCase().substring(0, c.length()));
    					if ((ss.toLowerCase().substring(0, c.length())) == (c.toLowerCase())) { //this bit needs fixing.
    						pass=true;
    						answ = c;
    					}
    				}
    				if (pass!=true)
    					System.out.println("Commands not recognized.");
    			}
    			if (answ == cmds[0]){
    				System.out.println(Cmd.check(stuff));
    			}else if (answ.substring(0, 3) == cmds[1]){
    				if (wE <= max){
    					System.out.println(answ.substring(3));
    					stuff[wE++] = answ.substring(3);
    				}else{
    					System.out.println("Inventory full, sorry.");}
    			}else if (answ == cmds[2]){
    				running = false;
    				System.out.println("Stopping...");
    			}
    		}
    	}
    	
    }

    Output:

    check
    check, check
    add, che
    stop, chec
    Commands not recognized.
    add
    check, add
    add, add
    stop, add
    Commands not recognized.
    stop
    check, stop
    add, sto
    stop, stop
    Commands not recognized.




    Please help, this is just a simple small script and I'm a beginner.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    351
    You need to use the equals() method to compare objects like Strings. Not the == operator.

    Comments on this post

    • tvc3mye agrees
    • burakaltr agrees
  4. #3
  5. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,557
    Rep Power
    1625

    = vs equals()


    Java objects are references to values. So, using a = comparison between 2 class instances only returns true if both references are referencing the same location in the Java heap space (that is, both are referencing the same object instance).


    If all you want is to compare their values, then the .equals() method should be used instead, as suggested by NormR. If the object instances belong to the same class, then you should overwrite the equals() method which you inherited from the Object with your own implementation in the class implementation.
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo