Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple if statement taking a string input doesn't work  RSS feed

 
Sebastian Schneider
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner sc = new Scanner(System.in);

String input = sc.nextLine();

if (input == "next") {
System.out.print("good Job!");
}
else {
System.out.println("Why ?");
}

//If I type in next it prints out "Why ?" instead of "Good Job!" - Why though ?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is on this line:



Rather than tell you the answer I'm going to direct you here:

http://www.coderanch.com/t/409507/java/java/Difference-equals
 
Sebastian Schneider
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:The problem is on this line:



Rather than tell you the answer I'm going to direct you here:

http://www.coderanch.com/t/409507/java/java/Difference-equals



Ahhh man, thank's a lot ! Got it now.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries Sebastian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!