Skip to Main Content

Java User Groups

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

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

Comments

Post Details

Added on Feb 22 2019
0 comments
130 views