Forum Stats

  • 3,752,916 Users
  • 2,250,585 Discussions
  • 7,868,049 Comments

Discussions

How to store the contents of string array to 2 dimensional character array

Hi,the following code asks for grid size and then takes the input values from the keyboard and stores contents in "str".Now I want to store contents of "str" in a 2-dimensional character array named char[][] board. How do I do that? Please be patient I am new to coding.

Scanner s = new Scanner(System.in);

System.out.println("Enter grid size");      

int t = s.nextInt(); // read number of elements      

s.nextLine(); // consume new line    

System.out.println("Enter the values");    

int d = s.nextInt(); // read number of elements       

s.nextLine();        

String str[] = new String[t];       

for(int k=0;k<t;k++)     

   {          str[k]=s.nextLine();    

   }

Sample Output:

Enter grid size

3

Enter the values

123

234

456

Tagged: