So i am new to java, and i am trying to use try and catch. For example if i ask how many grapes the user has, and they type bunches of letters, it will show a error dialog, instead of just getting a system error. I was able to do it with a scanner, but not with JOptionPane. I really want a dialog box to appear, that's why i am trying to use JOptionPane.showInputDialog.
Scanner which works...=
import java.util.Scanner;
class test {
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many grapes do u have?");
int grapes = 1;
try
{
grapes = input.nextInt();
}
catch (Exception e)
{
System.out.println("Good job Sherlock you broke the program");
return;
}
int mg;
if (grapes >= 100)
mg = 1;
else
mg = 2;
switch (mg){
case 1: System.out.println("You got a lot of grapes");
break;
case 2: System.out.println("You brarely got any grapes");
break;
}
}
}
JOptionPane which doesn't work...
import javax.swing.JOptionPane;
public class bday
{
public static void main(String[] args)
{
String age = "0";
try
{
age = JOptionPane.showInputDialog("What was your age yesterday?");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Thanks a lot, you broke it. CYA later.");
return;
}
int iage = Integer.parseInt(age);
String bday = "0";
try
{
bday = JOptionPane.showInputDialog("Was yesterday your B-Day? (True or False)");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "WHY U MESS UP PROGRAM???.... BYE BYE!!");
return;
}
boolean bage = Boolean.parseBoolean(bday);
if (bage == true){
iage += 1;
JOptionPane.showMessageDialog(null, "You are now " + iage);
}
else if (bage == false){
JOptionPane.showMessageDialog(null, "Happy unbirthday!");
}
if (iage ==10){
JOptionPane.showMessageDialog(null, "Congrats, double digits!");
}
if (iage >19){
JOptionPane.showMessageDialog(null, "U aint a Teenager");
}
else if (iage < 13)
JOptionPane.showMessageDialog(null, "U aint a Teenager");
}
}
You're try/catching the wrong thing. You should place the parse statements in the try block since that is what throws the exception.
e.g., not
String age = "0";
try
{
age = JOptionPane.showInputDialog("What was your age yesterday?");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Thanks a lot, you broke it. CYA later.");
return;
}
int iage = Integer.parseInt(age);
but rather:
String age = JOptionPane.showInputDialog("What was your age yesterday?");
try {
iage = Integer.parseInt(age);
} catch (NumberFormatException nfe) {
// show error
}
Also, you should avoid catching Exception
but rather should only catch specific exceptions, here NumberFormatException
.
Edit
In comment you ask:
One more question, should i do the same with the boolean?
The boolean type is a bit trickier (in my mind) when it comes to parsing. To see how the Boolean.parseBoolean(...)
works, please have a look at the Boolean API, specifically the parseBoolean method. You'll see that it does not throw a NumberFormatException if the text entered does not make sense. The API will tell you what in fact is returned. A try/catch block won't work here. Consider using String's equalsIgnoreCase(...)
if you need to trap for errors.