java错误中的二维数组(以null打印)

时间:2017-11-06 17:30:41

标签: java arrays loops oop dynamic-arrays

它以空值打印......我如何摆脱它?它给了我一些例子:

现:

Leo null null null

如果没有达到4,我希望除了Leo之外别无其他。

这是我目前的代码:

import java.util.*;
import java.io.*;

public class Trial {

    public static void main(String[] args) {

        String [] Student ={"Tom", "Jack", "Gio", "Leo"};
        String[][] Attendance = new String[4][4];



        System.out.println("STUDENT LIST-TAKE ATTENDANCE");

        for(int i = 0; i<4; i++){
            System.out.println("Type 1 for Present, 2 for Absent, 3 for Fieldtrip, or 4 for Late " + "for " + Student[i]);
            Scanner scan1 = new Scanner(System.in);
            double Att = scan1.nextDouble();

            int j = 0;

            if (Att == 1){
                Attendance[0][j] = Student[i];
                j++;
            }

            else if(Att == 2){
                Attendance[1][j] = Student[i];
                j++;
            }

            else if(Att == 3){
                Attendance[2][j] = Student[i];
                j++;
            }

            else if(Att == 4){
                Attendance[3][j] = Student[i];
                j++;
            }

            else
                System.out.println("Retry");

        }

        System.out.println("Present: ");
        for (int k = 0; k<=3; k++)
            System.out.print(Attendance[0][k] + "   ");
            System.out.println();

        System.out.println("Absent: ");
        for (int k = 0; k<=3; k++)
            System.out.print(Attendance[1][k] + "   ");
            System.out.println();

        System.out.println("Fieldtrip: ");
        for (int k = 0; k<=3; k++)
            System.out.print(Attendance[2][k] + "   ");
            System.out.println();

        System.out.println("Late: ");
        for (int k = 0; k<=3; k++)
            System.out.print(Attendance[3][k] + "   ");
            System.out.println();


        /*
        String[][] Attendance = {
            { "Present", },
            { "Absent",  },
            { "Fieldtrip", },
            { "Late",  }

        };


            System.out.println();
        for (int i = 0; i < Attendance; i++) {
            System.out.print(Attendance[i][0] + ": ");
            for (int j = 1; j < Attendance[i].length; j++) {
                System.out.print(Attendance[i][j] + " ");
         }
         */

        }
    }

1 个答案:

答案 0 :(得分:0)

我将以此为例,你应该能够弄清楚其余部分:

System.out.println("Present: ");
for (int k = 0; k<=3; k++)
    System.out.print(Attendance[0][k] + "   ");
System.out.println();

您现在遇到的问题是即使它们为空也会打印出来,所以在打印之前添加一个检查:

System.out.println("Present: ");
for (int k = 0; k<=3; k++) {
    if (Attendance[0][k] != null) {
        System.out.print(Attendance[0][k] + "   ");
    }
}
System.out.println();

现在,如果您确定一旦遇到空值,每个后续值也将为空,如果遇到这样的情况,您可以跳出循环:

System.out.println("Present: ");
for (int k = 0; k<=3; k++) {
    if (Attendance[0][k] != null) {
        System.out.print(Attendance[0][k] + "   ");
    } else {
        break;
    }
}
System.out.println();