我正在做AP CS分配,其中一條指令要求我將我的文本文件中的每列數據讀入1個單獨的一維數組。到目前爲止我還沒有弄清楚,可以使用一些建議/幫助。當我嘗試運行該程序我也得到了錯誤「java.util.InputMismatchException null(在java.util.Scanner中)java中的輸入不匹配錯誤
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
上面是文本文件,並在下面是我目前使用的代碼。
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();
錯誤引用哪行代碼? –