Home java Fill the two-dimensional array input from the keyboard on java

Fill the two-dimensional array input from the keyboard on java

Author

Date

Category

Good evening!

There is such a task:
Create a two-dimensional array N on M and fill it with an input from the keyboard.
The first thing through the gaps enter n and m, and then, depending on its sizes, we enter what should be in each cell in such a format:

input format:
4 6.
010101
111111
101011
101000.

Get an array of n on M – there was no problems. But how to fill it in this format, as shown above? If I use 2 FOR cycles – then I only read the first character in each row on each iteration of the cycle.

int n;
  int m;
  String [] [] inputdata;
  Scanner SC = NEW Scanner (System.in);
  N = SC.NEXTINT ();
  M = SC.NEXTINT ();
  inputdata = new string [n] [m];
  for (int i = 0; i & lt; inputdata.length; i ++) {
    for (int j = 0; j & lt; inputdata [i] .length; j ++) {
      INPUTDATA [I] [J] = SC.NEXTLINE ();
    }
  }

I will be grateful for the help!


A similar solution for a simple array is here. But he could not apply it for a two-dimensional array.
How to fill an array by elements from the keyboard?


Answer 1, Authority 100%

You have left to cut the resulting string on the characters and write down the cells.

for (int i = 0; i & lt; inputdata.length; i ++) {
  char [] in = sc.nextline (). Tochararray ();
  for (int j = 0; j & lt; inputdata [i] .length; j ++) {
    INPUTDATA [I] [J] = IN [J];
  }
}

Are you sure that you need String [] [] All the same CHAR [] []?

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions