javaarraylist

ArrayList from txt file trouble


4 Classes:

  1. Party - index, a name, a location, and list of a number of creatures (accessed by links to instances of the Creature class).
  2. Creature - index, a type, a name, party by index, empathy value, fear value, list of treasures, list of artifacts
  3. Treasure - index, type, creature by index, weight, value
  4. Artifact - index, type, creature by index, other fields

Use the ArrayList class to hold instances of the classes above.

test data (looks something like):

p : 10003 : Conglomeration

c : 20001 : Woman   : Lucy   :10001 : 17 : 22 : 20

t : 30004 : Silver : 20005 : 120 : 1000

a : 40001 : Wand : 20007 : ElderWand

This is what I've written so far:

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<Party> partyList = new ArrayList<Party>();
        ArrayList<Creature> creatureList = new ArrayList<Creature>();
        ArrayList<Treasure> treasureList = new ArrayList<Treasure>();
        ArrayList<Artifact> artifactList = new ArrayList<Artifact>();


            // 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 ;
            }

}

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.

Errors present:

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)

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.


Solution

  • I think your problem is that you scan through all the input in these lines:

               while (scanner.hasNext())
                        System.out.println(scanner.nextLine());
    

    Also your classes seem unusual. Why use static Object?
    Just build conventional classes for each of the types, for example:

    private class Creature{
       // data fields
    
       public Creature(int index, String type, String name, int partyC, int empathyC, double carryingCapacityC){
          // set data field values
       }
       
       // accessors, mutators etc.
    }