扫描程序不匹配读取文本文件时出现异常

时间:2017-03-22 10:43:07

标签: java.util.scanner inputmismatchexception

我的扫描仪正在使用分隔符从文本文件中读取。然而,当我运行程序时,唯一打印出的行是第一行数据,然后我会抛出输入不匹配异常。我相信错误是它没有移动到下一行。我理解不匹配是如何工作的我只是不确定如何解决它。我试过把scanner.nextLine();你可以在下面的代码中看到。

以下是我的扫描仪代码:

 /**
 * Method for reading the data from the electricToolData.txt file.
 */
public void readElectricToolData()
{
  Frame myFrame = null; // initialises frame to null
  FileDialog fileBox = new FileDialog(myFrame,
                 "Open", FileDialog.LOAD);
  fileBox.setVisible(true);

  String directoryPath = fileBox.getDirectory();
  String filename = fileBox.getFile();

  System.out.println(filename + "   " + directoryPath); // prints out name of file and directory path


  try {
   File dataFile = new File(filename);
   Scanner scanner = new Scanner(dataFile);

   while(scanner.hasNext())
   {
       String lineOfText = scanner.nextLine(); // reads the next line of the file and stores as String

       //if statement checks if line starts with either "//" or space.
        if (lineOfText.startsWith("//") || lineOfText.isEmpty())
        { 

                      }
       else // now got real data
       {

        Scanner scanner2 = new Scanner(lineOfText);
        scanner2.useDelimiter(",");
        lineOfText.trim();
        System.out.println(lineOfText);


           while(scanner2.hasNext()) 
           {
          //lineOfText.trim();
          boolean mRechargeable = scanner.nextBoolean();
          String mPower = scanner.next();
          String mToolName = scanner.next();
          String mItemCode = scanner.next();
          int mTimesBorrowed = scanner.nextInt();
          boolean mOnLoan = scanner.nextBoolean();
          int mCost = scanner.nextInt();
          int mWeight = scanner.nextInt();
          scanner.nextLine();



          ElectricTool electricTool = new ElectricTool(mToolName, mItemCode, mTimesBorrowed, mCost, mWeight, mPower);
          toolsList.add(electricTool);

        }

       }





       //System.out.println(lineOfText); // prints out string
   }
   scanner.close();
   scanner.close(); // closes scanner


   }
       catch(FileNotFoundException e) 
  {
      System.err.println("Caught IOException: " + e.getMessage());
    }

  }   

错误显示在boolean mRechargeable = scanner.nextBoolean();.

这是数据文件:

// this is a comment, any lines that start with //
// (and blank lines) should be ignored

// data is rechargeable, power, toolName, itemCode, timesBorrowed, onLoan, cost, weight
true,18V,Makita BHP452RFWX,RD2001,12,false,14995,1800
true,10.8V,Flex Impact Screwdriver FIS439,RD2834,14,true,13499,1200     
false,1350W,DeWalt D23650-GB Circular Saw, RD6582,54,true,14997,5400
false,1500W,Milwaukee DD2-160XE Diamond Core Drill,RD4734,50,false,38894,9000
true,10.8V,Bosch GSR10.8-Li Drill Driver,RD3021,25,true,9995,820
 false,900W,Bosch GSB19-2REA Percussion Drill,RD8654,85,false,19999,4567
true,10.8V,Flex Impact Screwdriver FIS439, RD2835,14,false,13499,1200 
true,18V,DeWalt DW936 Circular Saw,RD4352,18,false,19999,3300 
false,2100W,Sparky FK652 Wall Chaser,RD7625,15,false,29994,8400

1 个答案:

答案 0 :(得分:0)

问题是String.trim()不能像你想象的那样工作:它不会改变调用它的String,而是返回一个有效删除了空格的新String。这导致行不被修剪,并且其上有大量空格字符的“空行”无法使用您的第一个scanner.nextBoolean()调用进行解析。

所以,更新:

String lineOfText = scanner.nextLine(); // reads the next line of the file and stores as String

是:

// read the next line of the file, removing enclosing whitespace
String lineOfText = scanner.nextLine().trim();

评论理想情况下应该在该行之前,因为它更容易阅读和格式化,更明显的是它是一个评论。同时删除冗余的lineofText.trim();稍后在代码中。

此外,请记住在完成后关闭所有扫描程序实例(所以这里每行一个,文件一个)。

下一个问题是,在内部循环中,构建您的ElectricTool实例,您在扫描器上调用方法,而不是scan2(将其重命名为更加语义的东西,例如itemScanner):

Scanner itemScanner = new Scanner(lineOfText);
itemScanner.useDelimiter(",");

boolean mRechargeable = itemScanner.nextBoolean();
String mPower = itemScanner.next();
String mToolName = itemScanner.next();
String mItemCode = itemScanner.next();
int mTimesBorrowed = itemScanner.nextInt();
boolean mOnLoan = itemScanner.nextBoolean();
int mCost = itemScanner.nextInt();
int mWeight = itemScanner.nextInt();