4 클래스 :ArrayList를
Party
- 인덱스, 이름, 위치, 생물의 숫자의 목록 (생물 클래스의 인스턴스에 대한 링크 액세스).Creature
- 인덱스, 유형, 이름, 인덱스 자, 공감 값, 공포의 가치, 보물의 목록, 유물의 목록Treasure
- 인덱스, 유형, 인덱스, 중량 생물, 값Artifact
- 인덱스, 유형별, 생물체 별, 인덱스 별, 기타 필드
위의 클래스 인스턴스를 보유하려면 ArrayList 클래스를 사용하십시오. 나는이 내용을 인쇄 할 때 때문에 이미 문제를 알고
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 ;
}
}
을 :
p : 10003 : Conglomeration
c : 20001 : Woman : Lucy :10001 : 17 : 22 : 20
t : 30004 : Silver : 20005 : 120 : 1000
a : 40001 : Wand : 20007 : ElderWand
이것은 내가 지금까지 쓴 것입니다 :
테스트 데이터 (같이 보입니다) partyList 배열의 그것은 비어 있습니다. 나는 이유를 알 수없는 것 같습니다. 현재
오류 : 나를 경우 - 다른 루프를 대신 루프에 모든 일을 (스캔을 확인하려면 해당 클래스 '에 보면'하는
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)
아이디어는 ** 가능한가)? 예 : 첫 글자가 'p'인 경우 파티 클래스로 이동하여 줄을 처리하는 방법을 확인합니다.
모든 GUI 물건을 제거하고 입력을 구문 분석에 관한에만 코드를 게시하세요. –
그리고 지금까지 문제를 진단하는 데 사용한 단계는 무엇입니까? –
정적 데이터를 XML 파일에 저장하는 것이 좋습니다. 길이가 길고 자세한 정보를 얻기 시작하기 때문입니다. – L7ColWinters