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!
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.
The string
swill be"Number: 01"because this translates to:What you really want to do is:
because first you want to compute the value which you want to concatenate, so in your code: