2013-10-31 165 views
0

所以我一直在盯着代碼(不是很有效率)大約一個小時。我有一個看起來像這樣的.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) 

感謝您的幫助!

+0

爲什麼你需要使用數組?如果你真的這樣做,你應該先查看文件,看看有多少行,然後創建你的數組。 – Keppil

+0

代碼中的評論由Captain Obvious創建:)關於主題:你應該學習如何讀取文本文件,然後你應該解析一些字符串,然後**數組**將幫助你存儲一些值 – SpongeBobFan

回答

0

當你增加的方法setFirstNamei,它超出邊界,因爲你聲明的變量有1.嘗試的大小來增加數組的大小,並開始從索引存儲0

int i = 1; 

String[] string = new String[i]; 
int[] integer = new int[i]; 
0

因爲你開始與尺寸1這裏的數組:

int i = 1; 

String[] string = new String[i]; 

然後嘗試並分配陣列@指標1,這是你的數組的邊界,在這裏:

string [i] = scanner.next(); 

嘗試,而不是:

string [i-1] = scanner.next(); 

另外,我看到,當您嘗試另一個字符串添加到大小的陣列1.您是否正在添加動態增長數組你的學生計劃在會再次發生類?

0

數組是唯一的。這意味着如果您將數組設置爲:int[] arr = new int[5],則索引「5」將不可用。在這種情況下,5將是尺寸,所以索引將會變爲0-1-2-3-4。 如果您要訪問只有一個元素的陣列,則必須使用arr[0]訪問它。

0

你的代碼背後的基本理念應該是:

main: 
    read the number of semesters 
    create an array for n Semesters 
    n times: 
    read a Semester and store it in the array 

reading a semester: 
    read the number of students 
    create an array for n Students 
    n times: 
    read a Student and store it in the array 

reading a student: 
    (left as an excercise) 

該方案建議您創建一個包含stuents類Semester,並含有牌號類Student。另一個類叫GradeProcessor應該有方法readSemesterreadStudent

0

您聲明array[String]是大小11這一點:

int i = 1; 

String[] string = new String[i]; 

當你去參考[i],這是1,尺寸爲1,你必須要記住,拳頭指數由0表示。你將不得不參考0。我也不確定你的意思是初始化陣列的大小爲1.

public void setfirstName(){ 
    string [i] = scanner.next(); 
    i++; 
    System.out.println(string [0]); 
}