简体   繁体   中英

First element of array is al;ways null - java

Whenever i put the first students name in when it displays them it always comes up as null however nothing else does this.Why?

package school.register;
import java.util.Scanner;

public class SchoolRegister {
    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);
        String[][] Students = new String[5][2];
        int i = 0;
        int j = 0;
        for (i = 0; i < 5; i++) {
            System.out.println("Please put in a students name.");
            Students[i][j] = Input.nextLine();
            for (j = 0; j < 1; j++) {
                System.out.println("Please put in a students test score.");
                Students[i][j] = Input.nextLine();
            }
        }
        System.out.println("Score\tName");
        display(Students);

    }

    public static void display(String x[][]) {
        for (int row = 0; row < x.length; row++) {
            for (int column = 0; column < x[row].length; column++) {
                System.out.println(x[row][column]);

            }
        }
    }
}

try this:

public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);
        String[][] Students = new String[5][2];
        int i;
        int l = 0;
        for (i = 0; i < 5; i++) {

            System.out.println("Please put in a students name.");
            Students[i][0] = Input.nextLine();
            System.out.println("Please put in a students test score.");
            Students[i][1] = Input.nextLine();
        }
        System.out.println("Score\tName");
        Display(Students);

    }
System.out.println("Please put in a students name.");
Students[i][0] = Input.nextLine();
System.out.println("Please put in a students test score.");
Students[i][1] = Input.nextLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM