1

I am working on an assignment in which I need to read in sample input from a file and insert it into a 2D array. Here is an example of input:

5 6
1 3 4 B 4 3
0 3 5 0 0 9
0 5 3 5 0 2
4 3 4 0 0 4
0 2 9 S 2 1

The 5 and 6 are the dimensions of the array. The user must be able to input many arrays like this one at once, the program ends when the user inputs -1. This is the code I have so far that doesn't seem to be working as it should(I print out the array to make sure the code works):

public static void main (String[] args){
    Scanner sc = new Scanner(System.in);
    int arHeight = sc.nextInt();
    int arWidth = sc.nextInt();
    sc.useDelimiter(" ");
    String[][] map = new String[arHeight][arWidth];

        for(int i=0; i<arHeight; i++){
            for(int j=0; j<arWidth; j++){
                map[i][j] = sc.nextLine();
            }//end inner for
        }//end outter for 

            for(int i=0; i<arHeight; i++){
            for(int j=0; j<arWidth; j++){
                System.out.print(map[i][j] + " ");
            }//end inner for
        }//end outter for 
    }

The assignment states that I cannot use recursion and that I must use 2D arrays. I have looked at other questions but still can't seem to figure it out. Thanks for the help!!

2
  • Welcome to Stack Overflow! It looks like you are asking for homework help. While we have no issues with that per se, please observe these dos and don'ts, and edit your question accordingly. Commented Jan 25, 2017 at 22:23
  • Thanks, I'll get right on it :) Commented Jan 25, 2017 at 22:27

1 Answer 1

2

you read the whole line i*j times

    for(int i=0; i<arHeight; i++){
        for(int j=0; j<arWidth; j++){
            map[i][j] = sc.nextLine();

also you keep al the data in a string array and I don't know why. Here is the solution:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int arHeight = sc.nextInt();
    int arWidth = sc.nextInt();

    int[][] map = new int[arHeight][arWidth];

    for(int i=0; i<arHeight; i++){
        for(int j=0; j<arWidth; j++){
            map[i][j] = sc.nextInt();
        }//end inner for
    }//end outter for

    for(int i=0; i<arHeight; i++){
        for(int j=0; j<arWidth; j++){
            System.out.print(map[i][j] + " ");
        }//end inner for
    }

}

Then try to use

char[][] map = new char[arHeight][arWidth];

and in the for loop:

if(sc.next().charAt(0) != " ") map[i][j] =sc.next().charAt(0);

This way you should read all the chars which are not " ".

Sign up to request clarification or add additional context in comments.

3 Comments

I had used a String array because there are two characters in the array, B and S. Would an array of chars work best?
I will update the answer. I did not observe that you have chars
Thanks, I appreciate the help! It seems to be working properly now!

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.