简体   繁体   中英

2D array data null?

I have created a program to accept input from user and will store in 2d array. Example situation, the user enter an input no of semester = 2; then the no of course also = 2;. My loop is working correctly, but the data is not store well.

The output : null null null input4

It should like this : input1 input2 input3 input4

Here my code :

BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));

    try {

        System.out.print("Enter no of semester : ");
        data = inData.readLine();
        noSem = Integer.parseInt(data);

        // Semester loop
        for(int i = 0; i < noSem; i++) {

            System.out.println("\n\tSemester" + (i + 1));
            System.out.print("Enter number of course : ");
            data = inData.readLine();
            noCourse = Integer.parseInt(data);

            // course details loop
            for(int u = 0; u < noCourse; u++) {

                System.out.println("Enter Course Code : ");
                System.out.print("Enter Course Name : ");
                sbjName = new String[noSem][noCourse];
                sbjName[i][u] = inData.readLine();

                System.out.println("Enter Credit Hour : ");
                System.out.println("Enter Marks : ");
                System.out.println("\n");

            }
        }

        for(int x = 0; x < noSem; x++) {
            for(int y = 0; y < noCourse; y++) {
                System.out.println(sbjName[x][y]);
            }
        }
    }
BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));

try {
    System.out.print("Enter no of semester : ");

    int noSem = Integer.parseInt(inData.readLine());
    String[][] sbjName = new String[noSem][];

    // Semester loop
    for (int i = 0; i < noSem; i++) {
        System.out.println("\n\tSemester" + (i + 1));
        System.out.print("Enter number of course : ");

        int noCourse = Integer.parseInt(inData.readLine());
        sbjName[i] = new String[noCourse];

        // course details loop
        for (int u = 0; u < noCourse; u++) {
            System.out.println("Enter Course Code : ");
            System.out.print("Enter Course Name : ");
            sbjName[i][u] = inData.readLine();

            System.out.println("Enter Credit Hour : ");
            System.out.println("Enter Marks : ");
            System.out.println("\n");
        }
    }

    for (int x = 0; x < sbjName.length; x++) {
        for (int y = 0; y < sbjName[x].length; y++) {
            System.out.println(sbjName[x][y]);
        }
    }
}

Your problem is here:

sbjName = new String[noSem][noCourse];

That statement creates a new array during each iteration. In other words: you are throwing away the array(s) that you created before; thus any information that you previously stored in those arrays ... is thrown away too!

You want to make that call only once ; so you move it outside of your loops!

The problem seems to be that you are recreating the 2d array each time you go through the first for loop.

to solve this, you need to define the number of courses available in the beginning:

    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
    String[][] sbjName = null;
    String data;
    int noSem = 2;
    int noCourse = 3; // say there are 3 courses that are available

    try {

        System.out.print("Enter no of semester : ");
        data = inData.readLine();
        noSem = Integer.parseInt(data);
        System.out.print("Enter number of courses available : ");
        data = inData.readLine();
        noCourse = Integer.parseInt(data);
        sbjName = new String[noSem][noCourse];
        // Semester loop
        for (int i = 0; i < noSem; i++) {

            System.out.println("\n\tSemester" + (i + 1));
            System.out.print("Enter number of course : ");
            data = inData.readLine();
            int courseNum = Integer.parseInt(data);

            // course details loop
            for (int u = 0; u < courseNum; u++) {

                System.out.println("Enter Course Code : ");
                System.out.print("Enter Course Name : ");

                sbjName[i][u] = inData.readLine();

                System.out.println("Enter Credit Hour : ");
                System.out.println("Enter Marks : ");
                System.out.println("\n");

            }
        }

        for (int x = 0; x < noSem; x++) {
            for (int y = 0; y < noCourse; y++) {
                System.out.println(sbjName[x][y]);
            }
        }

    } catch (Exception e) {

    }

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