This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logic error

 
raghu dubey
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Could you please point out whats wrong in here. This doGet accepts parameters from a form. And then converts them into a database table name using the if else logic as given below. But the thing is it does not seem to match any options even though I am selecting a matching parameter:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String acntCode = request.getParameter("acntCode");
String configArea = request.getParameter("configAreaSelect");
String tableName;
System.out.println(">"+configArea+"<");
if(configArea == "Incident/SR")tableName = "PM_tblUSDSupportGroupXRef";
if(configArea == "RCA")tableName = "RCA_tblSupportGroupXRef";
if(configArea == "SRT")tableName = "SRT_tblTurnoverSupportGroupXRef";
if(configArea == "Change")tableName = "CM_tblGCARSupportGroupXRef";
else tableName = "Nomatch";
System.out.println(configArea);

ConfigModel config = new ConfigModel();

request.setAttribute("configTableData",config.getTableData(acntCode, tableName) );

RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp");
dispatcher.forward(request, response);

}
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are not compared using the "==" operator, they are compared using the "equals" method.
 
raghu dubey
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf!

I changed the code to

if(configArea.equals("Incident/SR"))tableName = "PM_tblUSDSupportGroupXRef";
if(configArea.equals("RCA"))tableName = "RCA_tblSupportGroupXRef";
if(configArea.equals("SRT"))tableName = "SRT_tblTurnoverSupportGroupXRef";
if(configArea.equals("Change"))tableName = "CM_tblGCARSupportGroupXRef";
else tableName = "Nomatch";

it still does not work and always matches to "Nomatch"

Please help.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at your logic and answer this question: under what circumstances will the else clause be triggered?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to be a little more blunt, you do not have a single "if" statement. You have four "if" statements that are all independent of each other.

Let's say your input is "RCA".

Your first "if" statement runs, is false, so nothing happens.

Your second "if" statement runs, is true, so you set the table name.

Your third "if" statement runs, is false, so nothing happens.

Your fourth "if" statement runs, is false, so you...???
 
Kevin lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:
to be a little more blunt, you do not have a single "if" statement. You have four "if" statements that are all independent of each other.

Let's say your input is "RCA".

Your first "if" statement runs, is false, so nothing happens.

Your second "if" statement runs, is true, so you set the table name.

Your third "if" statement runs, is false, so nothing happens.

Your fourth "if" statement runs, is false, so you...???


I agree
 
Himanshu Bisht
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:
to be a little more blunt, you do not have a single "if" statement. You have four "if" statements that are all independent of each other.

Let's say your input is "RCA".

Your first "if" statement runs, is false, so nothing happens.

Your second "if" statement runs, is true, so you set the table name.

Your third "if" statement runs, is false, so nothing happens.

Your fourth "if" statement runs, is false, so you...???


Amazing expanation. Perfect example of 'to explain but not to spoon feed'
[ October 07, 2008: Message edited by: Himanshu Bisht ]
 
raghu dubey
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bear/Fred,

Thanks for the help. I am still not able to realize that I can made such a mistake. I changed it to if else construct and it works as expected.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry about it; everybody does that sort of thing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic