Kenneth
Kenneth

Reputation: 1

Reading from a text file to fill a 2d array (in java)

I have a text file that is like this:

10, 11.
(0, 0).
(3, 10).
(0, 6), (0, 7), (0, 9), 
(1, 1), (1, 2), (1, 4), (1, 7),
(2, 1), (2, 7), (2, 8), (2, 10),
(3, 1), (3, 2), (3, 3), (3, 4), (3, 7),
(4, 0), (4, 6), (4, 9), (4, 10),
(5, 2), (5, 3), (5, 4), (5, 5), (5, 7),
(6, 1), (6, 8), (6, 9),
(7, 1), (7, 2), (7, 3), (7, 6), 
(8, 1), (8, 5), (8, 6), (8, 8), (8, 10),
(9, 3), (9, 7).

Where the first line gives the dimensions of the 2 d array, second line should equal "S", third line equal "F", and all other lines equal points that should be "X", and anything not listed should be "."

I still have to add more to the code, so any unused variables is for that, I'm just trying to get what I have to function first

So far the error I am getting is:

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at MazeOne.main(MazeOne.java:15)

When I try to run similar codes without reading from a text file and I print the array it also gives me weird stuff like this instead of the array:

  [[Ljava.lang.String;@55f96302, [Ljava.lang.String;@3d4eac69, [Ljava.lang.String;@42a57993, [Ljava.lang.String;@75b84c92]

My code so far looks like:

Any advice or help on where I'm going wrong would be greatly appreciated

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
import java.util.Scanner;

public class MazeOne {
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args)
    {
        int HEIGHT, LENGTH;
        //Get file name from keyboard
        File importFileName = new File("MazeOne.txt");
        Scanner inFile = new Scanner("MazeOne.txt");


        int Height = inFile.nextInt();
        int Length = inFile.nextInt();
        HEIGHT = Height-1;
        LENGTH = Length-1;
        String [][] MazeArray = new String[HEIGHT][LENGTH];
        String [][] Start = new String[][]{{".",".","."},{".","S","."},{".",".","."}};
        String [][] Block = new String[][]{{"X","X","X"},{"X","X","X"},{"X","X","X"}};
        String [][] Finish = new String[][]{{".",".","."},{".","F","."},{".",".","."}};
        String [][] Normal = new String[][]{{".",".","."},{".",".","."},{".",".","."}};

        for(int i = 0; i < HEIGHT; i++)
        {
            for(int j = 0; j < LENGTH; j++)
            {
                MazeArray[i][j] = ".";
            }
        }

        if(inFile.hasNextLine())
        {
            int X = inFile.nextInt();
            int Y = inFile.nextInt();
            MazeArray[Y][X] = "S";
        }
        if(inFile.hasNextLine())
        {
            int X = inFile.nextInt();
            int Y = inFile.nextInt();
            MazeArray[Y][X] = "F";
        }

        while(inFile.hasNextLine())
        {
            while(inFile.hasNextInt()){
                int X = inFile.nextInt();
                int Y = inFile.nextInt();
                MazeArray[Y][X] = "X";
            }
        }
        System.out.print(MazeArray);
    }
}

Upvotes: 0

Views: 906

Answers (2)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59978

First

Scanner should take with File :

Scanner inFile = new Scanner(new File("path\\MazeOne.txt"));

Not a String like you do :

Scanner inFile = new Scanner("MazeOne.txt");

You already define your File so you can use :

Scanner inFile = new Scanner(importFileName);

Second

The default delimiter of Scanner is a space so when you try to use :

int Height = inFile.nextInt();
int Length = inFile.nextInt();

This can make a problem, because the delimiter is not correct i suggest to remove the comma between your input to be 10 11
The same problem with the other inputs (0, 0). and ...

I suggest to make your inputs without any special character instead you can use

10 11
0 0
3 10
0 6 0 7 0 9 
1 1 1 2 1 4 1 7
2 1 2 7 2 8 2 10
3 1 3 2 3 3 3 4 3 7
4 0 4 6 4 9 4 10
5 2 5 3 5 4 5 5 5 7
6 1 6 8 6 9
7 1 7 2 7 3 7 6 
8 1 8 5 8 6 8 8 8 10
9 3 9 7

Third

You will get java.lang.ArrayIndexOutOfBoundsException: 10 exception because you have 12 line, and you initialize your array with just 10


Forth

To print your array you need to use loops, or you can use :

Arrays.deepToString(MazeArray)

Note don't use upper letter in the first character of your variables.

Fix this and you will solve your problem

Upvotes: 1

fluffy
fluffy

Reputation: 223

Use this Arrays.toString(arrayName) when you are printing an array out.

Upvotes: 1

Related Questions