简单文件阅读器抛出"字符串索引超出范围:-1"错误

时间:2014-12-18 14:44:21

标签: file indexing filereader

我正在研究一个程序,该程序将读取包含名称和后面的一些数字的文本文件。每次我试图找到它会给我索引超出范围错误!文本包含学生姓名,然后是姓名和每个数字之间的逗号。

public class GradingQuizes   
{  
   final static String inResponses = "responses.txt";  
   final static String inKey = "key.txt";  
   public static void main(String[] args) throws IOException  
{  

    Scanner responses;
    Scanner answerKey;

    File resp = new File(inResponses);
    responses = new Scanner(resp);
    File xkey = new File(inKey);
    answerKey = new Scanner(xkey);

    String line = " ";

    ArrayList<Student> myClass = new ArrayList<Student>();
    while (responses.hasNextLine())
    {
        int firstComma = line.indexOf(",");
        String studentName = line.substring(0, firstComma);
        System.out.println(studentName);
    }

}
}

0 个答案:

没有答案