aspose file tools*
The moose likes Beginning Java and the fly likes Logic error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Logic error" Watch "Logic error" New topic
Author

Logic error

raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
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);

}


Thanks,<br />Raghu.<br /> <br />SCJP 5, SCWCD 1.4, NCFM
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
Strings are not compared using the "==" operator, they are compared using the "equals" method.


Ping & DNS - my free Android networking tools app
raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
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

Joined: Jan 10, 2002
Posts: 61648
    
  67

Look carefully at your logic and answer this question: under what circumstances will the else clause be triggered?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

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...???


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kevin lee
Greenhorn

Joined: Sep 20, 2007
Posts: 5
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

Joined: Aug 27, 2004
Posts: 96
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

Joined: Jul 05, 2007
Posts: 72
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

Joined: Oct 13, 2005
Posts: 39828
    
  28
Don't worry about it; everybody does that sort of thing.
 
Consider Paul's rocket mass heater.
 
subject: Logic error