I need help with reading integers into a 2D square array of N x N dimensions.
For example, if the user input is:
123
333
414
Then I will need an array:
{
{1, 2, 3},
{3, 3, 3},
{4, 1, 4}
}
The problem that I am having is that there is no space between these integers. If there were a space, I could just do
for (int i = 0 ; i < N; i++) {
for(int j = 0; j < N ; j++) {
myArray[i][j] = scan.nextInt();
}
}
I approached this problem by trying to use substrings and parsing it into the array, although I did not get anywhere.
Another approach (Edit)
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
myArray[i][j] = Integer.parseInt(scan.nextLine().substring(j, j+1));
}
}
This does not work either - it keeps running after the three lines are entered.