Reputation: 11
I am doing an AP CS Assignment and one of the instructions wanted me to read each column of data in my text file into 1 separate one dimensional arrays. I haven't been able to figure it out so far and could use some advice/help. When I try to run the program I also get the error "java.util.InputMismatchException null (in java.util.Scanner)
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
above is the text file and below is the code I am currently using.
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Hurricanes2
{
public static void main(String[] args)throws IOException
{
//declare and initialize variables
File fileName = new File("hurcdata2.txt");
Scanner inFile = new Scanner(fileName);
int arrayLength = 59;
int [] year = new int[arrayLength];
String [] month = new String[arrayLength];
int [] pressure = new int[arrayLength];
int [] windSpeed = new int[arrayLength];
//INPUT - read data in from the file
int n = 0;
while (inFile.hasNext())
{
year[n] = inFile.nextInt();
month[n] = inFile.next();
pressure[n] = inFile.nextInt();
windSpeed[n] = inFile.nextInt();
System.out.println (year[n] + "\n");
n++;
}
inFile.close();
Upvotes: 1
Views: 457
Reputation: 471
You have both a String
and an int
so, either always ask for a String
(and parse it later to an int
), or, if you know the formatation of your file, you could use it in your favor. In your example, you have an int
, String
, int
, int
, String
, and then a line break.
From here you could things in several ways.
One solution implies the use of a Scanner
to read an entire line, and then using the String
's method .split(String regex )
, we create an array of Strings that we can now parse. However, for the method to work correctly, you need to be consistent in the way you separate your text. In my example, I used tab. I used your examples and tabbed them all. I won't show here because the editor won't conserve the tabs.
File fileName = new File("hurcdata2.txt");
int arrayLength = 6; //I used 6 since you showed 6 lines
//You could use an ArrayList or an algorithm to dynamically
//increase the size of the arrays.
int [] year = new int[arrayLength];
String [] month = new String[arrayLength];
int [] pressure = new int[arrayLength];
int [] windSpeed = new int[arrayLength];
Scanner fileScanner = null;
try {
fileScanner = new Scanner( fileName );
}
catch ( FileNotFoundException ex ) {
//Exception handling;
}
int n = 0;
while( fileScanner.hasNext() ) {
String toParse = fileScanner.nextLine();
String[] splitedString = toParse.split( "\t" );
year[n] = Integer.parseInt( splitedString[0] );
month[n] = splitedString[1];
pressure[n] = Integer.parseInt( splitedString[2] );
windSpeed[n] = Integer.parseInt( splitedString[3] );
n++;
}
fileScanner.close();
//I will include my test to check it.
for ( int i = 0 ; i < arrayLength; i++ ) {
System.out.print( "\ni: " + i );
System.out.print( " - year: " + year[i] );
System.out.print( "; month: " + month[i] );
System.out.print( "; pressure: " + pressure[i] );
System.out.println( "; windSpeed: " + windSpeed[i] );
}
Another solution is to use a StringReader
instead of .split( String regex );
. StringReader
is like a Scanner
, but it is specialized in Strings.
You could also use a FileReader
wrapped in a BufferedReader
to read from the file.
FileReader myFileReader = null;
BufferedReader myBufferedReader = null;
try {
myFileReader = new FileReader( fileName );
myBufferedReader = new BufferedReader( myFileReader );
int n = 0;
String toParse = null;
while( (toParse = myBufferedReader.readLine()) != null ) {
//Is this confusing for you? ^
String[] splitedString = toParse.split( "\t" );
year[n] = Integer.parseInt( splitedString[0] );
month[n] = splitedString[1];
pressure[n] = Integer.parseInt( splitedString[2] );
windSpeed[n] = Integer.parseInt( splitedString[3] );
n++;
}
}
catch ( FileNotFoundException ex ) {
//Exception handling;
}
catch ( IOException ex ) {
//Exception handling;
}
finally {
try {
myFileReader.close();
}
catch ( IOException ex ) {
//Exception handling;
}
try {
myBufferedReader.close();
}
catch ( IOException ex ) {
//Exception handling;
}
}
I hope I have helped.
Have a nice day. :)
Upvotes: 0
Reputation: 49848
Each line of your file has 5 items in it, but your code only reads the first 4. So when it thinks it is starting the next line, it is actually trying to read the last item of the current line, which is not an int
.
One solution would be to read the 5th item of each line, even if you don't have any where it needs to be stored.
Upvotes: 1
Reputation: 2490
Your are not picking the last column in the while
loop. So at second loop, scanner try to read the 5th column as an int.
You must add inFile.next()
at the end of the while
loop even if you don't use the result, so the scanner is not shifted at next loop.
Upvotes: 1
Reputation: 354
Take a look at the input data
1980 Aug 945 100 Allen
1983 Aug 962 100 Alicia
1984 Sep 949 100 Diana
And what you're grabbing:
while (inFile.hasNext())
{
year[n] = inFile.nextInt();
month[n] = inFile.next();
pressure[n] = inFile.nextInt();
windSpeed[n] = inFile.nextInt();
System.out.println (year[n] + "\n");
n++;
}
There are 5 fields in the input, and you're only grabbing 4. When it loops through for the second time, it expects to grab year(int) and instead grabs name(string).
Upvotes: 1