2012-01-31 5 views
1

4 클래스 :ArrayList를

  1. Party - 인덱스, 이름, 위치, 생물의 숫자의 목록 (생물 클래스의 인스턴스에 대한 링크 액세스).
  2. Creature - 인덱스, 유형, 이름, 인덱스 자, 공감 값, 공포의 가치, 보물의 목록, 유물의 목록
  3. Treasure - 인덱스, 유형, 인덱스, 중량 생물, 값
  4. 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'인 경우 파티 클래스로 이동하여 줄을 처리하는 방법을 확인합니다.

+3

모든 GUI 물건을 제거하고 입력을 구문 분석에 관한에만 코드를 게시하세요. –

+0

그리고 지금까지 문제를 진단하는 데 사용한 단계는 무엇입니까? –

+0

정적 데이터를 XML 파일에 저장하는 것이 좋습니다. 길이가 길고 자세한 정보를 얻기 시작하기 때문입니다. – L7ColWinters

답변

2

나는 당신의 문제는 당신이이 라인에있는 모든 입력을 통해 스캔 있다는 생각 :

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

은 또한 당신의 클래스가 이상한 것 같다. 왜 static Object을 사용해야하나요?
그냥 예를 들어, 각 유형에 대한 기존의 클래스를 구축 :이 같은

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. 
} 

뭔가 :http://goo.gl/hM2Fo

+0

사실 ... 아마도 이것일지도 모릅니다. 내가 그것을 바꾸어 보자! – tommy1370

+0

오케이! 지금 나는 약간의 오류가 떠오르고있다. (나는 내 ​​질문에 그것들을 올릴 것이다) – tommy1370

+0

'System.out.println'은 당신의 친구이다. 이를 사용하여 코드의 단계를 확인하십시오. 예를 들어'String line = scanner.nextLine();'의 값을 출력하여 코드가 도달했는지 확인하십시오. – rtheunissen

1

이 문제에 대한 조언을 드리면 디버그 모드를 사용해보십시오.

추신. 주된 문제는 당신이 아무 것도없는 객체를 만들려고한다는 것입니다.

0

내가 잘못 읽지 않는다면 모든 객체가 null을 반환합니다. 그런 다음 null 참조를 arrayList에 추가하려고 시도하고 있으므로 null 참조가 무시됩니다. 실제로 객체를 만들고 결과를 봅니다. 추가의 컬렉션의 javadoc에서

()

Collections that support this operation may place limitations on what elements may be 
added to this collection. In particular, some collections will refuse to add null 
elements, and others will impose restrictions on the type of elements that may be added. 
Collection classes should clearly specify in their documentation any restrictions on what 
elements may be added.