循环内的getter在赋值后返回null

时间:2014-10-24 18:05:49

标签: java oop loops null

我的程序出了点问题。简而言之,我所做的就是:

  1. 选择"添加新学生记录"。

  2. 当我尝试显示新添加的值(名字,姓氏,课程)时,它们全部返回为空。

  3. 我无法解决错误,但我会尝试发布所需的代码,以便让所有人都能获得更好的观点。

    主要课程:

    public static void main(String[] args) throws Exception{
    
    
        String cont1=" ";
        String X;        
        int x1,arr_count=1;
        String[] option = {"First Name","Last Name","Course"};
    
        BufferedReader reader1=new BufferedReader(new InputStreamReader(System.in));
        do{      
    
        System.out.println("\nWelcome! Please select a task: \nA. View a record.\nB. Add a record.\nC. Edit a record.\nD. Exit the program.");
        String option1 = reader1.readLine();
    
        if("a".equals(option1)|| "A".equals(option1)){
            System.out.println("Please enter the Student ID for the record to view.");
            String a = reader1.readLine();
            x1 = Integer.parseInt(a);
            for(int y=0;y<3;y++){
            System.out.print(Records.getRecord(x1,y) + " ");
            }            
            System.out.print("\nSubjects and Grades: \n");
            for(int y=3;y<6;y++){
            System.out.print(Records.getRecord(x1,y) + " (" + Records.getRecord(x1,y+3) + ")\n");
            } 
    
            System.out.println("\nReturn to Main Menu? (Y/N)");
            cont1= reader1.readLine(); 
        }
    
        else if("b".equals(option1)||"B".equals(option1)){
            arr_count++;
            for (int y = 0; y<3;y++){
            System.out.println("Enter Value for "+ option [y]+":"  ); 
            X = reader1.readLine();
            X = Records.getRecord(arr_count,y);          
            }
    
            System.out.println("\nNew Student added:\nID number: "+ arr_count);
            for(int y=0;y<3;y++){
            X = Records.getRecord(arr_count,y);
            System.out.print(X + " ");
            }
    

    记录课程:

    public class Records {
    
        public static String getRecord(int recordID,int index1)throws Exception{
            //BufferedReader reader1=new BufferedReader(new InputStreamReader(System.in));
            String records[][] = new String [10][9];
            records[0][0] = "John";    records[0][3] = "English";  records[0][6] = "C";
            records[0][1] = "Smith";   records[0][4] = "Math";     records[0][7] = "B";
            records[0][2] = "BS IT";   records[0][5] = "Graphics"; records[0][8] = "A";
    
            records[1][0] = "Juan";    records[1][3] = "Graphics"; records[1][6] = "B";
            records[1][1] = "Ponce";   records[1][4] = "Math";     records[1][7] = "B";
            records[1][2] = "BS ECE";  records[1][5] = "Robotics"; records[1][8] = "A";
    
            /**records[2][0] = "";     records[2][3] = "";     records[2][6] = "";
            records[2][1] = "";     records[2][4] = "";     records[2][7] = "";
            records[2][2] = "";     records[2][5] = "";     records[2][8] = "";
    
            records[3][0] = "";     records[3][3] = "";     records[3][6] = "";
            records[3][1] = "";     records[3][4] = "";     records[3][7] = "";
            records[3][2] = "";    records[3][5] = "";     records[3][8] = "";
    
            records[4][0] = "";     records[4][3] = "";     records[4][6] = "";
            records[4][1] = "";     records[4][4] = "";     records[4][7] = "";
            records[4][2] = "";    records[4][5] = "";     records[4][8] = "";*/
    
    
    
        return records[recordID][index1];
        }
    }
    

    我试图将空白值设置到记录数组,同时将用于将新值分配给数组的函数放入Records类中,但我仍然无法找到使其工作的方法。

2 个答案:

答案 0 :(得分:0)

您永远不会设置记录的值。

X = reader1.readLine();
X = Records.getRecord(arr_count,y);  

丢弃读取的值。在Record类中创建一个setValue方法 -

X = reader1.readLine();
Records.setValue(arr_count, y, X);

--- inRecord     setValue(x,y,val){         记录[x] [y] = val;     }

答案 1 :(得分:0)

看来您的Main类不完整。 do while循环停止的条件是什么?主循环方法还有其他内容吗?