Why does my code output the row and column number as 01 for 1 and 11 for 2?

76 Views Asked by At

I am trying to create a code to ask the user to specify the dimensions of an array, create the array, and fill each element with user input code. The line that is causing me issues is

System.out.println("Please enter the integer for row: " +i+1+ " column: " +j+1);

as I would like to specify which element the user is inputting, I have tried to include this. Without the +1 after "i" and "j" the output starts at 0 as it should. I am trying to add the +1 to make this easier for the user to understand.

Here is the code below:

import java.util.Scanner;

public class doubleArray{
    
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Hello! This program creates an array of numbers of your choosing and size!");
        System.out.println("Please enter the number of rows for your array!");
        
        int rows = scanner.nextInt();
        
        System.out.println("Please enter the number of columns for your array!");
        
        int columns = scanner.nextInt();
        
        int storage[][] = new int[rows][columns];
        
        for(int i=0; i<storage.length; i++) {
            System.out.println();
            for (int j=0; j<storage[i].length; j++) {
                System.out.println("Please enter the integer for row: " +i+1+ " column: " +j+1);
                storage[i][j] = scanner.nextInt();
            }
        }
        
        for(int i=0; i<storage.length; i++) {
            System.out.println();
            for(int j=0; j<storage[i].length; j++) {
                System.out.print(storage[i][j]+ " ");
            }
        }
        
        scanner.close();
    } 
}

It is intended to count up starting from 1 up until the what the user entered. ex).

Please enter the integer for row: 1 column: 1 //user input Please enter the integer for row: 1 column: 2 //user input

What actually happens is:

Please enter the integer for row: 01 column: 01 //user input Please enter the integer for row: 01 column: 11

I do not know how to troubleshoot this as I am a beginner in coding. Thank you to whoever helps!

3

There are 3 best solutions below

0
Radu Sebastian LAZIN On

In Java when you concatenate strings (or strings with other types that will be converted to strings) the concatenation goes one by one, from left to right.

int i = 0;
String s = "Number: " + i + 1;

The string s will be "Number: 01" because this translates to:

int i = 0;
String s = "Number: "; // "Number: "
s = s + i; // "Number: 0"
s = s + 0; // "Number: 01"

What you really want to do is:

int i = 0;
String s = "Number: " + (i + 1); // "Number: 1"

because first you want to compute the value which you want to concatenate, so in your code:

System.out.println("Please enter the integer for row: " + (i + 1) + " column: " + (j + 1));
0
Reilas On

"... I do not know how to troubleshoot this as I am a beginner in coding. Thank you to whoever helps! ..."

The expression is being evaluated from left to right.
It is appending i to your text, "Please enter the integer for row: ", and then appending 1 to that, etc..

You can re-order the evaluation, by parenthesizing these expressions.

System.out.println("Please enter the integer for row: " +(i+1)+ " column: " +(j+1));

Output

Hello! This program creates an array of numbers of your choosing and size!
Please enter the number of rows for your array!
1
Please enter the number of columns for your array!
1

Please enter the integer for row: 1 column: 1
0
Thomas Kläger On

Another approach would be to use System.out.printf():

System.out.printf("Please enter the integer for row: %d column: %d%n", i+1, j+1);