ArrayList от TXT File ПроблемаJAVA

Программисты JAVA общаются здесь
Anonymous
ArrayList от TXT File Проблема

Сообщение Anonymous »

4 классы: < /p>


[*] - индекс, имя, местоположение и список нескольких существ (доступ к ссылкам на экземпляры класса существ).
[*]

Код: Выделить всё

Creature
- индекс, тип, имя, партия по индексу, значение эмпатии, стоимость страха, список сокровищ, список артефактов
[*]

Код: Выделить всё

Treasure
- индекс, тип, существо от индекса, вес, значение
Artifact< /code> - индекс, тип, существо по индексу, другие поля < /li>
< /ol>

Используйте класс ArrayList, чтобы держать экземпляры вышеуказанного.p : 10003 : Conglomeration

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

t : 30004 : Silver : 20005 : 120 : 1000

a : 40001 : Wand : 20007 : ElderWand
< /code>

Это то, что я написал до сих пор: < /p>

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

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

Я знаю, что у меня уже есть проблемы, потому что, когда я пытаюсь распечатать содержимое массива партии. Это пусто. Я просто не могу выяснить, почему. < /P>

Присутствует ошибки: < /p>

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>

Idea ** Может ли я иметь цикл if-else и «поиск» соответствующего класса, чтобы увидеть, что сканировать (вместо того, чтобы делать все это в цикле)? Например: если первая буква - это «p», перейти к классу партии, чтобы увидеть, что делать с линией.


Подробнее здесь: https://stackoverflow.com/questions/908 ... le-trouble

Вернуться в «JAVA»