0
import java.util.Scanner;                                         
public class Unit1Err1{                                             
 public static void  main( String[] args ) {                                     
 Scanner inPut = new Scanner(System.in);                                     
     String sName = inPut.nextLine();                                              
     double gpa = inPut.nextDouble();                                               
     int noOfCourses = inPut.nextInt();                                      
  System.out.printf("%11s %18s %12s","Name","GPA","No Of Courses");
   System.out.println();
  System.out.printf("%11s %18s %12s",sName,gpa,noOfCourses);
   System.out.println();     
     sName = inPut.nextLine();                                                     
     gpa = inPut.nextDouble();                                                     
     noOfCourses = inPut.nextInt();                                        
   System.out.printf("%11s %18s %12s",sName,gpa,noOfCourses); 
   System.out.println();
    sName = inPut.nextLine();                                                 
    gpa = inPut.nextDouble();                                                      
    noOfCourses = inPut.nextInt();                                         
   System.out.printf("%11s %18s %12s",sName,gpa,noOfCourses);  
    System.out.println();
   }                                                                         
 }  

The error which shows up is:

CompileRunTest: throwable = java.util.InputMismatchException
java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextDouble(Scanner.java:2456)
    at Unit1Err1.main(Unit1Err1.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at tester.TesterThread.invokeMain(TesterThread.java:32)
    at tester.TesterThread.run(TesterThread.java:38)

I have read that this error is most likely due to a value trying to be input into a variable type which it is not. But my input is:

Henny Penny

3.5

15

Zen Ben

2.8

10

Jerry Berry

4.0

25

And unless its a simple mistake I believe I have everything correct so far. Any help at all would be greatly appreciated,

Note:this was marked as duplicate, but I have done what the other one has shown and it still has yet to work

DinoMeme
  • 49
  • 1
  • 7

1 Answers1

0

This is probably your calling for nextInt() method does not consume the newline character in your input. Your second nextLine() call takes the newline character when you entered "15" and nextDouble gets "Zen Ben" as a input. Just call nextLine() once more to get rid of the unwanted newline character.

  • This is and example of where I inserted the input.nextLine : int noOfCourses = inPut.nextInt(); input.nextLine(); System.out.printf("%14s %12s %18s","Name","GPA","No Of Courses"); And this is the error I get : 1. [File: /Unit1Err1.java Line: 8, Column: 1] cannot find symbol symbol: variable input location: class Unit1Err1 – DinoMeme May 18 '16 at 22:13
  • your scanner object is named as "inPut" but you call nextLine method with "input". Because there is not such a thing, the compiler cannot find it. – Mustafa Çavdar May 18 '16 at 23:21