I can't seem to find the issue in my code and was wondering if you wonderful people on here would be willing to help me out. My professor is requiring us to create a 2-D array from information pulled from a file she provided us. Using the Scanner and File class we should be able to accomplish this, however, I have hit a speed bump. My scanner is not recognizing the integer after the delimiter I have set for it. Here is the file she provides us with.
5x7
o,2,3
7,1,3
7,1,1
X,4,2
This info is separated by newlines where there are spaces in the blockquote.
Here is my code:
import java.io.*;
import java.util.*;
public class Battlefield {
// Use the FILL_CHAR for a space that contains no creature.
// Use these chars for creatures so that your code will pass
// the tests used for evaluating your program.
public final char FILL_CHAR = '-';
public final char OGRE = 'o';
public final char CENTAUR = '7';
public final char DRAGON = 'X';
private char[][] field;
public Battlefield(String fn) {
try {
// You write code here.
// Read a file and initialize the field.
// The name of the file is passed in from the driver.
// Keep all the file reading stuff in the try/catch block
// to make file exceptions easier to deal with.
File battlefield = new File(fn);
Scanner scan = new Scanner(battlefield);
scan.useDelimiter("x");
int row = scan.nextInt();
System.out.println(row);
System.out.println(scan.next());
System.out.println(scan.hasNextInt());
int column = scan.nextInt();
char[][] field = new char[row][column];
/**
Scanner scan2 = new Scanner(battlefield);
scan2.useDelimiter(",");
/**
field[scan2.nextInt()][scan2.nextInt()] = OGRE;
field[scan2.nextInt()][scan2.nextInt()] = CENTAUR;
field[scan2.nextInt()][scan2.nextInt()] = CENTAUR;
field[scan2.nextInt()][scan2.nextInt()] = DRAGON;
**/
} catch (IOException ex) {
System.err.println(ex.getStackTrace());
}
}
And my main method/driver class:
public class BattlefieldDrv {
public static void main(String[] args)
{
Battlefield battlefieldOne = new Battlefield("1field.dat");
System.out.println(battlefieldOne.toString());
}
}
Here is my stack trace:
> 5
7
o,2,3
7,1,3
7,1,1
X,4,2
false
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Unknown Source)
at java.base/java.util.Scanner.next(Unknown Source)
at java.base/java.util.Scanner.nextInt(Unknown Source)
at java.base/java.util.Scanner.nextInt(Unknown Source)
at Battlefield.<init>(Battlefield.java:38)
at BattlefieldDrv.main(BattlefieldDrv.java:15)
Thank you for any help or insight you have!
So let's step through this code.
scan.useDelimiter("x");
int row = scan.nextInt();
5 is read into row
.
System.out.println(row);
5 is printed.
System.out.println(scan.next());
The rest of the file is read and printed, because that's what comes after the x
.
System.out.println(scan.hasNextInt());
There's nothing left to read, so the NoSuchElementException
is thrown here.
You need to make the scanner also accept newlines as delimiter; you can do that by using
scan.useDelimiter("(x|\\s)");
(The \\s
is the pattern for "any whitespace").
As a side note, it's good practice to use the try-with-resources
-construct:
try (Scanner scan = new Scanner(Paths.get("1field.dat"))) {
scan.useDelimiter(...);
...
} catch (IOException e) {
This will result in your file resource being closed automatically.