所以我一直在盯着代碼(不是很有效率)大約一個小時。我有一個看起來像這樣的.txt文件...使用數組讀取.txt文件
3
CSCE 155A - Fall 2011
4
Anthony Hopkins 80 90 95 87 80 78 25 17 20 22 21 24 19 22 21 23 24 21 20 25 20 55 56 110 30 20 25 8
John Smith 99 95 82 72 64 52 15 14 11 21 25 12 19 20 21 23 21 12 12 10 15 50 50 60 25 15 20 9
Pan Mei 85 92 72 45 82 78 22 13 16 22 24 10 18 12 21 24 25 10 11 14 20 58 51 95 28 14 28 7
Rafael Vega 99 45 87 52 87 99 25 25 21 21 14 19 19 25 25 20 20 18 20 24 20 60 60 60 25 16 23 8
CSCE 155A - Spring 2012
1
Paul Kubi 80 90 5 87 80 0 25 0 20 22 21 24 19 22 21 0 24 21 20 25 20 0 0 0 30 20 25 8
CSCE 155A - Fall 2012
3
Tianna Delp 99 99 99 99 99 99 24 15 16 21 25 15 19 20 21 22 21 21 23 15 15 60 50 60 20 17 20 9
Taylor Delp 95 92 80 90 82 78 25 25 25 25 24 10 25 25 25 25 25 25 25 25 25 58 51 95 28 14 28 7
Rachel Valenz 99 45 87 52 87 99 25 25 21 21 14 19 19 25 25 20 20 18 20 24 20 60 60 60 25 16 23 8
我需要使用數組從這個txt文件讀取字符串和整數。這些數字是需要閱讀的各種家庭作業和考試成績,它們混在一起,這意味着並非所有的作業整數都在一起。每學期每個學生名單開始前的數字和頂部的數字分別表示該班學生數和學期數。我想知道如何使用數組讀取此文件並將其應用於代碼中的整數... for循環也將被使用,但我還沒有那麼遠......這是我到目前爲止的內容
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Student {
int i = 1;
String[] string = new String[i];
int[] integer = new int[i];
//initialize data members
private Scanner scanner;
private String firstName;
private String lastName;
private int assignmentScore;
private int labScore;
private int quizScore;
private int homeworkScore;
private int midterm1Score;
private int midterm2Score;
private int finalExamScore;
private int zyanteScore;
private int patScore;
private int testScore;
private String letterGrade;
public void openFile(){ //method to open the grades.txt file
try { //start try statement
scanner = new Scanner(new File("gradesA5.txt")); //initialize scanner to scan from the grades.txt file
} //end try statement
catch (FileNotFoundException e) { //start catch statement
System.out.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class"); //print statement telling user that the grades.txt file is not in the right place
System.exit(0); //system exit
} //end catch statement
} //end openFile method
public void setfirstName(){
string [i] = scanner.next();
i++;
System.out.println(string [0]);
}
public void setlastName(){
}
public void setAssignmentScore(){
}
public void setLabScore(){
}
public void setQuizScore(){
}
public void setHomeworkscore(){
}
public void setMidterm1Score(){
}
public void setMidterm2Score(){
}
public void setFinalExamscore(){
}
public void setZyanteScore(){
}
public void setPATScore(){
}
public void setTestScore(){
}
public void setLetterGrade(){
}
}
我把打印語句來測試,如果它是閱讀和分配的權利
,並調用該方法單獨的類...
public class CourseStatistics {
public static void main(String[] args) {
Student myStudent = new Student();
myStudent.openFile();
myStudent.setfirstName();
}
}
....和錯誤我得到
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Student.setfirstName(Student.java:46)
at CourseStatistics.main(CourseStatistics.java:10)
感謝您的幫助!
爲什麼你需要使用數組?如果你真的這樣做,你應該先查看文件,看看有多少行,然後創建你的數組。 – Keppil
代碼中的評論由Captain Obvious創建:)關於主題:你應該學習如何讀取文本文件,然後你應該解析一些字符串,然後**數組**將幫助你存儲一些值 – SpongeBobFan