-1
我正在研究一個需要我爲大學加載一些課程的項目。每門課程由課程名稱(例如「CSCI 150」,「CSCI 150L」)和章節編號(例如「CSCI 150 1」,「CSCI 150L 1」)組成。從具有不同變量類型的文本文件填充ArrayList
我試圖加載這些課程的ArrayList中,但每次我嘗試之後顯示出來,我得到一個輸入匹配異常或Nosuchelement例外
這裏是我的代碼看起來像使用文本文件IM在下面測試它。
public class Prog6 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
School university = new School();
File aFile = new File("prog6.txt");
Scanner fileReader = new Scanner(aFile);
String courseName;
int section;
int numberEnrolled;
while(fileReader.hasNextLine()){
courseName = fileReader.next() + fileReader.next();
section = fileReader.nextInt();
Course aCourse = new Course(courseName, section);
university.addCourse(aCourse);
}
university.displayCourses();
}
}
文本文件看起來就像這樣:
CSCI 150 1
CSCI 150 2
CSCI 150L 1
CSCI 140 1
MATH 174 1
MATH 132 2
MATH 412L 1
MATH 174 2
BIOL 110 1
BIOL 210 1
CBAD 310L 1
CBAD 110 1
CBAD 210 2
我建議讀一整行('nextLine()'),然後將它分割爲空格。這樣,您的程序就可以解決偏離預期格式的錯誤文本文件。 – domsson
另外,你正在描述一個'ArrayList',並且問題來自'displayCourses()'方法,但是你不會向我們展示'ArrayList'和那個方法。 – domsson
我知道displayCourses()方法的作品,即時通訊只是調用它來測試我的數組列表是從文件填充。問題是從文件中獲取值以正確存儲,因爲在同一行上有不同的變量類型,並且一些課程有150L或140L,這是一個int,最後一個字符串使我困惑。 – Strayfire