2012-10-02 6 views
1

나는 걸려 있습니다. 어떻게 이것을 작동 시키거나 더 좋은 방법이 있습니까? 코드 예제를 제공하십시오..txt 파일을 읽고 2 차원 문자 배열로 저장하십시오. java

public char[][] charmap = new char[SomeInts.amount][SomeInts.amount]; 
public void loadMap() throws IOException{ 
    BufferedReader in = new BufferedReader(new FileReader("map1.txt")); 
    String line = in.readLine(); 
    while (line != null){ 
     int y = 0; 
     for (int x = 0; x < line.length(); x++){ 

      //Error 
      charmap[x][y] = line[x]; 
      // 
     } 
     y++; 
    } 
} 

답변

4

구문 line[x]은 배열 용으로 예약되어 있습니다. String은 배열이 아닙니다. 당신은 String#charAt method를 사용하여 작성할 수

charmap[x][y] = line.charAt(x); 
+0

감사합니다. 오류가 해결되어 프로그램이 실행 가능해졌습니다. 생각대로 작동하지 않습니다. 나는 더 상세한 것을 게시 할 것이다. –

1

이 시도 .. 문자열에서 문자를 가져.

char[][] mapdata = new char[SomeInts.amount][SomeInts.amount]; 

public void loadMap() throws IOException{ 
    BufferedReader in = new BufferedReader(new FileReader("map1.txt")); 
    String line = in.readLine(); 
    ArrayList<String> lines = new ArrayList<String>(); 
    // Load all the lines 
    while (line != null){ 
     lines.add(line); 
    } 
    // Parse the data 
    for (int i = 0; i < lines.size(); i++) { 
     for (int j = 0; j < lines.get(i).length(); j++) { 
      mapdata[j][i] = lines.get(i).charAt(j); 
     } 
    } 
} 

희망이 있습니다.

관련 문제