Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

If/Else logic not working...  RSS feed

 
John Waclawski
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code I'm running is as follows:



And even if orderno.toString is showing "15781" when I hover over it using NetBeans, it still hits the Else statement.
Here's what else I've tried



and



and



and




amongst a few other editions. Most compile, the one not using "==" did not of course.
Anyways, any suggestions? I'm at a loss here. As you can see I tried & tried to no affect.

HELP!!!
 
Nacho Espinosa
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check with this

String orderno = tokens[0];
if (orderno.equals("15781"))
{
//Code is here
}
Else
{
//Code is here
}
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this link: People still don't get String comparison.
Regards, Jan
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would greatly help you to go over the above mentioned link. However, taking your code as an example, observe the following:

== operator compares the references of two String objects, and as "15781" is a newly created object, control is transferred to the else block.


In short, use equals() to compare the values of two strings, since it is overridden in String class, and "==" to compare reference.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing 'Else' to 'else' will also help. Java is case sensitive.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!