2017-03-22 7 views
0

スキャナが区切り文字を使用してテキストファイルから読み取り中です。しかし、私がプログラムを実行すると、出力される唯一の行がデータの最初の行になり、入力ミスマッチの例外がスローされます。私はエラーが次の行に移動しないと信じています。ミスマッチがどのように働くのか理解していますが、私はそれを修正する方法がわかりません。私は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()); 
    } 

    } 

エラーがmRechargeable = scanner.nextBoolean()ブール値で示します;.問題はString.trimは()あなたはそれがないと思う方法で動作しないということである

// 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 

答えて

0

:ここ

は、データファイルで、それはそれは上と呼ばれている文字列を変化させませんが、しかし、空白を削除した新しい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(); 

コメントは、理想的には、ラインをpreceed必要があります。冗長なlineofTrim()も削除してください。コードの後半。

さらに、完了したらすべてのScannerインスタンスを閉じるようにしてください(ここでは、各行に1つ、ファイルに1つ)。

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

I:

次の問題は、あなたのElectricToolインスタンスを構築し、内側のループで、あなたが(もっとセマンティックなもの、例えばitemScannerにこの名前を変更)ではなくscanner2よりも、スキャナのメソッドを呼び出しているということです私が第2のスキャナとは対照的に第1のスキャナを使用していたと言いました。返信いただきありがとうございます、コードは完璧に今働いています! –

+0

愚かな質問はありません - 私たち全員が過去にこのように間違いを犯しました。私は将来もそれをやり続けます! –

関連する問題