0

Possible Duplicate:
Scanner issue when using nextLine after nextInt

I'm very new to programming and i got an error that does not make sense me(i would have searched for the answer here, but i can hardly identify the problem). I am trying to read a file that has hurricane data. the first few lines of the files are as follows:

1980 Aug    945 100 Allen
1983 Aug    962 100 Alicia
1984 Sep    949 100 Diana
1985 Jul    1002    65  Bob
1985 Aug    987 80  Danny
1985 Sep    959 100 Elena
1985 Sep    942 90  Gloria
1985 Oct    971 75  Juan

Here is my code to try and read the file:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Hurricanes2


{
    public static void main(String[] args)
    {  



    double categoryAverage = 0.0;
    int categorySum = 0;
    int counter = 0;

    File fileName = new File("hurcdata2.txt");
    Scanner inFile = new Scanner("hurcdata2.txt");
    int[] year = new int[59];
    String[] month = new String[59];
    int[] pressure = new int[59];
    int[] windSpeed = new int[59];
    String[] hurcName = new String[59];

    while(inFile.hasNext())
    {
    year[counter] = inFile.nextInt();
    month[counter] = inFile.next();
    pressure[counter] = inFile.nextInt();
    windSpeed[counter] = inFile.nextInt();  
    hurcName[counter] = inFile.next();
    counter++;    

    }    

I keep getting the error. Java.util.InputMismatchException, and it highlights the line:

year[counter] = inFile.nextInt(); 

I got no idea what I've done wrong.

Community
  • 1
  • 1
anoop
  • 9
  • 1
  • 3

2 Answers2

0

The issue is because of new line characters at the end of every line in your file. After reading the first line entries, it's getting stuck at the new line character and giving you the mismatch exception since its not int (year[counter] = inFile.nextInt()).

To fix the issue, add statement below to read the new line char and ignore it after this statement hurcName[counter] = inFile.next();

      inFile.nextLine();

Full updated while loop as below:

while(inFile.hasNext())
{
   year[counter] = inFile.nextInt();
   month[counter] = inFile.next();
   pressure[counter] = inFile.nextInt();
   windSpeed[counter] = inFile.nextInt();  
   hurcName[counter] = inFile.next();

   //read the new line character at the end and ignore it
   inFile.nextLine();
   counter++;    
}   
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

Replace your while-loop with:

while(inFile.hasNextLine())
{
    year[counter] = inFile.nextInt();
    month[counter] = inFile.next();
    pressure[counter] = inFile.nextInt();
    windSpeed[counter] = inFile.nextInt();  
    hurcName[counter] = inFile.next();
    inFile().nextLine(); // to consume the new line character '\n'
    counter++;    
}

Java.util.InputMismatchException is thrown because your not reading the \n at the end of the while-loop, and instead the first inFile.nextInt() reads it.

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • I still am getting the error Java.util.InputMismatchException null (in java.util.scanner)... Is there any other reason this may be happening ? – anoop Nov 26 '12 at 00:54