0

Hello I seem to be having an error. The code is supposed to ask the user the number of rows and columns. Next, it will ask to enter fruits then list it. Here is the code:

Scanner input = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = input.nextInt();
System.out.print("Enter number of columns: ");
int columns = input.nextInt();
String[][] array = new String[rows][columns];
System.out.println("Enter fruits:");
    for(int i=0;i<array.length;i++){
        for(int j=0;j<array[i].length;j++){
            array[i][j] = input.nextLine();
        }
      System.out.println();
    }

   for(int i=0;i<array.length;i++){
    for(int j=0;j<array[i].length;j++){
      System.out.print(array[i][j]+"\t");
     }
    System.out.println();
   }

The problem it asks for one less String input. For example: Enter number of rows: 2 Enter number of columns: 2 Enter fruits: apple banana orange Then it prints the fruits. It should have allowed four fruits to be inputted, since rows x columns, which is 2x2 = 4, but it only asked for three inputs. What did I do wrong?

1 Answers1

0

All you have to do is:

for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++)