2 Replies - 1372 Views - Last Post: 09 March 2013 - 12:41 PM Rate Topic: -----

#1 roj3og  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 20-November 10

Java game crashes upon data file selection...

Posted 09 March 2013 - 11:57 AM

Java game- has a few errors but i am not sure what to do at this point.

When the game is run the output box reads-partyByIndex: 10001
foo: Unity
partyByIndex: 10001
foo: Unity
partyByIndex: 10003
foo: Conglomeration
partyByIndex: 10002
foo: Assemblage
partyByIndex: 10002
foo: Assemblage
partyByIndex: 10003
foo: Conglomeration
partyByIndex: 10001
foo: Unity
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at Project3.<init>(Project3.java:86)
at GUI.<init>(GUI.java:95)
at GUI.main(GUI.java:597)
Java Result: 1
BUILD SUCCESSFUL (total time: 13 seconds)

the sample data is-
// sample data file
// Parties format:
// p:<index>:<name>
p : 10001 : Unity
p : 10002 : Assemblage
p : 10003 : Conglomeration
// Creatures format:
// c:<index>:<type>:<name>:<party>:<empathy>:<fear>:<carrying capacity>
c : 20001 : Woman : Lucy :10001 : 17 : 22 : 20
c : 20002 : Woman : Jane :10001 : 22 : 15 : 25
c : 20003 : Woman : Nancy :10003 : 80 : 44 : 20
c : 20004 : Man : James :10002 : 35 : 13 : 50
c : 20005 : Troll : Fred :10002 : 03 : 05 : 150
c : 20006 : Warlock : Harry :10003 : 05 : 06 : 0
c : 20007 : Witch : Morgana:10001 : 10 : 15 : 0
// Treasures format:
// t:<index>:<type>:<creature>:<weight>:<value>
// creature = 0 means noone is carrying that treasure at the moment
t : 30001 : Gold : 20004 : 50 : 2000
t : 30002 : Gold : 0 : 75 : 5000
t : 30003 : Gems : 20002 : 10 : 10000
t : 30004 : Silver : 20005 : 120 : 1000
// Artifacts format:
// a:<index>:<type>:<creature>[:<name>]
a : 40001 : Wand : 20007 : ElderWand
a : 40002 : Wand : 20006
a : 40003 : Potion : 20007



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.DefaultCaret;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

/**
 * CMSC 335 Project #3
 */

/**
 * @author Roger Joudrey
 */

public final class GUI extends JFrame {
    private static final long serialVersionUID = 1L;

    
    public static JPanel getJpJobs() {
        return jpJobs;
    }

   
    public static void setJpJobs(JPanel aJpJobs) {
        jpJobs = aJpJobs;
    }
    private JTextField jtfmsg = new JTextField(10);
    private JRadioButton jrbChar, jrbParty, jrbLoot, jrbArt;
    private JButton jbtFile, jbtFear, jbtEmp, jbtCarrCap, jbtTrWeight, jbtTrVal;
    private JTextArea jpTxtArea; 
    private JScrollPane jpScroll;
    private DefaultCaret caret;
    private static JPanel jpJobs = new JPanel();
    private JTree jTree;

public void updateTextArea(final String text) {
  SwingUtilities.invokeLater(new Runnable() {
      @Override
    public void run() {
                getJpTxtArea().append(text);
                getJpTxtArea().setTabSize(7);
                getJpScroll().setPreferredSize(new Dimension(200,600));
    }
  });
}
 
public void redir() {
    OutputStream out;
        out = new OutputStream() {
@Override
public void write(int B)/>/> throws IOException {
   updateTextArea(String.valueOf((char) B)/>/>);
}

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
   updateTextArea(new String(b, off, len));
}

            @Override
            public void write(byte[] B)/>/> throws IOException {
   write(b, 0, b.length);
}
};
 
  System.setOut(new PrintStream(out, true));
  System.setErr(new PrintStream(out, true));
  
} 



public GUI() throws FileNotFoundException {
        this.jpScroll = new JScrollPane(getJpTxtArea());
       
        this.jpTxtArea = new JTextArea();

    
    
    final Project3 cave = new Project3();
    
   
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Sorcerer's Cave");
        jTree = new JTree(top);
        createNodes(top, cave);
        jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        for (int i = 0; i < jTree.getRowCount(); i++) {
        jTree.expandRow(i);
    }
        
        JPanel jpFile = new JPanel();
            jpFile.setLayout(new BorderLayout(5, 0));
            jpFile.add(new JLabel("Choose a source file: "), BorderLayout.WEST);
            jpFile.add(jbtFile = new JButton("Open"), BorderLayout.EAST);
         
        JPanel jpSort1 = new JPanel();
            jpSort1.setLayout(new FlowLayout());
            jpSort1.add(new JLabel("Sort Creatures: "));
            jpSort1.add(jbtFear = new JButton("Fear"));
            jpSort1.add(jbtEmp = new JButton("Empathy"));
            jpSort1.add(jbtCarrCap = new JButton("Carrying Capacity"));
            
        JPanel jpSort2 = new JPanel();
            jpSort2.setLayout(new FlowLayout());
            jpSort2.add(new JLabel("Sort Treasure: "));
            jpSort2.add(jbtTrWeight = new JButton("Weight"));
            jpSort2.add(jbtTrVal = new JButton("Value"));
        
        JPanel jpText = new JPanel();
            jpText.setLayout(new BorderLayout());
            jpText.add(new JLabel("Enter name: "), BorderLayout.WEST);
            jpText.add(jtfmsg, BorderLayout.CENTER);
            jtfmsg.setHorizontalAlignment(JTextField.RIGHT);
         
        JPanel jpRad = new JPanel();
            jpRad.setLayout(new FlowLayout(5));
            jpRad.add(new JLabel("Search by: "));
            jpRad.add(jrbChar = new JRadioButton("Character"));
            jpRad.add(jrbParty = new JRadioButton("Party"));
            jpRad.add(jrbLoot = new JRadioButton("Treasure"));
            jpRad.add(jrbArt = new JRadioButton("Artifact"));
            jrbChar.setSelected(true);
            ButtonGroup group = new ButtonGroup(); 
                group.add(jrbChar);
                group.add(jrbParty);
                group.add(jrbLoot);
                group.add(jrbArt);
        
        JButton jbtCave;
        JPanel jpBtns = new JPanel();
            jpBtns.setLayout(new GridLayout(1, 50));
            jpBtns.add(new JLabel("Return the elements in the cave:"));
            jpBtns.add(jbtCave = new JButton("Enter... "), BorderLayout.EAST);
        
        JPanel jpSearch = new JPanel();
            jpSearch.setLayout(new GridLayout(1, 2));
                jpSearch.add(jpRad);
                jpSearch.add(jpText);
        
        JPanel jpSort = new JPanel();
            jpSort.setLayout(new GridLayout(1, 2));
            jpSort.add(jpSort1);
            jpSort.add(jpSort2);
         
        JPanel jpTop = new JPanel();
        Border searchBorder = BorderFactory.createTitledBorder("Search and Sort");
            jpTop.setLayout(new GridLayout(2, 1));
                jpTop.setBorder(searchBorder);
                jpTop.add(jpSearch);
                jpTop.add(jpSort);
        
        jpJobs.setLayout(new GridLayout(9,1));
         
        JPanel jpMaster = new JPanel();
        Border mainBorder = BorderFactory.createCompoundBorder();
            jpMaster.setBorder(mainBorder);
            jpMaster.setLayout(new BorderLayout(5,25));
            jpMaster.add(jpTop, BorderLayout.NORTH);
            jpMaster.add(jpScroll, BorderLayout.CENTER);
            jpMaster.add(jpBtns, BorderLayout.SOUTH);
            jpMaster.add(jpJobs, BorderLayout.EAST);
            jpMaster.add(new JScrollPane(jTree), BorderLayout.WEST);
            
            caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);//Update scrolling text
        setLayout(new FlowLayout());
            add(jpMaster);

        jbtFile.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Project3.openFile();
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(TextField.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        jrbChar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        
        jrbParty.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        
        jrbLoot.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        
        jrbArt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
         
        jtfmsg.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                if (jrbChar.isSelected() == true){
                    Iterator<Party> partyIterator = cave.getPartyList().iterator();
                    while (partyIterator.hasNext()){
                        Party p = partyIterator.next();
                        Iterator<Creature> creatureIterator = p.getCreatureList().iterator();
                    while (creatureIterator.hasNext()){
                        Creature c = creatureIterator.next();
                    if (c.getName().equals(jtfmsg.getText())){
                        System.out.println("Found: \n" + c.toString() + "\n\t member of " +
                        p.toString() + "\n carrying: \n"  + c.getTreasureList().toString() + c.getArtifactList());
                    break;
                        }
                    }
                    }
                }                
                
                
                else if (jrbParty.isSelected() == true){
                Iterator<Party> partyIterator = cave.getPartyList().iterator();
                    while (partyIterator.hasNext()){
                        Party p = partyIterator.next();
                    if (p.getName().equals(jtfmsg.getText())){
                        System.out.println("Found: \n" + p.toString() + 
                        ", with members: \n" + p.getCreatureList().toString());
                    break;
                        }
                    }
                }         
                
                
                else if (jrbLoot.isSelected() == true){
                    Iterator<Party> partyIterator = cave.getPartyList().iterator();
                    while (partyIterator.hasNext()){
                        Party p = partyIterator.next();
                        Iterator<Creature> creatureIterator = p.getCreatureList().iterator();
                    while (creatureIterator.hasNext()){
                        Creature c = creatureIterator.next();
                        Iterator<Treasure> treasureIterator = c.getTreasureList().iterator();
                    while (treasureIterator.hasNext()){
                        Treasure t = treasureIterator.next();
                    if (t.getType().equals(jtfmsg.getText())){
                        System.out.println("Found: \n" + t.toString() + ", carried by " +
                        c.toString() + ", member of \n" + p.toString());
                    break;
                        }
                       }
                      }
                    }
                }
                
                
                      Iterator<Party> partyIterator = cave.getPartyList().iterator();
                      while (partyIterator.hasNext()){
                          Party p = partyIterator.next();
                          Iterator<Creature> creatureIterator = p.getCreatureList().iterator();
                      while (creatureIterator.hasNext()){
                          Creature c = creatureIterator.next();
                          Iterator<Treasure> treasureIterator = c.getTreasureList().iterator();
                      while (treasureIterator.hasNext()){
                          Treasure t = treasureIterator.next();
                      }
                          Iterator<Artifact> artifactIterator = c.getArtifactList().iterator();
                      while (artifactIterator.hasNext()){
                          Artifact a = artifactIterator.next();
                      if (a.getType().equals(jtfmsg.getText())){
                          System.out.println("Found: \n" + a.toString() + ", carried by " +
                          c.toString() + ", member of " + p.toString());
                      break;
                          }
                         }
                        }
                       }        
                      }
            });
        
        jbtCave.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Iterator<Party> partyIterator = cave.getPartyList().iterator();
                System.out.println("----------");
                while (partyIterator.hasNext()){
                    Party p = partyIterator.next();
                    System.out.println("Party " + p.toString() + " has members: ");
                    Iterator<Creature> creatureIterator = p.getCreatureList().iterator();
                while (creatureIterator.hasNext()){
                    Creature c = creatureIterator.next();
                    System.out.println("     " + c.toString());
                    Iterator<Treasure> treasureIterator = c.getTreasureList().iterator();
                while (treasureIterator.hasNext()){
                    Treasure t = treasureIterator.next();
                    System.out.println("          " +  
                    "     carrying:     " + t.toString());
                }
                    Iterator<Artifact> artifactIterator = c.getArtifactList().iterator();
                while (artifactIterator.hasNext()){
                    Artifact a = artifactIterator.next();
                    System.out.println("          "  
                    + "     carrying:    "  + a.toString());
                }
                        }
                System.out.println("----------");
                }
                for (Treasure t: cave.getUnfoundTreasure()) {
                    System.out.println(t + " awaits to be found");
                }
            }
        });
        
        jbtFear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Collections.sort(cave.getAllCreatures(), new SortFear());
                System.out.println("----------");
                System.out.println("Sorted by fear: ");
                for (Creature c: cave.getAllCreatures()) {
                    System.out.println(c);
                }
            }
        });

        jbtEmp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Collections.sort(cave.getAllCreatures(), new SortEmpathy());
                System.out.println("-----------");
                System.out.println("Sorted by empathy: ");
                for (Creature c: cave.getAllCreatures()) {
                    System.out.println(c);
                }
            }
        });
        
        jbtCarrCap.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            Collections.sort(cave.getAllCreatures(), new SortCarrCap());
                System.out.println("-----------");
                System.out.println("Sorted by carrying capacity: ");
                for (Creature c: cave.getAllCreatures()) {
                    System.out.println(c);
                }
            }
        });    
    
        jbtTrWeight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            Collections.sort(cave.getAllTreasures(), new SortWeight());
                System.out.println("----------");
                System.out.println("Sorted by weight: ");
                for (Treasure t: cave.getAllTreasures()) {
                    System.out.println(t);
                }    
            }
        }); 
        
        jbtTrVal.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Collections.sort(cave.getAllTreasures(), new SortValue());
                System.out.println("----------");
                System.out.println("Sorted by value: ");
                for (Treasure t: cave.getAllTreasures()) {
                    System.out.println(t);
                }    
            }
        });
        
        
        jtfmsg.requestFocusInWindow();
       
    }

public static JPanel getPanel(){
    return getJpJobs();
}

private void createNodes(DefaultMutableTreeNode top, 
    Project3 cave) {
        DefaultMutableTreeNode party = null;
        DefaultMutableTreeNode creature = null;
        DefaultMutableTreeNode treasure = null;
        DefaultMutableTreeNode artifact = null;
        for (Party p: cave.getPartyList()){
            top.add(party = new DefaultMutableTreeNode("Party -" + p.toString()));
            for (Creature c: p.getCreatureList()){
                party.add(creature = new DefaultMutableTreeNode(c.getName() + " - " + c.getType()));
                for (Treasure t: c.getTreasureList()){
                    creature.add(treasure = new DefaultMutableTreeNode(t.getType() + " - " + t.getValue()));
                }
                for (Artifact a: c.getArtifactList()){
                    creature.add(artifact= new DefaultMutableTreeNode(a.getName() + " - " + a.getType()));
                }
            }
        }
        DefaultMutableTreeNode unfound = new DefaultMutableTreeNode("Unfound");
        top.add(unfound);
            for (Treasure t: cave.getUnfoundTreasure()){
                unfound.add(treasure = new DefaultMutableTreeNode(t.getType() + " - " + t.getValue()));
            }
    }

    public JTextArea getJpTxtArea() {
        return jpTxtArea;
    }

    public void setJpTxtArea(JTextArea jpTxtArea) {
        this.jpTxtArea = jpTxtArea;
    }

    public JScrollPane getJpScroll() {
        return jpScroll;
    }

    public void setJpScroll(JScrollPane jpScroll) {
        this.jpScroll = jpScroll;
    }

    public DefaultCaret getCaret() {
        return caret;
    }
   
    public void setCaret(DefaultCaret caret) {
        this.caret = caret;
    }

class Jobs extends JPanel implements Runnable, ActionListener{
    private static final long serialVersionUID = 1L;
    
    private boolean goFlag;

    private JProgressBar progressBar;
    private String jobName;
    private long jobTime;
    private HashMap <Integer, SorcerersCave> caveMap;
    private long delay;
    private Creature creature;
    
    public Jobs(Creature c){
        this.creature = null;
        this.delay = 200L;
        this.progressBar = new JProgressBar ();
        this.goFlag = false;
        this.creature = c;
        run();
    }
    
    
    
    @Override
     public void actionPerformed (ActionEvent e) {
         if (isGoFlag()) {
            setGoFlag(false);
            
         } 
         else {
            setGoFlag(true);
            synchronized (this) {
               notify ();}
         }
    }

    @Override
    public final void run() {
        
        getProgressBar().setString(getCreature().getJobName());
        getProgressBar().setStringPainted(true); 
        
        JLabel crName = new JLabel(getCreature().getName());
        JButton start = new JButton("Start/Stop");
        
        JPanel crJobs = new JPanel();
            crJobs.setLayout(new FlowLayout());
        crJobs.add(crName);
        crJobs.add(getProgressBar());
        crJobs.add(start);
        
        
        start.addActionListener(this);
        
        JPanel panel = GUI.getPanel();
        panel.add(crJobs);
        
               
        int progress = 0;
        while (progress != 100) {
            try {
                progress++;
                getProgressBar().setValue(progress);
                Thread.sleep(getDelay());
                synchronized(this){
                
                    while (!isGoFlag()){
                        wait();
                    }
                }
            } catch (InterruptedException e) {}
        }


    }

   
    public boolean isGoFlag() {
        return goFlag;
    }

   
    public void setGoFlag(boolean goFlag) {
        this.goFlag = goFlag;
    }

    
    public JProgressBar getProgressBar() {
        return progressBar;
    }

   
    public void setProgressBar(JProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    
    public String getJobName() {
        return jobName;
    }

   
    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

   
    public long getJobTime() {
        return jobTime;
    }

   
    public void setJobTime(long jobTime) {
        this.jobTime = jobTime;
    }

   
    public HashMap <Integer, SorcerersCave> getCaveMap() {
        return caveMap;
    }

    
    public void setCaveMap(HashMap <Integer, SorcerersCave> caveMap) {
        this.caveMap = caveMap;
    }

    
    public long getDelay() {
        return delay;
    }

    
    public void setDelay(long delay) {
        this.delay = delay;
    }

   
    public Creature getCreature() {
        return creature;
    }

   
    public void setCreature(Creature creature) {
        this.creature = creature;
    }
}
    
public static void main(String[] args) throws FileNotFoundException {
        GUI frame = new GUI();
            frame.setTitle("Sorcerer's Cave");
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        
        
  
    }
}


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import javax.swing.JFileChooser;

/**
 * CMSC 335 Project #3
 */

/**
 * @author Roger Joudrey
 */

public final class Project3 {

    private ArrayList<Party> partyList;
    private ArrayList<Treasure> unfoundTreasure;
    private ArrayList<Artifact> unfoundArtifact;
    private ArrayList<Creature> allCreatures;
    private ArrayList<Treasure> allTreasures;
    private ArrayList<Thread> jobList;
    private HashMap <Integer, SorcerersCave> caveMap;

    public Project3() throws FileNotFoundException {
        this.caveMap = new HashMap<>();
        this.jobList = new ArrayList<>();
        this.allTreasures = new ArrayList<>();
        this.allCreatures = new ArrayList<>();
        this.unfoundArtifact = new ArrayList<>();
        this.unfoundTreasure = new ArrayList<>();
        this.partyList = new ArrayList<>();

        
        Scanner input = new Scanner(openFile());
        
        
        while (input.hasNextLine()) {
            String line = input.nextLine().replaceAll(" ", "");

            if (!line.isEmpty()) {

                Scanner output = new Scanner(line).useDelimiter(":");

                String check = output.next();
                switch (check) {
                    case "p":
                        
                        Party p = new Party(output.nextInt(), output.next(), getCaveMap(), getAllCreatures());
                        partyList.add(p);
                        break;
                    case "c":
                        {
                            int index = output.nextInt();
                            String type = output.next();
                            String name = output.next();
                            int partyIndex = output.nextInt();
                            int empathy = output.nextInt();
                            int fear = output.nextInt();
                            double carryingCap = output.nextDouble();
                            Creature c = new Creature(index, type, name, partyIndex,
                                     empathy, fear, carryingCap, getPartyList(), getCaveMap(), getAllCreatures());
                            allCreatures.add(c);
                            break;
                        }
                    case "t":
                        {
                            int index = output.nextInt();
                            String type = output.next();
                            int creatureIndex = output.nextInt();
                            double weight = output.nextDouble();
                            int value = output.nextInt();
                            Treasure t = new Treasure(index, type, creatureIndex, weight, 
                                    value, getPartyList(), getUnfoundTreasure(), getCaveMap(), getAllTreasures());
                            allTreasures.add(t);
                            break;
                        }
                    case "a":
                        {
                            int index = output.nextInt();
                            String type = output.next();
                            int creatureIndex = output.nextInt();
                            String name = output.next();
                            Artifact a = new Artifact(index, type, creatureIndex, name, getPartyList(), getUnfoundArtifact(), getCaveMap());
                            unfoundArtifact.add(a);
                            break;
                        }
                    case "j":
                        {
                            int index = output.nextInt();
                            String jobName = output.next();
                            int target = output.nextInt();
                            long time = output.nextLong();
                            Creature c = (Creature)(caveMap.get(target));
                            Thread j = new Thread(c);
                            jobList.add(j);
                            c.setJobName(jobName);
                            j.start();
                            break;
                        }
                }

            }

        }

    }

    
    
    
    public static File openFile() throws FileNotFoundException {
        JFileChooser fileChooser = new JFileChooser(".");
        File fileChoice = null;
        if (fileChooser.showOpenDialog(null)
                == JFileChooser.APPROVE_OPTION) {
            fileChoice = fileChooser.getSelectedFile();
            try (Scanner input = new Scanner(fileChoice)) {
                while (input.hasNext()) {
                    System.out.println();
                    input.nextLine();
                }
            }
        } else {
            System.out.println("No file Selected");
        }
        return fileChoice;
    }
    
    public void addTreasure(Treasure t){
        getUnfoundTreasure().add(t);
    }
    
    public void addArtifact(Artifact a){
        getUnfoundArtifact().add(a);
    }
    
    public ArrayList<Party> getPartyList() {
        return partyList;
    }

    
    public void setPartyList(ArrayList<Party> partyList) {
        this.partyList = partyList;
    }

    
    public ArrayList<Treasure> getUnfoundTreasure() {
        return unfoundTreasure;
    }

    
    public void setUnfoundTreasure(ArrayList<Treasure> unfoundTreasure) {
        this.unfoundTreasure = unfoundTreasure;
    }

   
    public ArrayList<Artifact> getUnfoundArtifact() {
        return unfoundArtifact;
    }

   
    public void setUnfoundArtifact(ArrayList<Artifact> unfoundArtifact) {
        this.unfoundArtifact = unfoundArtifact;
    }

    
    public ArrayList<Creature> getAllCreatures() {
        return allCreatures;
    }

    
    public void setAllCreatures(ArrayList<Creature> allCreatures) {
        this.allCreatures = allCreatures;
    }

   
    public ArrayList<Treasure> getAllTreasures() {
        return allTreasures;
    }

    
    public void setAllTreasures(ArrayList<Treasure> allTreasures) {
        this.allTreasures = allTreasures;
    }

   
    public ArrayList<Thread> getJobList() {
        return jobList;
    }

   
    public void setJobList(ArrayList<Thread> jobList) {
        this.jobList = jobList;
    }

    
    public HashMap <Integer, SorcerersCave> getCaveMap() {
        return caveMap;
    }

    
    public void setCaveMap(HashMap <Integer, SorcerersCave> caveMap) {
        this.caveMap = caveMap;
    }
    
}



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import javax.swing.JProgressBar;

/**
 * CMSC 335 Project #3
 */

/**
 * @author Roger Joudrey
 */

public class SorcerersCave {
   
}
class Party extends SorcerersCave{
    private int index;
    private String name;
    private ArrayList<Creature> creatureList;
    
    
    public Party(){
        this.creatureList = new ArrayList<>();
    }
    
    public Party(int index, String name, HashMap<Integer, SorcerersCave> caveMap, 
            ArrayList<Creature> allCreatures){
        this.creatureList = new ArrayList<>();
        this.index = index;
        this.name = name;
        caveMap.put(index, this);
    }
       
    
    @Override
    public String toString(){
        return name;
    }
    
    public int getIndex(){
        return this.index;
    }
    
    public String getName(){
        return this.name;
    }
    
    public void addCreature(Creature c){
        getCreatureList().add(c);
    }

    
    public ArrayList<Creature> getCreatureList() {
        return creatureList;
    }

    
    public void setCreatureList(ArrayList<Creature> creatureList) {
        this.creatureList = creatureList;
    }
    
}

class Creature extends SorcerersCave implements Runnable, ActionListener{
    private int index;
    private String type;
    private String name;
    private int partyByIndex;
    private int empathy;
    private int fear;
    private double carryingCapacity;
    private ArrayList<Treasure> treasureList;
    private ArrayList<Artifact> artifactList;
    private boolean goFlag = false;

    private JProgressBar progressBar;
    private String jobName;
    private long jobTime;
   
    
    
    public Creature(int index, String type, String name, int partyByIndex, int empathy,
            int fear, double carryingCapacity, ArrayList<Party> partyList, 
            HashMap<Integer, SorcerersCave> caveMap, ArrayList<Creature> allCreatures){
        this.progressBar = new JProgressBar ();
        this.artifactList = new ArrayList<>();
        this.treasureList = new ArrayList<>();
        this.index = index;
        this.type = type;
        this.name = name;
        this.partyByIndex = partyByIndex;
        this.empathy = empathy;
        this.fear = fear;
        this.carryingCapacity = carryingCapacity;
        caveMap.put(index, this);
        
        System.out.println("partyByIndex: " + partyByIndex);
        SorcerersCave foo = caveMap.get(partyByIndex);
        System.out.println("foo: " + foo); 
        
        ((Party)caveMap.get(this.partyByIndex)).addCreature(this);
        allCreatures.add(this);
    }
    
    
    @Override
    public String toString(){
        return index + "\t" + name + "\t" + type + "\t" + empathy + "\t" + fear + "\t"  
                + carryingCapacity;
    }
    
    
    public int getIndex(){
        return this.index;
    }
    
    
    public String getType(){
        return this.type;
    }
    
    
    public String getName(){
        return this.name;
    }
    
    
    public int getPartyByIndex(){
        return this.partyByIndex;
    }
    
    
    public int getEmpathy(){
        return this.empathy;
    }
    
    
    public int getFear(){
        return this.fear;
    }
    
    
    public double getCarryingCap(){
        return this.carryingCapacity;
    }
    
    public void addTreasure(Treasure t){
        getTreasureList().add(t);
    }
    
    public void addArtifact(Artifact a){
        getArtifactList().add(a);
    }
    
    public void addJob(long time){
        setJobTime(time);
    }
    
    public void setJobName(String jobName){ 
        this.jobName = jobName; }
    
    @Override
    public void run() {
        Jobs jobs = new Jobs(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    
    public boolean isGoFlag() {
        return goFlag;
    }

    
    public void setGoFlag(boolean goFlag) {
        this.goFlag = goFlag;
    }

    
    public ArrayList<Treasure> getTreasureList() {
        return treasureList;
    }

    
    public void setTreasureList(ArrayList<Treasure> treasureList) {
        this.treasureList = treasureList;
    }

    
    public ArrayList<Artifact> getArtifactList() {
        return artifactList;
    }

    
    public void setArtifactList(ArrayList<Artifact> artifactList) {
        this.artifactList = artifactList;
    }

   
    public JProgressBar getProgressBar() {
        return progressBar;
    }

    
    public void setProgressBar(JProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    
    public String getJobName() {
        return jobName;
    }

   
    public long getJobTime() {
        return jobTime;
    }

    
    public void setJobTime(long jobTime) {
        this.jobTime = jobTime;
    }

    private static class Jobs {

        public Jobs() {
        }

        private Jobs(Creature aThis) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }
  
}

class Treasure extends SorcerersCave{
    private int index;
    private String type;
    private int creature;
    private double weight;
    private int value;
    
    
    public Treasure(int index, String type, int creature, double weight, int value,
            ArrayList<Party> partyList, ArrayList<Treasure> unfoundTreasure, 
            HashMap<Integer, SorcerersCave> caveMap, ArrayList<Treasure> allTreasures){
        this.index = index;
        this.type = type;
        this.creature = creature;
        this.weight = weight;
        this.value = value;
        allTreasures.add(this);
        caveMap.put(index, this);
        if (this.creature == 0){
            unfoundTreasure.add(this);
        }
        else{
           
            ((Creature)caveMap.get(this.creature)).addTreasure(this);
           
        }
        
    }
    
    
    @Override
    public String toString(){
        return index + "\t" + type + "\t" + weight + "\t" + value;
    }
    
    public int getIndex(){
        return this.index;
    }
    
   
    public String getType(){
        return this.type;
    }
    
   
    public int getCarryingCreature(){
        return this.creature;
    }
    
   
    public double getWeight(){
        return this.weight;
    }
    
   
    public int getValue(){
        return this.value;
    }
    
}

class Artifact extends SorcerersCave{
    private int index;
    private String type;
    private int creature;
    private String name;
    
    
    public Artifact(int index, String type, int creature, String name,
            ArrayList<Party> partyList, ArrayList<Artifact> unfoundArtifact, 
            HashMap<Integer, SorcerersCave> caveMap){
        this.index = index;
        this.type = type;
        this.creature = creature;
        this.name = name;
        caveMap.put(index, this);
        if (this.creature == 0){
            unfoundArtifact.add(this);
        }
        else {
            ((Creature)caveMap.get(this.creature)).addArtifact(this);        }
        }
    
   
    @Override
    public String toString(){
        return index + "\t" + type;
    }
    
   
    public int getIndex(){
        return this.index;
    }
    
    
    public String getType(){
        return this.type;
    }
    
   
    public String getName(){
        return this.name;
    }
    
    
    public int getCreatureIndex(){
        return this.creature;
    }

}
    
class SortValue implements Comparator<Treasure> {

    @Override
    public int compare(Treasure o1, Treasure o2) {
        int value1 = o1.getValue();
        int value2 = o2.getValue();
        if (value1 < value2) {
            return -1;
        } else if (value1 == value2) {
            return 0;
        } else {
            return 1;
        }
    }
    
}

class SortWeight implements Comparator<Treasure> {
       
    @Override
    public int compare(Treasure o1, Treasure o2) {
        double weight1 = o1.getWeight();
        double weight2 = o2.getWeight();
        if (weight1 < weight2) {
            return -1;
        } else if (weight1 == weight2) {
            return 0;
        } else {
            return 1;
        }
    }
    
}

class SortCarrCap implements Comparator<Creature> {

    @Override
    public int compare(Creature o1, Creature o2) {
        double carrCap1 = o1.getCarryingCap();
        double carrCap2 = o2.getCarryingCap();
        if (carrCap1 < carrCap2) {
            return -1;
        } else if (carrCap1 == carrCap2) {
            return 0;
        } else {
            return 1;
        }
    }
    
}

class SortEmpathy implements Comparator<Creature> {

    @Override
    public int compare(Creature o1, Creature o2) {
        int emp1 = o1.getEmpathy();
        int emp2 = o2.getEmpathy();
        if (emp1 < emp2) {
            return -1;
        } else if (emp1 == emp2) {
            return 0;
        } else {
            return 1;
        }
    }
    
}

class SortFear implements Comparator<Creature> {

    @Override
    public int compare(Creature o1, Creature o2) {
        int fear1 = o1.getFear();
        int fear2 = o2.getFear();
        if (fear1 < fear2) {
            return -1;
        } else if (fear1 == fear2) {
            return 0;
        } else {
            return 1;
        }
    }
    
}





Is This A Good Question/Topic? 0
  • +

Replies To: Java game crashes upon data file selection...

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4364
  • View blog
  • Posts: 12,182
  • Joined: 18-April 07

Re: Java game crashes upon data file selection...

Posted 09 March 2013 - 12:30 PM

Well one thing I notice is down in your artifacts list you have some items without a name. According to your code they have to have a name because you read it in...

case "a":
{
    int index = output.nextInt();
    String type = output.next();
    int creatureIndex = output.nextInt();
    String name = output.next(); //<--- You read in name, so it has to be there.
    Artifact a = new Artifact(index, type, creatureIndex, name, getPartyList(), getUnfoundArtifact(), getCaveMap());
    unfoundArtifact.add(a);
    break;
}



I thought maybe it had to do with treasures but you could be running through treasures just fine since there is no output that I see when they are created. So I think it is hitting your artifacts and when it hits those items after "ElderWand" it is going to look for a name and not find that element.

:)
Was This Post Helpful? 0
  • +
  • -

#3 roj3og  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 20-November 10

Re: Java game crashes upon data file selection...

Posted 09 March 2013 - 12:41 PM

It does the same with this sample data and they all have names.

// Parties format:
// p:<index>:<name>
p : 10001 : Unity
p : 10002 : Assemblage
p : 10003 : Conglomeration
p : 10004 : Partnership
p : 10005 : Company
p : 10006 : Entourage

// Creatures format:
// c:<index>:<type>:<name>:<party>:<empathy>:<fear>:<carrying capacity>:<age>:<height>:<weight>
// index type name party emp fear cap age hgt wght
// yrs cm kg
c : 20001 : Woman : Lucy : 10002 : 89 : 103 : 20 : 50 : 185 : 145
c : 20002 : Woman : Jane : 10002 : 111 : 180 : 25 : 57 : 200 : 183
c : 20003 : Woman : Nancy : 10003 : 172 : 52 : 20 : 91 : 228 : 258
c : 20004 : Woman : Mary : 10006 : 167 : 166 : 20 : 103 : 124 : 233
c : 20005 : Woman : Daphne : 10005 : 60 : 24 : 20 : 101 : 172 : 282
c : 20006 : Woman : Rachelle : 10001 : 3 : 186 : 20 : 106 : 266 : 126
c : 20007 : Woman : Imogene : 10003 : 42 : 1 : 20 : 64 : 132 : 260
c : 20008 : Woman : Gracie : 10003 : 9 : 15 : 20 : 108 : 120 : 59
c : 20101 : Man : James : 10003 : 83 : 199 : 50 : 28 : 126 : 75
c : 20102 : Man : George : 10006 : 157 : 65 : 50 : 97 : 140 : 111
c : 20103 : Man : Thomas : 10004 : 81 : 23 : 50 : 66 : 260 : 129
c : 20104 : Man : Edward : 10006 : 69 : 152 : 50 : 66 : 165 : 180
c : 20105 : Man : Tristan : 10002 : 134 : 45 : 50 : 50 : 207 : 142
c : 20106 : Man : Shelton : 10003 : 17 : 167 : 50 : 15 : 147 : 262
c : 20107 : Man : Errol : 10006 : 140 : 181 : 50 : 80 : 221 : 226
c : 20201 : Troll : Herensuge : 10004 : 134 : 151 : 150 : 65 : 132 : 214
c : 20202 : Troll : Nagendra : 10004 : 139 : 49 : 150 : 68 : 284 : 169
c : 20203 : Troll : Ormr : 10006 : 41 : 89 : 150 : 20 : 146 : 251
c : 20301 : Warlock : Harry : 10005 : 43 : 199 : 0 : 89 : 142 : 182
c : 20302 : Warlock : Alasdair : 10004 : 42 : 179 : 0 : 109 : 265 : 184
c : 20303 : Warlock : Bronmai : 10005 : 144 : 195 : 0 : 55 : 176 : 196
c : 20304 : Warlock : Keredwel : 10006 : 111 : 126 : 0 : 95 : 190 : 262
c : 20401 : Witch : Morgana : 10004 : 173 : 152 : 0 : 70 : 142 : 174
c : 20402 : Witch : Esmeralda : 10006 : 57 : 53 : 0 : 100 : 174 : 243
c : 20403 : Witch : Penelope : 10001 : 8 : 14 : 0 : 111 : 170 : 93
c : 20501 : Dragon : Fafnir : 0 : 188 : 53 : 0 : 42 : 178 : 64
c : 20502 : Dragon : Nidhogg : 0 : 128 : 124 : 0 : 36 : 149 : 111
c : 20601 : Elf : Tabatha : 10006 : 35 : 182 : 0 : 100 : 206 : 134
c : 20602 : Elf : Serena : 10004 : 140 : 174 : 0 : 114 : 174 : 89
c : 20603 : Elf : Rosalinda : 10004 : 26 : 63 : 0 : 78 : 155 : 234
c : 20604 : Elf : Liza : 10006 : 21 : 28 : 0 : 60 : 284 : 130
c : 20605 : Elf : Daren : 10005 : 23 : 52 : 0 : 113 : 242 : 244
c : 20701 : Dwarf : Wilburn : 10005 : 120 : 62 : 0 : 79 : 290 : 265
c : 20702 : Dwarf : Morton : 10005 : 22 : 162 : 0 : 25 : 146 : 184
c : 20703 : Dwarf : Duncan : 10005 : 133 : 48 : 0 : 63 : 287 : 147
c : 20704 : Dwarf : Forest : 10005 : 53 : 4 : 0 : 41 : 242 : 221
c : 20801 : Hobbit : Parker : 10005 : 136 : 74 : 0 : 89 : 261 : 100
c : 20802 : Hobbit : Rodrick : 10002 : 47 : 21 : 0 : 120 : 300 : 54
c : 20803 : Hobbit : Aron : 10006 : 62 : 108 : 0 : 46 : 204 : 107
c : 20804 : Hobbit : Kareem : 10002 : 199 : 161 : 0 : 113 : 134 : 266
c : 20805 : Hobbit : Alden : 10003 : 57 : 46 : 0 : 69 : 221 : 275
c : 20806 : Hobbit : Normand : 10003 : 131 : 195 : 0 : 59 : 215 : 112
c : 20807 : Hobbit : Tanner : 10006 : 86 : 34 : 0 : 120 : 260 : 183
c : 20901 : Dog : Berkeley : 10005 : 25 : 70 : 0 : 26 : 285 : 59
c : 20902 : Dog : Divit : 10005 : 96 : 171 : 0 : 65 : 184 : 268
c : 20903 : Dog : Flitter : 10005 : 27 : 119 : 0 : 35 : 208 : 212
c : 20904 : Dog : Jimbo : 10001 : 96 : 194 : 0 : 52 : 147 : 57
c : 21001 : Wolf : Kodashay : 10003 : 111 : 103 : 0 : 38 : 143 : 196
c : 21002 : Wolf : Raspuntine : 10002 : 59 : 9 : 0 : 29 : 169 : 231
c : 21101 : Enchanter : Mazara : 10004 : 156 : 185 : 0 : 60 : 151 : 217
c : 21102 : Enchanter : Nisien : 10005 : 187 : 97 : 0 : 23 : 187 : 54
c : 21103 : Enchanter : Oschwy : 10001 : 197 : 181 : 0 : 42 : 292 : 86
c : 21201 : Charmer : Peigi : 10002 : 154 : 143 : 0 : 89 : 215 : 186
c : 21202 : Charmer : Ravenclaw : 10006 : 135 : 100 : 0 : 85 : 265 : 189
c : 21301 : Shaman : Sargon : 10002 : 97 : 138 : 0 : 94 : 236 : 233
c : 21302 : Shaman : Serafina : 10003 : 196 : 72 : 0 : 71 : 230 : 181
c : 21303 : Shaman : Sulwen : 10003 : 152 : 108 : 0 : 89 : 179 : 249
c : 21401 : Seer : Tenser : 10001 : 2 : 41 : 0 : 50 : 214 : 246
c : 21501 : Sybil : Fachtna : 10006 : 162 : 160 : 0 : 59 : 256 : 229
c : 21502 : Sybil : Cackletta : 10003 : 54 : 29 : 0 : 53 : 218 : 252
c : 21601 : Clairvoyant: Ciaran : 10006 : 57 : 108 : 0 : 26 : 199 : 200
c : 21602 : Clairvoyant: Dalamar : 10003 : 135 : 7 : 0 : 36 : 171 : 125
c : 21701 : Medium : Melf : 10006 : 180 : 20 : 0 : 57 : 223 : 200
c : 21801 : Wolf : Mordo : 10001 : 86 : 11 : 0 : 65 : 216 : 73

// Treasures format:
// t:<index>:<type>:<creature>:<weight>:<value>
// creature = 0 means noone is carrying that treasure at the moment
t : 30001 : Gold : 20004 : 50 : 2000
t : 30002 : Gold : 0 : 75 : 5000
t : 30003 : Gems : 20002 : 10 : 10000
t : 30004 : Silver : 20005 : 120 : 1000

// Artifacts format:
// a:<index>:<type>:<creature>[:<name>]
a : 40101 : Wand : 20403 : ElderWand
a : 40102 : Wand : 21103 : BlueWand
a : 40103 : Wand : 20401 : CrystalWand
a : 40104 : Wand : 20301 : LeftWand
a : 40105 : Wand : 20303 : DearWand
a : 40106 : Wand : 20304 : CenterWand
a : 40107 : Wand : 21202 : StarWand
a : 40108 : Wand : 20402 : GraceWand
a : 40201 : Potion : 21401 : Healing
a : 40202 : Potion : 21301 : Poison
a : 40203 : Potion : 21302 : Simulation
a : 40204 : Potion : 21303 : Copycat
a : 40205 : Potion : 20603 : Hiding
a : 40206 : Potion : 21102 : Mutation
a : 40301 : Stone : 20802 : Rainbow
a : 40302 : Stone : 20804 : Granite
a : 40303 : Stone : 20702 : Amethyst
a : 40401 : Scroll : 21502 : Crying
a : 40402 : Scroll : 20601 : Liquid Fire
a : 40403 : Scroll : 21701 : Summon Unicorn
a : 40401 : Sword : 20001 : Excalibur
a : 40502 : Sword : 20105 : Iceflame
a : 40503 : Sword : 20002 : Vorpal
a : 40504 : Sword : 20003 : Vajra
a : 40505 : Sword : 20101 : Kusanagi
a : 40506 : Sword : 20103 : Amoracchius
a : 40507 : Sword : 20703 : Sommerswerd
a : 40508 : Sword : 20801 : Dragnipur
a : 40509 : Sword : 20102 : Callandor
a : 40510 : Sword : 20104 : Blackfyre
a : 40601 : Armour : 20006 : Plate
a : 40602 : Armour : 20002 : Chain
a : 40603 : Armour : 20007 : Lorica
a : 40604 : Armour : 20106 : Tanko
a : 40605 : Armour : 20103 : Scale
a : 40606 : Armour : 20605 : Lamellar
a : 40607 : Armour : 20704 : Laminar
a : 40608 : Armour : 20803 : Brigandine
a : 40609 : Armour : 20807 : Splinted
a : 40610 : Armour : 20203 : Leather

// Jobs for creatures
// measure time in seconds
// j:<index>:<name>:<creature index>:<time>:[:<required artifact type>:<number>]*
// Group 10006 - Keredwel and Aron
j : 50001 : Heal Troll : 20304 : 10 : Wand : 3 : Potion : 0 : Stone : 0 : Scroll : 0 : Sword : 0 : Armour : 0
j : 50002 : Strike Now : 20304 : 20 : Wand : 0 : Potion : 0 : Stone : 0 : Scroll : 0 : Sword : 1 : Armour : 1
j : 50003 : Read Mind : 20803 : 25 : Wand : 2 : Potion : 0 : Stone : 0 : Scroll : 2 : Sword : 1 : Armour : 0
j : 50004 : Will Two : 20803 : 30 : Wand : 1 : Potion : 0 : Stone : 0 : Scroll : 1 : Sword : 1 : Armour : 0
j : 50005 : Zero All : 20803 : 23 : Wand : 0 : Potion : 0 : Stone : 0 : Scroll : 0 : Sword : 2 : Armour : 3
// Group 10005 - Nisien, Daphne
j : 50006 : Find Keys : 21102 : 22 : Wand : 1 : Potion : 1 : Stone : 1 : Scroll : 0 : Sword : 1 : Armour : 1
j : 50007 : Call Home : 21102 : 15 : Wand : 2 : Potion : 0 : Stone : 1 : Scroll : 0 : Sword : 0 : Armour : 0
j : 50008 : Sleep : 21102 : 7 : Wand : 2 : Potion : 1 : Stone : 1 : Scroll : 0 : Sword : 0 : Armour : 1
j : 50009 : Wake : 20005 : 16 : Wand : 0 : Potion : 0 : Stone : 0 : Scroll : 0 : Sword : 0 : Armour : 1
j : 50010 : Fight : 20005 : 27 : Wand : 2 : Potion : 0 : Stone : 0 : Scroll : 0 : Sword : 2 : Armour : 1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1