Originally Posted by
Shenaniganizer
I kind of don't understand what you mean. ...
The problem with gui programs is that I can't look over your shoulder and see exactly what you are doing.
Try the following. Hit 'Cancel'
Try again and click "OK" without entering anything.
Then try again and go through the steps that resulted in your puzzlement (Enter 2 for the first one and see where it goes from there.)
//
// BookReturns2.java from Zaphod_b
//
// Save your original code for comparison.
//
import java.util.Scanner;
import javax.swing.JOptionPane;
public class BookReturns2 {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int scifi = 34;
int nonfiction = 28;
int scifibook = 0;
int nonfictionbook = 0;
String scifibookString = JOptionPane.showInputDialog(null,
"What type of book do you have?\n" +
"Enter 1 for Sci-Fi,\n" +
"Enter 2 for Non-Fiction,\n" +
"Enter 3 for Graphic Novel,\n" +
"Enter 4 for Novel: ");
// For debugging show what was returned
System.out.println("scifibookString = <" + scifibookString + ">");
// For program testing
if (scifibookString == null) {
// Do whatever you need to do if the user hit 'Cancel'
// For now, just print something on System.out
System.out.println("You cancelled the first book transaction");
}
else if (scifibookString.length() == 0) {
// Do whatever you need to do if the user hit 'OK'
// without entering anything.
// For now, just print something on System.out
System.out.println("You didn't enter anything.");
}
else {
// For a "real" program probably put everything in
// a try{}catch(){} block to keep invalid entries
// from crashing the program.
// For now, just let the chips fall where they may.
scifibook = Integer.parseInt(scifibookString);
}
if (scifibook == 1){
scifi += 1;
JOptionPane.showMessageDialog(null,
"Thank you for returning this Sci-Fi book. Have a nice day!",
"Thank you!",
JOptionPane.INFORMATION_MESSAGE);
System.out.println("We now have " + scifi + " Sci-Fi books.");
System.out.println("Exit point number 1");
System.exit(0);
}
String nonfictionbookString = JOptionPane.showInputDialog(null,
"What type of book do you have?\n" +
"Enter 1 for Sci-Fi,\n" +
"Enter 2 for Non-Fiction,\n" +
"Enter 3 for Graphic Novel,\n" +
"Enter 4 for Novel: ");
// Go through all of the above stuff checking for null or empty
// string returned from the message dialog box
// For debugging show what was returned
System.out.println("nonfictionbookString = <" + nonfictionbookString + ">");
if (nonfictionbookString == null) {
// Do whatever you need to do if the user hit 'Cancel'
// For now, just print something on System.out
System.out.println("You cancelled the second book transaction");
}
else if (nonfictionbookString.length() == 0) {
// Do whatever you need to do if the user hit 'OK'
// without entering anything.
// For now, just print something on System.out
System.out.println("You didn't enter anything in the second dialog box.");
}
else {
// For a "real" program probably put everything in
// a try{}catch(){} block to keep invalid entries
// from crashing the program.
// For now, just let the chips fall where they may.
nonfictionbook = Integer.parseInt(nonfictionbookString);
}
if (nonfictionbook == 2){
nonfiction += 1;
JOptionPane.showMessageDialog(null,
"Thank you for returning this Non-Fiction book. Have a nice day!",
"Thank you!",
JOptionPane.INFORMATION_MESSAGE);
System.out.println("We now have " + nonfiction + " Non-Fiction books.");
System.out.println("Exit point number 2");
System.exit(0);
}
}
}
Maybe you can see what I was talking about
: If you click "OK" without entering anything it returns with a String whose
length is zero. the Integer.parseInt() method in your original code chokes on that, and you get the error message that you reported.
If you click "Cancel" it returns with a null value instead of a reference to something useful. The Integer.parseInt() method in your original code chokes on that, and you get a NullPointerException error.
Go back to your original code (without my if(){}else(){} stuff protecting against a couple of kinds of exceptions). Follow the same sequence of inputs and see what happens with the error messages.
Cheers!
Z