user2817804
user2817804

Reputation: 45

Converting 1-D String array to 2-D char array

I'm working on a program that requires me to take in a 1-D String array from a file and turn it into a 2-D array. Taking in the array from the file works fine, but I can't get the second part to work.

The code I'm working with is:

char[][] array2 = new char [7][5];

for (int i = 0; i < array1.length; i++)
{
    array2[i]= array[i].toCharArray();   
}

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 7; j++)
    {
         System.out.println(array2[i][j]);
    }
}

The array is supposed to print in a grid format, but is printing downward.

Any help is appreciated, thanks.

Upvotes: 0

Views: 7509

Answers (4)

TheKojuEffect
TheKojuEffect

Reputation: 21081

Use print instead of println in inner loop and after each loop print a blank line with println.

for (int i = 0; i < 7; i++) // see changes 5/7. You did "new char[7][5]" not [5][7]
{
    for (int j = 0; j < 5; j++) // see changes 7/5
    {
         System.out.print(array2[i][j]);
    }
    System.out.println();
}

Update:

Following is a program that convert String array to 2D char array.

public class StringToChar {
    public static void main(String[] args) {
        String[] strArr = { "HELLO", "WORLD" };
        char[][] char2D = new char[strArr.length][];

        for (int i = 0; i < strArr.length; i++) {
            char2D[i] = strArr[i].toCharArray();
        }

        for (char[] char1D : char2D) {
            for (char c : char1D)
                System.out.print(c + " ");

            System.out.println();
        }
    }
}

Upvotes: 2

Aniket Kulkarni
Aniket Kulkarni

Reputation: 12983

Have a look at the String.toCharArray()

If it is printing downwards then change

  for (int i = 0; i < 7; i++)  //row loop
  {
    for (int j = 0; j < 5; j++) //column loop
     {
        System.out.print(array2[i][j]);
     }
    System.out.println(); //add here 
  }

Have a look at

  1. formatting
  2. print vs println

Upvotes: 0

user902383
user902383

Reputation: 8640

few suggestions,

  1. replace char[][] array2 = new char [7][5]; with char[][] array2 = new char [array1.length][]; (where array1 holds your strings), so your 2d array will have as many rows as you have strings
  2. your loop
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 7; j++)
....
 }

change to

for (int i = 0; i < array2.length; i++)
{ 
   for (int j = 0; j <  array2[i].length; j++)
....

another thing is if you want your string printed in rows, use System.out.print, and whenever you finished inner loop, print out'\n' character

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200148

You are using println, which is why each character is printed on its own line. You must use print instead.

Note that your initialization with

new char [7][5];

doesn't work as you expect because the inner arrays will be overwritten. Use

new char[7][]

for the same result, but more clarity as to your intent. Here

for (int i = 0; i < 5; i++)
   for (int j = 0; j < 7; j++)

you have apparently reversed the order of indices: you are iterating only through 5 outer arrays, but you have allocated 7. What you should do instead is check against the actual array length and not a hardcoded number. The inner array may be of any size, after all (it depends on the string length).

Upvotes: 0

Related Questions