【问题标题】:Input Mismatch error in javajava中的输入不匹配错误
【发布时间】:2016-10-27 13:32:35
【问题描述】:

我正在做 AP CS 作业,其中一条指令要我将文本文件中的每一列数据读入 1 个单独的一维数组。到目前为止,我还无法弄清楚,可以使用一些建议/帮助。当我尝试运行程序时,我也收到错误“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

上面是文本文件,下面是我当前使用的代码。

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();

【问题讨论】:

  • 错误是指哪一行代码?

标签: java arrays file exception text


【解决方案1】:

看看输入数据

1980 Aug    945 100 Allen
1983 Aug    962 100 Alicia
1984 Sep    949 100 Diana

还有你要抓的东西:

        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++;
        }

输入中有 5 个字段,而您只抓取 4 个。当它第二次循环时,它希望抓取 year(int) 而不是抓取 name(string)。

【讨论】:

    【解决方案2】:

    您没有选择while 循环中的最后一列。所以在第二个循环中,scanner 尝试将第 5 列读取为 int。 即使您不使用结果,也必须在 while 循环的末尾添加 inFile.next(),因此扫描仪不会在下一个循环中移动。

    【讨论】:

      【解决方案3】:

      文件的每一行都有 5 项,但你的代码只读取前 4 项。所以当它认为它正在开始下一行时,它实际上是在尝试读取当前行的最后一项,即不是int

      一种解决方案是读取每行的第 5 项,即使您没有任何需要存储它的位置。

      【讨论】:

      • 是的,我一直在思考为什么它不会读取,我认为这可能是因为它在 int "1980" 之后一直读取下一个字符串。但是我想弄清楚的是如何读取列,这样它就不会继续到字符串上。所以对于 int 年,它只会读取 1980,1983 等。
      【解决方案4】:

      您同时拥有 Stringint,因此,要么总是要求 String(稍后将其解析为 int),要么,如果您知道文件的格式,您可以使用它对你有利。在您的示例中,您有一个 intStringintintString,然后是换行符。

      从这里您可以通过多种方式进行操作。

      一种解决方案意味着使用Scanner 读取整行,然后使用String 的方法.split(String regex ),我们创建一个我们现在可以解析的字符串数组。但是,要使该方法正确工作,您需要在分隔文本的方式上保持一致。在我的示例中,我使用了制表符。我使用了您的示例并将它们全部标记了。我不会在这里显示,因为编辑器不会保留选项卡。

      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] );
      }
      

      另一种解决方案是使用StringReader 而不是.split( String regex );StringReader 类似于 Scanner,但它专门用于字符串。

      您还可以使用包裹在BufferedReader 中的FileReader 来读取文件。

      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;
          }        
      }
      

      我希望我有所帮助。

      祝你有美好的一天。 :)

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 2012-07-01
        • 1970-01-01
        • 2013-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-03
        • 1970-01-01
        相关资源
        最近更新 更多