我正在處理一個程序,我需要掃描一個txt文件。 txt文件保證在不同類型的何時何地出現時遵循特定的格式。我嘗試在我的程序中利用這一點,並使用掃描儀將我認識的零件放入整數,以及雙打和字符串。當我運行我的程序它告訴我,我有一個類型不匹配異常,我知道由於所有我的類型相匹配的txt文件的格式,所以我怎麼讓IDE認爲這沒關係。這裏有一塊有問題的代碼是有幫助的。Java輸入不匹配異常與專門的txt文件
ArrayList<Student>studentList=new ArrayList<Student>();//makes a new Array list that we can fill with students.
FileInputStream in=new FileInputStream("studentList.txt");//inputs the text file we want into a File Input Stream
Scanner scnr=new Scanner(in);//Scanner using the Input Stream
for(int i=0;i<scnr.nextInt();i++)//we know the first number is the number of minor students so we read in a new minor that number of times
{
Undergrad j=new Undergrad();//make a new undergrad
j.setDegreeType("MINOR");//make the degree type minor because we know everyone in this loop is a minor.
j.setFirstName(scnr.next());//we know the next thing is the student's first name
j.setLastName(scnr.next());//we know the next thing is the student's last name
j.setID(scnr.nextInt());//we know the next thing is the student's ID
j.setGPA(scnr.nextDouble());//we know the next thing is the student's GPA
j.setCreditHours(scnr.nextDouble());//we know the next thing is the student's credit hours
studentList.add(j);//Finally, we add j to the arraylist, once it has all the elements it needs
}
哪一行會導致錯誤?錯誤是否發生在列表中的每個學生或特定的學生? – bradimus
你可以發佈'studentList.txt'嗎?或者至少是造成問題的部分? – bradimus