Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Parsing error

807601Mar 22 2008 — edited Mar 22 2008
Hey guys,

I've searched through the forum, and though I've found other similar parsing problems, they haven't help me detect my problem.
Yes, this is a school project but I'm really just looking to track down the bug. It's a simple piece of code, I've cut out EVERYTHING except the bare minimum.

When run, and menu item 1 is chosen, it asks to enter a name, such as: Jason Clark

The next line asks for a purchase amount. When the purchase amount is entered, immediately an error is generated. All that is being done is storing the purchase amount in a variable of type double.

Now, you'll need the CreditCard class so linked to the .zip containing it and the API. http://8seconddrift.net/typeAPI.zip

What I've noticed is that the error occurs on the second displaying of the menu.
import java.util.Scanner;
import java.io.File;
import type.lib.CreditCard;
import java.io.PrintStream;

public class delete5 {
  public static void main(String[] args) throws java.io.IOException{
    
    PrintStream output = System.out;
    PrintStream fileOutput = new PrintStream(new File("a2output.txt"));
    Scanner input = new Scanner(System.in);
    Scanner fileInput = new Scanner(new File("a2.txt"));
    
    final int NUMBER_OF_RECORDS = fileInput.nextInt();
    
    CreditCard cardDatabase[] = new CreditCard[NUMBER_OF_RECORDS];
    
    int i = 0;
    
      //START PED
      String cardName = new String("");
      double creditLimit;   
      int cardNumber;
      // END PED
    
    for(i = 0; i < NUMBER_OF_RECORDS; i++) {
      //START PED
      cardNumber = fileInput.nextInt();
      cardName = fileInput.nextLine().trim();
      creditLimit = fileInput.nextDouble();
      // END PED
      CreditCard temp = new CreditCard(cardNumber, cardName);
      cardDatabase[i] = temp;
      cardDatabase.setLimit(creditLimit);
System.out.println(cardDatabase[i] + " has a limit of: " + cardDatabase[i].getLimit() + "." + " Account Name: " + cardDatabase[i].getName()); //TEST
}
fileInput.close();



int m = 0;


String userSelect = "";

String tempFullName = "";
int nameChecker = 0;
double purchaseAmount = 0.00;
int state;

String menu[] = {"MAIN MENU", "---------\n", "1 - Menu item 1", "2 - Menu item 2", "Please enter a selection:"};

for( ; !userSelect.toLowerCase().equals("quit"); ) {

for(m = 0; m < 5; m++) {
output.println(menu[m]);
}
userSelect = input.nextLine();
state = Integer.parseInt(userSelect); // LINE 59

if(Integer.parseInt(userSelect) == 1) {
output.println("Enter the card holder's full name: ");
tempFullName = input.nextLine();

for(nameChecker = 0; nameChecker < (cardDatabase.length - 1) && !cardDatabase[nameChecker].getName().equals(tempFullName); nameChecker++) {
}

if(cardDatabase[nameChecker].getName().equals(tempFullName)) {
output.println("Found in array slot " + nameChecker + ", and is name " + (nameChecker + 1)); //REMOVE

output.println("Enter the amount of the purchase: ");
purchaseAmount = input.nextDouble();

} else {
output.println("Sorry, the name " + tempFullName + " was not found in the database.");
}

}


}
}
}
The error generated is:
NumberFormatException: For input string: ""
  at java.lang.NumberFormatException.forInputString(Unknown Source)
  at java.lang.Integer.parseInt(Unknown Source)
  at java.lang.Integer.parseInt(Unknown Source)
  at delete5.main(delete5.java:59)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)

I've marked line 59

This is the text file a2.txt that the code reads from:
10
999990 Gerry Boudens
1000.00
999991 Jimmy Chow
1500.00
999992 Jason Clark
2000.00
999993 Paula Godin
500.00
999994 Edward Kozak
5000.00
999995 Roger Leblanc
6000.00
999996 Karen Liu
2500.00
999997 Ian Maika
3500.00
999998 Jane Orr
3000.00
999999 Jeff Sheedy
7000.00

Now, can anyone shed some light as to why that parsing error comes up? It runs the first time fine. I've spent over 7 hours trying to figure it out.

Thanks in advance!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 19 2008
Added on Mar 22 2008
17 comments
1,393 views