Click to See Complete Forum and Search --> : if and else
The One
02-24-2006, 10:33 AM
i got the following code:
if(BasicSalary1 == "2"){
JOptionPane.showMessageDialog(null,
"Incorrect input.",
"Please try again",
JOptionPane.PLAIN_MESSAGE);
}
else if(BasicSalary1 != "2"){
JOptionPane.showMessageDialog(null,
": correct input",
"",
JOptionPane.ERROR_MESSAGE);
}
but whenever i run the program it always opens the second error message no matter what number i type in my main program. ("Correct input")
why?
How do i fix it?
BigDog
02-24-2006, 11:12 AM
Try outputing the actual value of BasicSalary1 either to that erro rpane or to whatever logging you have set up. I'm guessing that you are having trouble with some sort of format conversion or null value. Try:
if(BasicSalary1 == "2"){
JOptionPane.showMessageDialog(null,
"Incorrect input.",
"Please try again",
JOptionPane.PLAIN_MESSAGE);
}
else if(BasicSalary1 != "2"){
JOptionPane.showMessageDialog(null,
": The value of BasicSalary1 is " + BasicSalary1 ,
"",
JOptionPane.ERROR_MESSAGE);
}
Also, if in the future, if you could wrap your code examples in [ CODE]...[ /CODE] tags (or use the # button on the top of the edit window). It makes it a lot easier to read.
The One
02-24-2006, 11:39 AM
nope
see i have got a basicSalary textfield. so i wud like if a input 2 that error msg comes out. but it is always coming up with the second error. HELP?
Khalid Ali
02-24-2006, 12:02 PM
first of all ur string comparison seems wrong to me. My guess is u want string.equals("2") method. As I have mentioned before few times. Try to readjava API. You can get this from java.sun.com website(surprised?)
The One
02-25-2006, 09:06 AM
so how do i do it so if a letter is typed in the textfield the error window opens. do i put like string in the brackets or wat? HOW?
The One
02-25-2006, 10:54 AM
any ideas
The One
02-26-2006, 03:11 PM
The following is a bit from ma code:
class CalcButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
String BasicSalary1 = BasicSalarytextfield.getText();
String LunchAllow1 = LunchAllowtextfield.getText();
String HousingAllow1 = HousingAllowtextfield.getText();
String TravelAllow1 = TravelAllowtextfield.getText();
String Bonus1 = Bonustextfield.getText();
String Overtime1 = Overtimetextfield.getText();
double BasicSalary2 = Double.parseDouble (BasicSalary1);
double LunchAllow2 = Double.parseDouble (LunchAllow1);
double HousingAllow2 = Double.parseDouble (HousingAllow1);
double TravelAllow2 = Double.parseDouble (TravelAllow1);
double Bonus2 = Double.parseDouble (Bonus1);
double Overtime2 = Double.parseDouble (Overtime1);
if(BasicSalary1.equals("1")){
JOptionPane.showMessageDialog(null,
"Incorrect input",
"Sorry",
JOptionPane.PLAIN_MESSAGE);
}
else if(BasicSalary1.equals("4")){
JOptionPane.showMessageDialog(null,
": The value of basicSalary1 is " + BasicSalary1,
"",
JOptionPane.ERROR_MESSAGE);
}
u c that if and else bit. i need that 2 work if i type in a letter it will produce the error msg. i think u have to do a try and catch. if so could someone just show where try and catch go so it prints the error when a letter is typed in the textfield???
The One
02-27-2006, 08:00 AM
cause iv tried it with the if and else and it dont work so how do i add the try and catch? can someone show me? (above code) Cos what i want to do is when u type a letter in a field it produces an error as only numbers will be allowed.
The One
02-28-2006, 07:53 AM
can anyone help please????
The One
02-28-2006, 11:35 AM
its ok all done!