2012-12-14 3 views
2

.txt 파일에서 9x9의 int 배열을 저장하려고 시도했습니다. 나는이 웹 사이트에서 매우 오랜 시간 동안이 작업을 시도하고 있었고, 많은 조정을 한 후에, 나는 그것을 얻는 것에 아주 가깝다. 유일한 문제는 아무 것도 배열에 저장되지 않는다는 것입니다.이 .txt 파일을 2D int 배열에 쓰고 NumberFormatException을 수신하지 못하도록하려면 어떻게해야합니까?

import javax.swing.*; 
import java.util.*; 
import java.io.*; 
import java.awt.*; 


public class test { 

public static void main(String[] args) throws IOException { 


    int[][] thing = new int[9][9]; 
    int row = 0; 
    int rows = 9; 
    int columns = 9; 


    File fin = new File("C:\\Users\\David\\workspace\\tester\\initial.txt"); 

    BufferedReader reader = null; 

    try { 
     reader = new BufferedReader(new FileReader(fin)); 
     String line = reader.readLine(); 
     int lineNum = 0; 

     while (line != null) { 
      lineNum++; 
      System.out.println("line " + lineNum + " = " + line); 
      line = reader.readLine(); 

      String [] tokens = line.split(","); 
      for (int j=0; j<tokens.length; j++) { 
       System.out.println("I am filling the row: " + row); 
       thing[row][j] = Integer.parseInt(tokens[j]); 
      } 
      row++; 
     } 

     System.out.println("I am printing the array for testing purposes: "); 
     for (int i=0; i < rows; i++) { 
      for (int j = 0; j < columns; j++) 
       System.out.print(thing[i][j]); 
      System.out.println(""); 
     } 
    } catch (IOException error) { 

    } finally { 
     if (reader != null) reader.close(); 
    } 

} 

}

나는 내가 단순한 사이드 프로젝트로 만들려고하고 스도쿠 게임에 대한 테스트로이 일을하고 있다고한다, 그냥 오전 : 여기

내 코드입니다 슈퍼 좌절.

이것은 또한이 사이트의 첫 번째 게시물 이었으므로 서식을 쉽게 지정해주십시오. 모두에게 감사드립니다!

편집 : 변경 codaddict을 만들어도 얘기하고 지금은 출력을 얻을 :

line 1 = 5 3 0 0 7 0 0 0 0 
I am filling the row: 0 
Exception in thread "main" java.lang.NumberFormatException: For input string: "6 0 0 1 9 5 0 0 0" 
at java.lang.NumberFormatException.forInputString(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at test.main(test.java:36) 
+1

디버그 라인 "나는 행을 채우고 있습니다 :"는 출력에 나타나지 않습니다 ... 모든 파일을 읽은 다음 구문 분석하려고합니다. 모든 읽기 및 쓰기 논리를 동일한 루프에 포함하도록 변경하십시오. – Averroes

+0

Sodoku 필드를 .txt로로드해야합니까? 그렇지 않으면 전체 Sodoku 구조체에 클래스를 사용하여'Serializable'으로해야합니다. – SomeJavaGuy

답변

1

문자열 line 루프 따라서 두 번째 루프가 실행되지 않습니다 동안 동안 먼저 종료 한 후 이미 null의 경우, 따라서 아무 값도 배열 셀에 할당되지 않습니다. int 배열 셀의 기본값은 0이며 인쇄됩니다. 첫 번째 루프에 할당 부분을 삽입하여 문제를 해결하십시오.

3

당신은 일을 :

while (line != null) { 
     lineNum++; 
     System.out.println("line " + lineNum + " = " + line); 
     line = reader.readLine(); 
    } 

    // At this point you've already reached the end of the file 
    // and line is null, so you never go inside the next while. 

    while (line != null) { 
     // you need to split on space not comma 
     String [] tokens = line.split(" "); 
     for (int j=0; j<tokens.length; j++) { 
      System.out.println("I am filling the row: " + row); 
      thing[row][j] = Integer.parseInt(tokens[j]); 
     } 
     row++; 
    } 

당신이 루프 동안 외부의 각 행을 처리해야이 문제를 해결하려면 :

while (line != null) { 
     lineNum++; 
     System.out.println("line " + lineNum + " = " + line); 
     line = reader.readLine(); 

     String [] tokens = line.split(","); 
     for (int j=0; j<tokens.length; j++) { 
      System.out.println("I am filling the row: " + row); 
      thing[row][j] = Integer.parseInt(tokens[j]); 
     } 
     row++; 
    } 
+0

이제 Number Format 오류가 발생합니다. 스레드 "main"의 예외 java.lang.NumberFormatException : 입력 문자열 : "6 0 0 1 9 5 0 0 0" \t at java.lang.NumberFormatException.forInputString (알 수없는 소스) \t at java.lang.Integer. parseInt (알 수없는 소스) \t에서 java.lang.Integer.parseInt (알 수없는 소스) \t at test.main (test.java:36) – Dusquad

+0

@Dusquad : 내 대답을 편집했습니다. 쉼표가 아닌 공백으로 입력 문자열을 분할해야합니다. – codaddict

0

당신은 동안 루프 자체가 처음에 전체 파일을 읽었습니다.

따라서 두 번째 루프에서 값을 인쇄 할 수 없습니다.

첫 번째 while 루프에 텍스트 파일의 값을 저장해야합니다.

while (line != null) { 
    lineNum++; 
    System.out.println("line " + lineNum + " = " + line); 
    line = reader.readLine(); 

    String [] tokens = line.split(","); 
    for (int j=0; j<tokens.length; j++) { 
     System.out.println("I am filling the row: " + row); 
     thing[row][j] = Integer.parseInt(tokens[j]); 
    } 
    row++; 
}