'Not loading ArrayList from txt file
I am attempting to populate an ArrayList from a text file that I am able to populate. I am trying to pull the objects at [0] and [1] and running them through a method. What I'm unsure of is whether or not I am loading the file incorrectly or if I'm just trying to pull the elements incorrectly. The method I am putting arguments into does work when I create the objects individually. Below is my main trying to run the method and my load file. Any help would be appreciated.
Text file contents
Ryu Balance 50 50 40 5 3 3 3 3 3 3 3 3 3 3 3 3 0 0 0
Ken Balance 50 50 40 5 3 3 3 3 3 3 3 3 3 3 3 3 0 0 0
Error
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Roster.loadFromFile(Roster.java:33)
at Main.main(Main.java:11)
Main
public class Main {
public static void main(String[] args){
Roster roster = new Roster();
roster.loadFromFile();
Battle.match(roster.get(0), roster.get(1), 180);
}
}
Import file
import java.util.*;
import java.io.*;
//A class meant for building and populating a roster
public class Roster {
ArrayList<Hero> heroList;
public Roster () {
heroList = new ArrayList<Hero>();
}
public void loadFromFile() {
Scanner inFS = null;
FileInputStream fileByteStream = null;
try{
// open the File and set delimiters
fileByteStream = new FileInputStream("Roster.txt");
inFS = new Scanner(fileByteStream);
inFS.useDelimiter("[,\r\n]+");
inFS.nextLine();
// continue while there is more data to read
while(inFS.hasNext()) {
// read four data elements
String name = inFS.next();
String fightStyle = inFS.next();
int hitPoints = inFS.nextInt();
int stamina = inFS.nextInt();
int mana = inFS.nextInt();
int dodgeBlock = inFS.nextInt();
int[] statBlock = new int[12];
statBlock[0] = inFS.nextInt();
statBlock[1] = inFS.nextInt();
statBlock[2] = inFS.nextInt();
statBlock[3] = inFS.nextInt();
statBlock[4] = inFS.nextInt();
statBlock[5] = inFS.nextInt();
statBlock[6] = inFS.nextInt();
statBlock[7] = inFS.nextInt();
statBlock[8] = inFS.nextInt();
statBlock[9] = inFS.nextInt();
statBlock[10] = inFS.nextInt();
statBlock[11] = inFS.nextInt();
int win = inFS.nextInt();
int draw = inFS.nextInt();
int loss = inFS.nextInt();
Hero hero = new Hero(name, fightStyle, hitPoints, stamina, mana, dodgeBlock, statBlock, win, draw, loss);
//TO DO: instantiate an object of the Hero class
heroList.add(hero);
//TO DO: add object to the collection (heroList)
}
fileByteStream.close();
// error while reading the file
}catch(IOException error1) {
System.out.println("Oops! Error related to: Character.dat" );
}
}
Solution 1:[1]
After taking in what @vsfDawg had said the issue was the delimiter.
I changed the delimiter to "[\s\n]+" to take into account both the space and next line. In addition I ran into a new problem in which it would only read my second line. Therefore I removed the first inFS.nextLine() before the try actually begins.
Thank you all for the help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anthony Langley |
