2013-10-12 43 views
0

我的代碼,當我編譯和運行輸出文件的第一行,它說這一切:爲什麼我的代碼不會繼續讀取整個文件

java.lang.StringIndexOutOfBoundsException: String index out of range: -1 
at java.lang.String.substring(Unknown Source) 
at Popcorn1.main(Popcorn1.java:59) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272) 

,代碼:

import java.io.*; 
import java.util.Scanner; 
public class Popcorn1 { 

public static void main(String args[]) throws FileNotFoundException { 
    printHeader(); 
    File file; 

    do { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Enter the the file name"); 
     String filename = in.next(); 

     file = new File(filename); 
    } while (!file.exists()); 

    FileReader inputFile = new FileReader(file); 
    Scanner inFile = new Scanner(inputFile); 

    System.out.println("  PopCorn Co-op"); 
    System.out.println("          Production in Hundreds"); 
    System.out.println("          of Pint Jars Per Acre"); 
    System.out.println("         1 2 3 4 5 6"); 
    System.out.println("Farm Name      ---|---|---|---|---|---|"); 
    System.out.println(); 

    // Printing out title and table header for reader to easily read data 
    String errorMSG = " "; 
    while (inFile.hasNextLine()) { 
     String inputLine = inFile.nextLine(); 
     //System.out.print(inputLine); 
     int position; 
     String name; 
     int jars; 
     double acres; 
     position = inputLine.indexOf(',');//Get the Location of the comma to use as a delimiter 

     name = inputLine.substring(0, position); //Everything to the left of the comma is the farm name 
     System.out.printf("%-31s", name); 

     inputLine = inputLine.substring(position + 2, inputLine.length());   //rest of the string 
     Scanner line = new Scanner(inputLine); 
     { 
      //acres = 0; 

      jars = 0; 
      acres = 0; 
      if (line.hasNextDouble()) { 
       acres = line.nextDouble(); 
      } else { 
       errorMSG += "There is missing data"; 
      } 
      // jars = 0; 
      if (line.hasNextInt()) { 

       jars = line.nextInt(); 
      } else { 
       errorMSG += "There is missing data"; 
      } 
     } 

     int starsConversion = (int) (jars/acres/25); 

     for (int i = 1; i < starsConversion; i++) { 
      if (i == 20) { 
       System.out.print("#"); 
      } else { 
       System.out.print("*"); 
      } 
     } 
     if (starsConversion < 20) { 
      for (int i = 1; i < (21 - starsConversion); i++) { 
       System.out.print(" "); 
      } 
      System.out.print("|"); 
      { 
       System.out.println(); //go to the next line 
      } 
     } 

     System.out.println(errorMSG); 
    } 
} 
} 

回答

3

這是很難確定到底是怎麼回事沒有文件,但它看起來像

position = inputLine.indexOf(','); 

將返回-1(沒有找到」, '在文件中),因此給你一個無效的索引。

嘗試在計算後打印出位置以確保其有效。

System.out.println("Position: " + position); 
System.out.println("Length: " + inputLine.length()); 
System.out.println("Trimmed: " + inputLine.trim().length()); 

如果長度爲0或只是空格,那麼它將不會有逗號。

這可能是固定的讀取nextLine後添加以下:

if(inputLine.trim().length() == 0) 
    continue 
if(inputLine.indexOf(",") == -1) 
    System.out.println("There is no comma on this line: " + inputLine); 
+0

則可以鍵入,一個expample。我打電話後打印什麼意思。我已經打印出來了 – user2872881

相關問題