By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

error: Exception in thread "main" java.util.InputMismatchException

P: 30
I dont get it..why is the error: Exception in thread "main" java.util.InputMismatchException
this is my code

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @(#)textFileRead.java
  3.  *
  4.  *
  5.  * @author 
  6.  * @version 1.00 2008/10/17
  7.  */
  8.  
  9. import java.io.*;
  10. import java.util.*;    
  11. public class Adamos {
  12.  
  13. static Scanner console = new Scanner(System.in);
  14.  
  15.     public static void main(String[]args) throws FileNotFoundException
  16.         {
  17.  
  18.         Scanner inFile = new Scanner("C:\\Documents and Settings\\elson.KINGJIM\\Desktop\\grade.txt");
  19.  
  20.         int num,count=0;
  21.         double tot=0.0;
  22.         num = inFile.nextInt();
  23.  
  24.         double sum = 0.0;
  25.         double sort[] = new double[num];
  26.         double grade[] = new double[num+1];
  27.  
  28.  
  29.         for(int i=0;i<num;i++)
  30.         {
  31.             int temp = 0;
  32.  
  33.             String frstName =inFile.next();
  34.             String lstName = inFile.next();
  35.             double g1 = inFile.nextDouble();
  36.             grade[temp++] = g1;
  37.             double g2 = inFile.nextDouble();
  38.             grade[temp++] = g2;
  39.             double g3 = inFile.nextDouble();
  40.             grade[temp++] = g3;
  41.             double g4 = inFile.nextDouble();
  42.             grade[temp++] = g4;
  43.             double g5 = inFile.nextDouble();
  44.             grade[temp++] = g5;
  45.  
  46.             tot=g1+g2+g3+g4+g5;
  47.             sum=tot/temp;
  48.  
  49.  
  50.         if(sum>=75)    
  51.         {
  52.         System.out.printf("\n%.2f %s %s Passed",sum,frstName,lstName);
  53.         sort[count++] = sum;
  54.  
  55.             }
  56.         else
  57.         {
  58.         System.out.printf("\n%.2f %s %s Failed",sum,frstName,lstName);
  59.         sort[count++] =sum;
  60.  
  61.         }
  62.         }
  63.         System.out.println("\n\nSorted Average");
  64.         System.out.print("-----------------------------\n");
  65.  
  66.         Arrays.sort(sort);
  67.         for(int i=0;i<sort.length;i++)
  68.         {
  69.         System.out.println(" "+sort[i]); 
  70.  
  71.         }
  72.  
  73.         System.out.println();    
  74.         inFile.close();
  75.     }
  76.  
  77.  
this is whats inside of my grade.txt

5
a b 75.0 76.0 77.0 78.0 79.0
c d 74.0 75.0 77.0 78.0 79.0
e f 70.0 72.0 73.0 74.0 75.0
g h 90.0 89.0 73.0 67.0 89.0
i j 78.0 77.0 75.0 66.0 87.0


this is the error :
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at Adamos.main(Adamos.java:22)

Process completed.

why is that the error? I don't get it?
Oct 19 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
why is that the error? I don't get it?
There must be something else in that input stream that made your scanner barf.
Read what was there instead of that int:

Expand|Select|Wrap|Line Numbers
  1. Scanner inFile= ...;
  2. try {
  3.    // your normal code here
  4. }
  5. catch (Exception e) {
  6.    System.out.println("this made me barf: "+inFile.readLine());
  7. }
  8.  
... and see what happens.

kind regards,

Jos
Oct 19 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.