• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

issue with if else statement

 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating an if else statement that does error checking on textfields....everything was working fine until I tried to add an alpha/numeric check on one of the fields and it didn't like what was created....if anyone has an idea as to why it is doing this I would appericate the advice....below is the logic.
if(wOrder.equals("")){
fieldMessage FMG = new fieldMessage();
workOrder.requestFocus();
}
else if(loc.equals("")){
fieldMessage FMG = new fieldMessage();
location.requestFocus();
}
else if(emp.equals("")){
fieldMessage FMG = new fieldMessage();
employee.requestFocus();
}
else if(prt.equals("")){
fieldMessage FMG = new fieldMessage();
part.requestFocus();
}
else if(qnty.equals("")){
fieldMessage FMG = new fieldMessage();
quantity.requestFocus();
}
else if (qnty != ("")){
try
{
for (int i=0; i<qnty.length(); i++)
Integer.parseInt(qnty.substring(i, i+1));
}
catch(NumberFormatException nfe)
{
numericMessage NMG = new numericMessage();
quantity.requestFocus();
quantity.setText("");
}
}
else{
// output format, submit, etc
//it works fine.
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a little piece of your code:

First, the "if" itself. The operator != asks if two objects are the same object in memory; it does not ask if they're equal, in the sense that 2 +2 equals 4. If you want to ask if a String variable contains "", then always use the equals function. You can write the condition backwards to guard against null variables, like this:

Next, in the "for" loop, you're looking at each character in the string individually, which is probably not what you want to do. Instead, look at the whole thing together:

Hopefully, this will get you back on the right track.
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help but the problem really isn't in the logic of the try block...it's actually in the way it is handling the else if
and I actually want to test each individually.
so shouldn't else if(! "".equals(qnty)) {
work?
Thanks again
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should it achieve what you wanted the original to achieve, but correctly? Yes.
If you want to test each character in a string to see if it's a digit, then you could use Character.isDigit() -- much cheaper.
[ September 29, 2003: Message edited by: Ernest Friedman-Hill ]
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok....here is the question that I have.....the try and catch statement does catch the error that I wanted it to catch....and it posted the error message window saying that all characters must be numeric (yes I want to check them individually) when I go back to the orginial screen it doesn't seem to want to exit out of this statement....is it because of the way it is being used?
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have your tried inserting break; as the last line of your catch block? - you need to get out of the for loop.
I would urge you to follow Ernest's advice and use Character.isDigit() to do this checking; try/catch is an extremely wasteful way to do this sort of checking.
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so you know I got it working, you were right and thanks for the help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic