0

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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Read your input as String, convert it with `toCharArray` method and then add to your 3-dimensional array. – DimaSan Jan 14 '17 at 17:45
  • http://stackoverflow.com/questions/33696011/how-to-use-split-a-string-after-a-certain-length – nicomp Jan 14 '17 at 17:45
  • Is there any way to keep this as a two-dimensional int array? I just added another approach that I think could work. –  Jan 14 '17 at 17:46
  • ***there is no space between these integers...***....are those ints between 0 and 9 then??? – ΦXocę 웃 Пepeúpa ツ Jan 14 '17 at 17:49
  • Yes. They are all single-digit integers. –  Jan 14 '17 at 17:50
  • In Java 8 you could use `"123".chars().map(ch -> ch-'0').toArray();` which returns `[1, 2, 3]` array of integers. But be sure that string really contains only digits. Use this approach for each row. – Pshemo Jan 14 '17 at 17:57
  • How can I do this with a string array, then? –  Jan 14 '17 at 18:02
  • `scan.nextLine()` statment is running `N*N` times , so it requires N*N new line inputs – Rajmani Arya Jan 14 '17 at 18:03
  • 1
    Try this `for (int i = 0; i < N; i++) { String s = scan.nextLine(); for (int j = 0; j < N; j++) { myArray[i][j] = Integer.parseInt(substring(j, j+1)); } }` – Rajmani Arya Jan 14 '17 at 18:04
  • @RajmaniArya thanks! this worked. –  Jan 14 '17 at 18:08

2 Answers2

1

Perhaps this helps:

public static void main( String[] args ) {

    Scanner scanner = new Scanner( System.in );

    int[][] array = new int[3][3];

    for ( int[] ints : array ) {
        char[] line = scanner.nextLine().toCharArray();
        for ( int i = 0; i < line.length; i++ ) {
            ints[i] = Character.getNumericValue( line[i] );
        }
    }

    Arrays.stream( array ).forEach( x -> System.out.println( Arrays.toString( x ) ) );
}

Also with Java 8

public static void main( String[] args ) {

    Scanner scanner = new Scanner( System.in );

    int[][] array = new int[3][3];

    for ( int[] ints : array ) {
        ints = scanner.nextLine().chars().map( Character::getNumericValue ).toArray();
    }

    Arrays.stream( array ).forEach( x -> System.out.println( Arrays.toString( x ) ) );
}
J. Pichardo
  • 3,077
  • 21
  • 37
0

The reason is java scanner gets whole number (e.g:123) as a one number.

for (int i = 0 ; i < N; i++) {
        int p = scan.nextInt();
        for (int j = N-1; j >= 0; j--) {
            array[i][j] = p % 10;
            p = p / 10;
        }
    }
Shehan V
  • 164
  • 1
  • 14