Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayList from txt file trouble
    primarykey
    data
    text
    <p>4 Classes: </p> <ol> <li><code>Party</code> - index, a name, a location, and list of a number of creatures (accessed by links to instances of the Creature class).</li> <li><code>Creature</code> - index, a type, a name, party by index, empathy value, fear value, list of treasures, list of artifacts</li> <li><code>Treasure</code> - index, type, creature by index, weight, value</li> <li><code>Artifact</code> - index, type, creature by index, other fields</li> </ol> <p>Use the ArrayList class to hold instances of the classes above.</p> <p>test data (looks something like): </p> <pre><code>p : 10003 : Conglomeration c : 20001 : Woman : Lucy :10001 : 17 : 22 : 20 t : 30004 : Silver : 20005 : 120 : 1000 a : 40001 : Wand : 20007 : ElderWand </code></pre> <p>This is what I've written so far: </p> <pre><code>import java.io.IOException; import java.util.*; import javax.swing.JFileChooser; import java.util.Scanner; public class SorcerersCave { public static void main(String[] args) { ArrayList&lt;Party&gt; partyList = new ArrayList&lt;Party&gt;(); ArrayList&lt;Creature&gt; creatureList = new ArrayList&lt;Creature&gt;(); ArrayList&lt;Treasure&gt; treasureList = new ArrayList&lt;Treasure&gt;(); ArrayList&lt;Artifact&gt; artifactList = new ArrayList&lt;Artifact&gt;(); // open and read file: try { Scanner scanner = new Scanner(chooser.getSelectedFile()) .useDelimiter("\\s*:\\s*"); while (scanner.hasNextLine()) { String line = scanner.nextLine(); int index; String type; String name; char identifier = line.charAt(0); if (identifier == 'p') { index = scanner.nextInt(); name = scanner.next(); partyList.add(new Party(partyInfo(index, name))); } else if (identifier == 'c') { index = scanner.nextInt(); type = scanner.next(); name = scanner.next(); int partyC = scanner.nextInt(); int empathyC = scanner.nextInt(); double carryingCapacityC = scanner.nextDouble(); creatureList.add(new Creature(creatureInfo(index, type, name, partyC, empathyC, carryingCapacityC))); } else if (identifier == 't') { index = scanner.nextInt(); type = scanner.next(); int creatureT = scanner.nextInt(); double weightT = scanner.nextDouble(); int valueT = scanner.nextInt(); treasureList.add(new Treasure(treasureInfo(index, type, creatureT, weightT, valueT))); } else if (identifier == 'a') { index = scanner.nextInt(); type = scanner.next(); int creatureA = scanner.nextInt(); artifactList.add(new Artifact(artifactInfo(index, type, creatureA))); } else { System.out.println("This is not a valid line of input"); } System.out.println("Identifier: " + identifier); } } catch (IOException e) { e.printStackTrace(); } System.out.println("party: " + partyList.toString()); } } private class Creature { public Creature (int index, String type, String name, int partyC, int empathyC, double carryingCapacityC) { return; } } private class Party { public Party(int index, String name) { return; } } private class Artifact { public Artifact(int index, String type, int creatureA) { return; } } private class Treasure { public Treasure(int index, String type, int creatureT, double weightT, int valueT) { return ; } } </code></pre> <p>I know I already have issues because when I try to print the contents of the partyList array. It's empty. I just can't seem to figure out why.</p> <p>Errors present: </p> <pre><code>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 SorcerersCave.main(SorcerersCave.java:43) </code></pre> <p>Idea** Is it possible for me to have the if-else loop and 'look to' corresponding class to see what to scan (instead of doing it all in the loop)? For example: if the first letter is a 'p' go to the party class to see what to do with the line.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload