0

For an assignment I have to read an input which looks like this:

. . . . . . . . . . . . . * .
. . . . . . . . . . . . * . .
. . . . . . . . . . . . * * *
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
* * * * * * * * . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .

and store it in an array which contains Strings. For now I've come up with this, however, I don't know how to arrange the input in such a way that it can be stored as an array.

void readInputField(){
    String inputField; //string in which input is stored
    inputField = sc.nextLine(); //scans the input
    String[][] fieldParts; //array in which I want to store Strings of inputField
    fieldParts = new String[height][width]; //width and height are determined 
    //by earlier scanner input and correspond to the dimensions of the input array

    fieldParts = inputField.split(" "); //error on this line, how to split the input
    //as parts of the array?
}
2
  • What is the exact error? Also, from the looks of it, you may want to use a char[][] instead of a String[][] since Strings typically hold more than one character, but each cell of your array holds only one character (if I understood you correctly) Commented May 20, 2016 at 10:50
  • fieldParts is declared to hold 2D array String[][] but result of split is 1D array String[]. Anyway are you sure that values of height and width will be correct (15 in this case)? Commented May 20, 2016 at 11:26

2 Answers 2

1

Would not be able to tell what the problem is until you post the exact error you are getting. Like the comment above, character array would be ideal if you are just reading simple characters. But for strings, if you are reading the input line by line, then the code would be splitting a single line inside a for loop like this. (Assumes the user clicks on "enter" after providing every input line)

String[][] fieldParts = new String[row][col];
int j = 0;
for(int i=0;i<row;i++)
{
   String inputField = sc.nextLine();
   String[] row = new String[col];
   row = inputField.split(" ");
   fieldParts[j] = row; //the row you just read
   j++;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

fieldParts = inputField.split(" "); well most probable error here is that fieldParts is declared to store String[][] while split returns String[], so since number of dimension doesn't match we can't assign properly returned array to that variable. You solved it by assigning array to specific row with fieldPath[j]=....
Anyway since you are replacing rows already created via new String[row][col] you can improve your code by not creating them at all. Simply use new String[row][] instead (rows will be nulls).
0

You are storing value in fieldParts[][] which is 2d array.
So you need to split input 2 times.

void readInputField(){
            String inputField; //string in which input is stored
            inputField = ". . . . . . . . . . . . . * .\n" +
                    ". . . . . . . . . . . . * . .\n" +
                    ". . . . . . . . . . . . * * *\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    "* * * * * * * * . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .";
            int height = 10;
            int width = 10;
            String[][] fieldParts; //array in which I want to store Strings of inputField
            fieldParts = new String[height][width]; //width and height are determined
            //by earlier scanner input and correspond to the dimensions of the input array
            String val[] = inputField.split("\n");
            for(int i=0;i<height;i++){
                String wd[] = val[i].split(" ");
                for(int j=0;j<width;j++){
                    fieldParts[i][j] = wd[j];
                }
            }
        }

1 Comment

While this is a valid solution, it completely neglects OP's (partial) solution. OP reads the input linewise with a Scanner. Therefore, the first split("\n") is neglectable. Furhtermore, OP asks why his/her attempt of split(" ") is not working (see comment in code). This question it ignored completely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.