Results 1 to 6 of 6
  1. #1
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default String cannot be equated to another?

    Java Code:
    import java.util.*;
    
    public class stringError {
    
    	public static void main(String[] args) {
    		String input;
    		Scanner sc = new Scanner(System.in);
    		System.out.print("Input: ");
    		input = sc.nextLine();
    		input = input.toLowerCase();
    		if(input == "hi"){
    			System.out.println("Never will reach this.");
    		}
    		sc.close();
    	}
    
    }
    I don't really understand how this doesn't work, but even if you put in "hi" for the input, it never will do the if.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: String cannot be equated to another?

    Strings are objects, not primitives. With objects, == compares whether the references are the same...in other words, when comparing String objects always use the equals method. See
    http://docs.oracle.com/javase/tutori...jectclass.html

  3. #3
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: String cannot be equated to another?

    Oh, ok, I remember learning something like that, just simply if(input.equals("hi")){

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: String cannot be equated to another?

    You might even want to try equalsIgnoreCase.

  5. #5
    vikastiwari86 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: String cannot be equated to another?

    Quote Originally Posted by Darkzombies View Post
    Java Code:
    import java.util.*;
    
    public class stringError {
    
    	public static void main(String[] args) {
    		String input;
    		Scanner sc = new Scanner(System.in);
    		System.out.print("Input: ");
    		input = sc.nextLine();
    		input = input.toLowerCase();
    		if(input == "hi"){
    			System.out.println("Never will reach this.");
    		}
    		sc.close();
    	}
    
    
    
    
    
    }
    I don't really understand how this doesn't work, but even if you put in "hi" for the input, it never will do the if.






    change the if condition
    if(input.equals("hi")){
    }

  6. #6
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: String cannot be equated to another?

    You're a day late for my lost memory :3

Similar Threads

  1. Replies: 3
    Last Post: 02-10-2012, 06:26 PM
  2. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  3. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  4. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  5. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •