Friday, 15 February 2013

java - NumberFormatException on Integer input after previous Long input -


itemprop = "text">

I get a NumberFormatException when I get a call for integer input after

class < After making a call for the first long input, here is my code snippet

  System.out.print ("Student ID:"); StudentID = sc.nextLong (); System.out.print ("Student Number:"); StudName = integer Paracetrain (s. Nxtine ());  

Followed as error output

  There are exceptions in the thread "main" java.lang.NumberFormatException: for input string: "" student name : Java in .lang.NumberFormatException.forInputString at java.lang.Integer.parseInt (Integer.javaitter92) (NumberFormatException.java:65) java.lang.Integer.parseInt (Integer.java:615) student.StudentClient.main On (StudentClient.java:68) Java Results:? 1  

I can understand how I can solve this error


I use sc.nextInt () Instead, at the beginning of my code:

  System.out.println ("\ nMENU 1-QUIT / 2-ADD Student"); Option = option = sc.nextInt (); If (option == 1) {System.exit (0); } If (option == 2) {studentIndex ++; Student list [studentIndex] = new student (); System.out.print ("Student Title (Mr. / Mrs.):"); StudentTitle = sc.nextLine (); System.out.print ("Student First Name:"); First name = sc.nextLine ();  

Output 1x input is omitted from the above code. Who makes it:

  Student Title: Student First Name:  

Your problem is that the next length () reads the next long number of calls, but not the new line after that. Therefore, when you call the next line, you get the blank line back from the end of the number, as long as you read up to the new line. For example, if I do this:

  System.out.println ("'+ sc.nextLong () +"' '); System.out.println ("'" + Sc.nextLine () + "''); Enter the numbers like System.out.println ("'" + sc.nextLine () + "' ');  

... and 42 and 63; I will be printed:


No comments:

Post a Comment