Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
How can a 1d string be converted to a 2d char array? – blackpanther Sep 26 '13 at 13:42
1  
println prints a line break at the end of the line. use print to print without line break – njzk2 Sep 26 '13 at 13:43
    
To add on to @njzk2's comment, use print inside the j loop to print each character in a string in one row, and then immediately following that loop block, use println(); to go to a new line for the next string/char array. – jonhopkins Sep 26 '13 at 13:47
    
It is printing the right way now, but it is only printing out the first row, after that it prints out blank for the rest of the rows. – user2817804 Sep 27 '13 at 15:31

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();
        }
    }
}
share|improve this answer
    
Thanks, you wouldnt happen to know why im getting a "java.lang.NullPointerException" after the first line is printed? – user2817804 Sep 26 '13 at 13:56
    
@user2817804 See my updated answer. – TheKojuEffect Sep 26 '13 at 13:59
    
Thanks again, the exception is gone. Now i just need to figure out why the array only prints out one line and ignores the rest. – user2817804 Sep 26 '13 at 14:03
    
@user2817804 :). Actually, your question is quite incomplete. However, if you feel like you've got your answer, you can close this question. – TheKojuEffect Sep 26 '13 at 14:09
    
Well since you asked lol, any idea why the array is only printing out the first line? Could there be a an issue coming from changing the 1d string to the 2d char? – user2817804 Sep 26 '13 at 14:33

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

share|improve this answer

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).

share|improve this answer

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
share|improve this answer
    
array1 is probably an array of String, here – njzk2 Sep 26 '13 at 13:57
    
@njzk2: ohh! I missed that. Thanks Sir. – Aniket Kulkarni Sep 26 '13 at 14:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.