Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

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

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to store the contents of string array to 2 dimensional character array

    store contents of "str" in a 2-dimensional character array named char[][] board.
    Can you give example Strings and the desired array contents?


    Also posted here: https://www.dreamincode.net/forums/t...2d-char-array/

    and here: https://coderanch.com/t/706642/java/...ay-dimensional
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Function that will add the contents of a String array
    By parveshraju92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 19th, 2014, 04:20 AM
  2. Create two dimensional array from a one dimensional array
    By Kristenw17 in forum Collections and Generics
    Replies: 1
    Last Post: April 9th, 2013, 07:51 PM
  3. Replies: 1
    Last Post: April 1st, 2013, 04:13 PM
  4. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 09:13 PM

Tags for this Thread