2014-11-25 6 views
0

이 프로젝트는 3 개의 별도 메인 클래스를 가지고 있습니다. 알파벳 순으로 정렬 된 국가 목록의 파일을 입력하면 정렬되지 않은 파일이 무작위로 재 배열 된 파일로 출력됩니다.정렬 된 파일을 입력하고 압축을 해제 한 다음 정렬되지 않은 파일 (Java)을 출력합니다.

내 첫 메인 클래스는 다음과 같습니다

package assignment3; 
import java.io.PrintWriter; 
import java.io.File; 
import java.util.Scanner; 

public class Assignment3 {` 

public static void main(String[] args) throws Exception{ 
    Scanner stdIn = new Scanner(new File("C:/Users/Vicki/Desktop/CountrySortedFormat.txt")); 
    PrintWriter out = new PrintWriter("C:/Users/Vicki/Desktop/CountryUnsortedFormat.txt"); 

    String[] line = new String[238]; 
    while (stdIn.hasNextLine()){  
     for (int k = 0; k <= line.length-1; k++){ 
      line[k]=stdIn.nextLine(); 
      out.println(line[k]); 
      out.close(); 
     } 
    } 
} 
} 

내 코드는 눈에 보이는 문제가 발생하지 않는하지만 난 배열을 인쇄 시도 "널 (null)"의 배열을 얻었다. 내가 뭔가 잘못하고 있는거야?

편집 : CountryUnsortedFormat

+0

정답으로 질문을 변경할 수 없으므로 향후이 질문을 한 사람들에게 오해가 생길 수 있습니다. – Lrrr

+0

또한 여러 문제가 내가 언급 한 및 내 대답을 해결할 수 있습니다. – Lrrr

답변

0

. 무엇보다도이 라인이 있습니다 :

while (stdIn.hasNextLine()) 

이 라인이있는 이유는 무엇입니까?

for (int k = 0; k <= line.length-1; k++) 

이미 루프가 있으며 두 번째 루프가 필요하지 않습니다.

또한 모든 루프에서 출력을 닫습니다! 그게 뭐야?

out.close(); 

방금 ​​함수를 끝내면됩니다. 이 당신의 출력과 같이 정렬 한 후

int k = 0; 
while (stdIn.hasNextLine()) { 
    // for (int k = 0; k <= line.length - 1; k++) { 
    line[k] = stdIn.nextLine(); 
    out.println(line[k]); 
    k++; 
    // } 
} 
out.close(); 

는 또한 출력 정렬되지 않은 상태로 만들기 위해 어떤 일을 didnt는

:

루프는 다음과 같이해야한다.

+0

감사! 이것은 많은 도움이됩니다! 내가 아직 작업하고있는 클래스가 2 개 더 있습니다. 다음 클래스는 파일을 정렬 해제합니다. – RadicalVick

1

그 라인의 PrintWriter 파일 이름을 변경 :

PrintWriter out = new PrintWriter("C:/Users/Vicki/Desktop/CountrySortedFormat.txt"); 

파일을 다시 만듭니다. 이 줄 뒤에 입력 파일이 비어 있습니다. 코드와

가 여러 문제 : @Jens이 너무이 문제가 말하는 옆에

+0

고마워, 나는 실수했다고 생각하지 않았다. CountryunsortedFormat으로되어 있습니다. – RadicalVick

+0

옌스 당신은 내 대답에서 말한대로 맞지만, OP는 내가 여러 가지 문제점을 가지고있다. – Lrrr

0
while (stdIn.hasNextLine()) { 
      // for (int k = 0; k <= line.length - 1; k++) { 
      String str = stdIn.nextLine(); 
      out.println(str); 
      // out.close(); --> here you are closing the out put writer so after first line it will be closed. 
      // } 

     } 

루프 외부의 스캐너와 인쇄기를 닫습니다. 그렇지 않으면 첫 번째 줄 다음에 작성자가 닫히고 첫 번째 줄만 인쇄됩니다.

0

@Ali가 지적한 것처럼 코드에는 여러 가지 결함이 있습니다. 또한 입력 정렬을위한 로직이 없습니다.

배열 정렬 대신 ArrayList를 사용하면 배열 정렬이 쉬워집니다. 다음은 용도에 맞는 샘플 코드입니다.

import java.io.PrintWriter; 
import java.io.File; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Iterator; 
import java.util.Scanner; 

public class Assignment3 { 

    public static void main(String[] args) throws Exception{ 
     Scanner stdIn = new Scanner(new File("sortedInput.txt")); 
     PrintWriter out = new PrintWriter("unsortedOutput.txt"); 

     ArrayList<String> line = new ArrayList<String>(); 
     System.out.println(stdIn.hasNextLine()); 
     while (stdIn.hasNextLine()){ 
       line.add(stdIn.nextLine()); 
     } 

     //here is your unsorting 
     Collections.shuffle(line); 

     for(int i=0; i < line.size() ; i++) 
      out.println(line.get(i)); 

     //Close printwriter once you are done writing everything 
     out.close(); 
    } 
}