简体   繁体   中英

Java prints “null” while taking in array of strings as input from the user using Scanner and ends abruptly

import java.util.Scanner;
class ArrayofArrays {
    public static void main(String[] args) {
        String[][] ListofNames = {
            {"barbie","cinderella","troomtroom"},
            {"wonderwoman","captainmarvel","Cheetah"}
        };
        for(String[] i : ListofNames) {
            for(String x: i) {
                System.out.println(x);
            }
        }
        int r,c;
        Scanner obj = new Scanner(System.in);
        System.out.println("Enter rows\n");
        r = obj.nextInt();
        System.out.println("Enter columns\n");
        c = obj.nextInt();
        String[][] Inputnames = new String[r][c];
        for(int j = 0;j<r;j++) {
            for(int l = 0;l<c;l++) {
                System.out.println("Enter name\n");
                Inputnames[j][l] = obj.nextLine();
            }
        for(int m = 0;m<r;m++) {
            for(int n = 0;n<c;n++) {
                System.out.println(Inputnames[m][n]);
                }
            }
        }
    }
}

I was learning java and when I tried to to take in Array of Arrays consisting of strings as user input, it printed out null and didnt take further inputs. What am I missing out?

The output is like this on the cmd:

C:\Users\dynam\Desktop\Java Files>java ArrayofArrays barbie cinderella troomtroom wonderwoman captainmarvel Cheetah Enter rows

2 Enter columns

1 Enter name

null Enter name

batman

batman

I've seen this happen a lot in several questions. The issue is described here more in detail.

To fix your issue, replace the obj.nextInt() with Integer.parseInt(obj.nextLine()) . So basically your code should look like the following:

    System.out.println("Enter rows\n");
    r = Integer.parseInt(obj.nextLine());
    System.out.println("Enter columns\n");
    c = Integer.parseInt(obj.nextLine());

If by any chance your will enter something else than an integer here, you might have to use a try-catch block to make sure you deal with NumberFormatException s.

Because the Scanner.nextInt method does not read the newline character in your input created after pressing "Enter," and so the call to Scanner.nextLine returns after reading that new line.

Also Looks like your for loops are messed up, try the following code (cleaned up a bit)-

package com.example.demo;

import java.util.Scanner;

class ArrayofArrays {
    public static void main(String[] args) {
        String[][] ListofNames = {
                {"barbie", "cinderella", "troomtroom"},
                {"wonderwoman", "captainmarvel", "Cheetah"}
        };
        for (String[] i : ListofNames) {
            for (String x : i) {
                System.out.println(x);
            }
        }
        int r, c;
        Scanner obj = new Scanner(System.in);
        System.out.println("Enter rows\n");
        r = obj.nextInt();
        System.out.println("Enter columns\n");
        c = obj.nextInt();
        obj.nextLine();
        String[][] Inputnames = new String[r][c];
        for (int j = 0; j < r; j++) {
            for (int l = 0; l < c; l++) {
                System.out.println("Enter name\n");
                Inputnames[j][l] = obj.nextLine();

            }
        }
        for (int m = 0; m < r; m++) {
            for (int n = 0; n < c; n++) {
                System.out.println(Inputnames[m][n]);
            }
        }
    }
}

Hope this helps!

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