0
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

Savannah Madison
  • 378
  • 6
  • 12

2 Answers2

1

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 NumberFormatExceptions.

maloomeister
  • 1,788
  • 1
  • 7
  • 13
1

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!

Tanuj
  • 1,310
  • 7
  • 15