2015-01-25 4 views
-1

이 줄을 쓰는 방법을 모르겠습니다. +print(cArray: char[]): void. 나는 숙제 문제로 무엇을하고 싶은지 알고있다. 책이 비열한 직업을 설명하는 것은 바로이 배열 선이다. 문제를 알고 싶다면 : Excercise12_15.tx 파일이 없으면 프로그램을 작성하십시오. 테스트 I/O를 사용하여 파일에 무작위로 생성 된 100 개의 정수을 작성하십시오. 정수는 파일의 공백으로 구분됩니다. 파일에서 데이터를 다시 읽고 순서대로 데이터를 표시하십시오.java.io.PrintWriter + print (cArray : char []) : void

package WriteReadData; 

import java.util.*; 
public class WriteReadData { 

    public static void main(String[] args) throws Exception 
    { 
     java.io.File file = new java.io.File("Excercise12_15.txt"); 
     final int SIZE = 100; 
     int [] emptyArray = new int[SIZE]; 

     if (file.exists()) 
     { 
      System.out.print("File exists"); 
      System.exit(0); 
     }//end if 

     try 
     { 
      java.io.PrintWriter output = new java.io.PrintWriter(file); 

      for (int i = 1; i < SIZE; i++) 
      { 
      emptyArray[i] = (int)(Math.random() * 100); 
      output.print(emptyArray: int[]): void 
      }//end for 

     }//end try 
     catch 
     { 
      output.close(); 
     }//end catch 
    }//end main 
}//end class 
+0

이 프로그램은 물론 끝나지 않았습니다. 올바른 방향으로 움직이기 위해서는 대답해야합니다. – Doug099

+0

배열을 텍스트 파일에 인쇄하는 데 문제가 있습니까? –

+0

catch 블록에 output.close()를 배치하지 마십시오. –

답변

0
java.io.File file = new java.io.File("C:/Users/someUser/Desktop/Excercise12_15.txt"); 
    final int SIZE = 100; 
    int [] emptyArray = new int[SIZE]; 

    if (file.exists()) 
    { 
     System.out.print("File exists"); 
     System.exit(0); 
    }//end if 

//Place your output variable up here, so that it could be seen in the catch and finally block. 

    java.io.PrintWriter output = null; 
    try 
    { 
     output = new java.io.PrintWriter(file); 

     for (int i = 1; i < SIZE; i++) 
     { 
      emptyArray[i] = (int)(Math.random() * 100); 
      //Your issuse was here, you didn't write the array to the file correctly 
      output.print(emptyArray[i] + " "); 
     }//end for 

    }//end try 
    catch (Exception ex) 
    { 
     System.out.println(ex.getMessage()); 
    }//end catch 
    finally{ 
     //Don't place the close in the catch block, do it in the finally, because it always 
     //executes even when a catch happens. 
     output.close(); 
    } 

} 

이 제대로 공백이있는 텍스트 파일로 배열을 작성하는 방법입니다.

+0

도움을 주셔서 감사합니다. 그러나이 책은 예제를 다음과 같이 나열하는 이유는 무엇입니까? + print (cArray : char []) : void? (Java 10th Edition 소개, 다니엘 리앙). – Doug099

+0

@ Doug099 - 대단히 죄송 합니다만 그 책은 없습니다. 실수 일 수 있습니다. 공식 사이트에서 오류를 확인하십시오. –

관련 문제