Other possible issues with if statements - book return program
I'm working a program that's meant to act as book return program; it'll ask you to enter a number, corresponding to the type of book you have, and depending on that number, will add 1 to a static number (for example, 34 books for Sci-Fi) and display the outcome in the console.
When I run it, and try to press 2 for Non-Fiction, nothing will happen. When I press it again, then the "Thank you for returning this Non-Fiction... etc" message will display, but my desired message in the console won't come up (which should say "We now have 29 Non-Fiction Books.")
You know what I mean? This is the error I get:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at BookReturns.main(BookReturns.java:23)
And here's my code:
Code :
import java.util.Scanner;
import javax.swing.JOptionPane;
public class BookReturns {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int scifi = 34;
int nonfiction = 28;
String scifibookString = JOptionPane.showInputDialog(null, "What type of book do you have?\nEnter 1 for Sci-Fi,\nEnter 2 for Non-Fiction,\nEnter 3 for Graphic Novel,\nEnter 4 for Novel: ");
int 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.exit(0);
}
String nonfictionbookString = JOptionPane.showInputDialog(null, "What type of book do you have?\nEnter 1 for Sci-Fi,\nEnter 2 for Non-Fiction,\nEnter 3 for Graphic Novel,\nEnter 4 for Novel: ");
int 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.exit(0);
}
}
}
Re: Other possible issues with if statements - book return program
That's the kind of error message you get if you click "OK" without entering anything in the dialog box.
Check the return value of the String before you try parseInt.
Note that if you click "Cancel" it returns a null value, so you should check for null before anything else.
Then check the length of the String to make sure it's not zero.
Note, also that if you enter something other than an integer, you will get a NumberFormatException. Eventually you will want to catch that to keep the program from crashing. But for now, maybe just check for null and zero-length Strings.
Cheers!
Z
Re: Other possible issues with if statements - book return program
I kind of don't understand what you mean. :( I apologize, I\'m a complete newbie at this.
When I enter 1 for Sci-Fi, it works as intended. When I enter 2 for Non-Fiction, that\'s when nothing happens the first time. But if I enter 2 again, then it\'ll work.
The return value of the String would be what the user inputs, right? Or am I understanding that wrong?
And what do you mean by checking the String to make sure it isn\'t zero?
Re: Other possible issues with if statements - book return program
Quote:
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.)
Code java:
//
// 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