2014-11-23 32 views
0

我想分裂我的變量,並輸入我的學生數據,所以我可以繼續我的成績計算程序,但我第二次分裂輸入的字符串,有一個問題,我不能找出它的起因。麻煩分裂Java程序中的數據

用戶將輸入信息,看起來像這樣 約翰·丹佛:E100 Q70 Q50 H100 E100 E90 H80 Q60 H100

程序需要裂開所有這些數據,並得到進入到一個數組名,然後是輸入數據中的「e」,「q」或「h」代表的考試分數,測驗分數和家庭作業分數。

import java.util.Scanner; 

public class GradeCalcWithArrays { /* 
           * Daniel The purpose is to calculate 
           * entered grades 
           */ 
public static void main(String[] args) { 

    Scanner s = new Scanner(System.in); 

    boolean done = false; 
    boolean quit = false; 
    int choice = 0; 
    int maxstudents = 200; 

    int[] examstats = new int[3]; /* 
           * Array created to store the information 
           * entered for exams 
           */ 
    int[] quizstats = new int[3]; /* 
           * Array created to store the information 
           * entered for quizzes 
           */ 
    int[] homeworkstats = new int[3]; /* 
            * Array created to store the 
            * information entered for homework 
            */ 

    String[] studentnames = new String[maxstudents]; /* 
                * Array created to 
                * store the student 
                * name information 
                * entered 
                */ 

    System.out.println("Welcome to GradeBook!"); 
    System.out.println("Please provide grade item details"); 

    System.out.print("Exams (number, points, weight):"); 

    examstats[0] = s.nextInt(); // inputs exam number 
    examstats[1] = s.nextInt(); // inputs exam points 
    examstats[2] = s.nextInt(); // inputs exam weight 

    System.out.print("Quizzes  (number, points, weight):"); 

    quizstats[0] = s.nextInt(); // inputs quiz number 
    quizstats[1] = s.nextInt(); // inputs quiz points 
    quizstats[2] = s.nextInt(); // inputs quiz weight 

    System.out.print("Homework (number, points, weight):"); 

    homeworkstats[0] = s.nextInt(); // inputs homework number 
    homeworkstats[1] = s.nextInt(); // inputs homework points 
    homeworkstats[2] = s.nextInt(); // inputs homework weight 

    double[] examscores = new double[examstats[0]]; 
    double[] quizscores = new double[quizstats[0]]; 
    double[] hwscores = new double[homeworkstats[0]]; 

    System.out.println("--------------------"); 

    do { 
     System.out.println("What would you like to do?"); 
     System.out.println(" 1 Add student data"); 
     System.out.println(" 2 Display student grades & statistics"); 
     System.out.println(" 3 Plot grade distribution"); 
     System.out.println(" 4 Quit"); 
     System.out.print("Your choice:"); 
     choice = s.nextInt(); /* 
          * Choice will determine what the next course of 
          * action will be with the program 
          */ 

     if (choice == 1) { 
      System.out.println("Enter student data:"); 

      for (int i = 0; i <= maxstudents; i++) { 
       System.out.print("Data>"); 
       String dataentry = s.nextLine(); 

       String[] firstsplit = dataentry.split(":"); 
       studentnames[i] = firstsplit[0]; 
       String[] secondsplit = firstsplit[1].split(" "); 

       for (int j = 0; j <= maxstudents; j++) { 

        String c; 

        c = secondsplit[j].substring(0, 1); 

        if (c == "e") { 
         secondsplit = secondsplit[j].split("e"); 
         examscores[i] = Double.parseDouble(secondsplit[j]); 

        } 
        if (c == "q") { 
         secondsplit = secondsplit[j].split("q"); 
         quizscores[i] = Double.parseDouble(secondsplit[j]); 

        } 
        if (c == "h") { 
         secondsplit = secondsplit[j].split("h"); 
         hwscores[i] = Double.parseDouble(secondsplit[j]); 

        } 

        if (dataentry.equals("done")) { 
         break; 
        } 
       } 
      } 
     } 

     if (choice == 2) { 

     } 

     if (choice == 3) { 

     } 

     if (choice == 4) { 
      quit = true; 
      System.out.println("Good bye!"); 
     } 

    } while (quit == false); 

} 

} 

我得到一個錯誤,指出:異常線程 「main」 java.lang.ArrayIndexOutOfBoundsException:1 在GradeCalcWithArrays.main(GradeCalcWithArrays.java:83)

有人可以幫助我解決這個問題並告訴我,如果我的程序正在工作?

回答

0

有一點是錯誤的是,你有maxstudents設置爲200,你分配的大小爲200的studentnames數組,但這意味着有效的下標是從0到199,但你的for循環使用小於或等於對200測試:

 `for (int i = 0; i <= maxstudents; i++)` 

這意味着,i是200上的最後一次迭代,所以在該迭代您的代碼將有效地執行studentnames[200] = firstsplit[0];,這將導致一個ArrayIndexOutOfBoundsException。

但是,如果在不跳出for循環的情況下達到第200次迭代,上述問題將會成爲問題。看看代碼,我確實發現如果輸入是「完成的」,你會試圖破解,但是break語句實際上在嵌套的for循環中,所以它不會脫離外部循環。在進行第一次拆分之前,該測試真的應該發生。如果輸入「完成」,甚至沒有理由甚至嘗試拆分,所以沒有理由進入嵌套for循環。

還有其他問題:嵌套for循環不應該針對maxstudents進行測試,因爲它是您關心的等級數。我會讓其他人爲你弄清楚 - 提示其中的一個:你應該檢查你的輸入是否有錯誤。