File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes issue with if else statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "issue with if else statement" Watch "issue with if else statement" New topic
Author

issue with if else statement

Chris Fetterley
Ranch Hand

Joined: May 13, 2003
Posts: 51
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Chris Fetterley
Ranch Hand

Joined: May 13, 2003
Posts: 51
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: May 13, 2003
Posts: 51
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

Joined: Sep 03, 2003
Posts: 125
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.


The Inner that is named is not the true Inner.
Chris Fetterley
Ranch Hand

Joined: May 13, 2003
Posts: 51
Just so you know I got it working, you were right and thanks for the help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: issue with if else statement