2012-01-11 2 views
-2

안녕하세요. 안녕하세요. 안녕하세요. 안녕하세요. 다른 프로젝트의 코드를 변환 할 때 문제가있어 도움이 필요합니다. 'readFile'메서드에서 파일을 읽을 때 정수로 String을 구문 분석하려고합니다. 그러나,어레이가 필요하지만 발견되었습니다.

안부를, 마이크를

어떤 도움이 감사를

import java.util.*; 
import java.io.*; 


public class JavaApplication1 
{ 
static int [] matrix = new int [10]; 
static Scanner input = new Scanner(System.in); 

public static void main(String[] args) throws IOException 
{ 
    String fileName = "Integers.txt"; 

    // read the file 
    readFile(fileName); 

    // print the matrix 
    printArray(fileName, matrix); 


} 


// Read File 
     public static void readFile(String fileName) throws IOException 
     { 
      String line = ""; 

      FileInputStream inputStream = new FileInputStream(fileName); 
      Scanner scanner = new Scanner(inputStream); 
      DataInputStream in = new DataInputStream(inputStream); 
      BufferedReader bf = new BufferedReader(new InputStreamReader(in)); 

      int lineCount = 0; 
      String[] numbers; 
      while ((line = bf.readLine()) != null) 
      { 
       numbers = line.split(" "); 
       for (int i = 0; i < 10; i++) 
       { 
       matrix[lineCount][i] = Integer.parseInt(numbers[i]); 
       } 
       lineCount++; 
      } 
      bf.close(); 
     } 

    public static void printToFile(String fileName, String output) throws IOException 
{ 
    java.io.File file = new java.io.File(fileName); 
    try (PrintWriter writer = new PrintWriter(file)) 
    { 
     writer.print(output); 
    } 
} 

    public static void printArray(String fileName, int [] array) 
     { 
      System.out.println("The matrix is:"); 

      for (int i = 0; i < 10; i++) 
       { 
        System.out.println(); 
       } 
      System.out.println(); 
     } 



} 
+1

가 제대로 오류 메시지를 읽을 수 매트릭스를 원 추측 :은 행 번호가 있습니다. 이것은 오류가 발생한 행입니다. –

+2

"나에게 오류를주고있다": 누가? 어디에? 어떤 예외입니까? – Viruzzo

답변

1
matrix[lineCount][i] = Integer.parseInt(numbers[i]); 

잘못되고 나에게 오류 '발견 배열을 필수 사항은 int입니다'주고있다.

어느

matrix[lineCount]= Integer.parseInt(numbers[i]); 

OR

matrix[i]= Integer.parseInt(numbers[i]); 
3

matrix는 INT이다 matrix[lineCount] 수단 int 형의 어레이이다이어야한다.

matrix[lineCount][i]을 시도해보십시오. int의 위치를 ​​얻고 있습니다. 그 이유는 그 오류가 발생하는 이유입니다.

난 당신이 int[][] matrix = new int[10][10];

관련 문제