2

Good day everyone, for the past couple of days I have been working on converting a 1D string array to a 2D char array. My 1D array works fine(zero issues) but when I convert to the 2D char array it only prints out the first row. Below is my code. Any feedback is appreciated. Thanks!

for(int i = 0; i < array1.length; i++) //prints out array
{  
    System.out.println("1d " + number[i]); //prints the line from the file
}
final int ROWS = 7;
final int COLS = 5;

char[][] 2darray = new char [ROWS][COLS];

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

for (int row = 0; row < ROWS; row++)
{
    for (int col = 0; col < COLS; col++)
    {
         System.out.print(2darray[row][col]);
    }
    System.out.println();
}
7
  • You seem to have 3 arrays. Commented Sep 27, 2013 at 15:12
  • 1
    This will not compile; variable names can't start with a number. Commented Sep 27, 2013 at 15:13
  • And in two instances you have array.length, but this should be array1.length()-1 Also, you need to have row < ROWS-1 and col < COLS-1. Does this compile? Does this execute without error? Commented Sep 27, 2013 at 15:15
  • As of now, it does compile Commented Sep 27, 2013 at 15:18
  • 2
    @user2817804 What's happened with your earlier question? 1DStringTo2DChar didn't help? Commented Sep 27, 2013 at 15:21

2 Answers 2

3

You cannot have variables start with a number in Java. I suggest changing your variables accordingly and trying it out.

char[][] array2 = new char [ROWS][COLS];

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

for (int row = 0; row < ROWS; row++)
{
    for (int col = 0; col < COLS; col++)
    {
         System.out.print(array2[row][col]);
    }
    System.out.println();
}
Sign up to request clarification or add additional context in comments.

8 Comments

This still has errors (not yours, his). array.length should instead be array1.length()-1, row < ROWS should be row < ROWS-1, and same for col < COLS should be col < COLS-1
@nhgrif array1.length() doesn't compile
What is his string array called? I thought his string array was array1?
The problem is that it is only printing the first row, its printing out blank rows after the first
@nhgrif length is the number of indices, so I don't think you need to use length-1, so long as you're using < and not <=
|
1

See Comments

for(int i = 0; i < array1.length; i++) //prints out array
{  
    // Why did you use number ?
    System.out.println("1d " + array1[i]); //prints the line from the file
}

// You don't need these now.
// final int ROWS = 7;
//  final int COLS = 5;


// This will initialize 2darray of size as required according to length of array1
char[][] 2darray = new char [array1.length][];

for (int i = 0; i < array1.length; i++) // What is `array`?
{
    2darray[i]= array1[i].toCharArray();   
}

for (int row = 0; row < 2darray.length; row++) // Use actual size of 2darray row
{
    for (int col = 0; col < 2darray[i].length; col++) // use actual size of 2darray column
    { 
         System.out.print(2darray[row][col]);
    }
    System.out.println();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.