2014-07-06 5 views
1

공백을 나타내는 정수의 줄과 그림을 만들기 위해 인쇄 할 문자를 포함하는 입력 파일을 만드는 프로그램을 만들려고합니다. 행의 첫 번째 정수는 항상 인쇄 할 공백 수입니다 (0 일 수 있음). 모든 대체 정수는 인쇄 할 문자 수입니다. 행의 마지막 정수는 항상 -1이되어 행의 끝을 표시합니다. 예를 들어, 0 2 4 2 -1 행은 "0 공백 - 2 문자 - 4 공백 - 2 문자 - 다음 행"을 나타냅니다. 문자는 ...프로그램이 완전히 끝내지 못했습니다.

이것은 내가 지금까지 무엇을 가지고 예 : "#"또는 "%"로 무엇이든 할 수있다

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

public class PicturePrinter 
{ 
private Scanner fileScanner; 
private FileWriter fwriter; 
private PrintWriter outputFile; 

public PicturePrinter(File file, boolean appendToOutputFile) throws IOException 
{ 
    fileScanner = new Scanner(file); 
    if (appendToOutputFile) 
    { 
     fwriter = new FileWriter("output.txt", true); 
     outputFile = new PrintWriter(fwriter); 
     printPicture(true); 
     outputFile.close(); 
    } 
    else 
    { 
     printPicture(false); 
    } 
    fileScanner.close(); 
} 

//*************** 

public static void main(String[] args) throws IOException 
{ 
    String infileName; 
    Scanner keyboard = new Scanner(System.in); 
    boolean badFile; 
    File file; 
    System.out.println("This program prints pictures from input files."); 
    System.out.println("Do you need a picture printed? (y or n): "); 
    String answer = keyboard.nextLine(); 
    while (answer.charAt(0) == 'y' || answer.charAt(0) == 'Y') 
    { 
     do 
     { 
      System.out.print("Enter your input file's name: "); 
      infileName = keyboard.nextLine(); 
      file = new File(infileName); 
      if (!file.exists()) 
      { 
       badFile = true; 
       System.out.println("That file does not exist."); 
      } 
      else 
      { 
       badFile = false; 
      } 
     } 
     while (badFile); 
     System.out.print("Would you like to export the picture to output.txt? (y or n): "); 
     answer = keyboard.nextLine(); 
     if (answer.charAt(0) == 'y' || answer.charAt(0) == 'Y') 
     { 
      PicturePrinter pp = new PicturePrinter(file, true); 
     } 
     else 
     { 
      PicturePrinter pp = new PicturePrinter(file, false); 
     } 
     System.out.print("Would you like another picture printed? (y or n): "); 
     answer = keyboard.nextLine(); 
    } 
} 

public static void printPicture(boolean picture) 
{ 
    Scanner key = new Scanner(System.in); 
    while (key.hasNextLine()) 
     { 
      if (key.hasNextInt() && key.nextInt() != -1) 
      { 
       int space = key.nextInt(); 
       System.out.format("[%spaces]%n", ""); 
       int chars = key.nextInt(); 
       System.out.format("[%charss]%n", "#"); 
      } 
      else 
      { 
       System.out.println(); 
      } 
     } 
    } 
} 

모든 것이 괜찮아 컴파일하지만이 프로그램을 실행할 때, 그것은 후에 빈 간다 그림을 내보내는 것에 대한 대답을 얻습니다. 그 후에는 아무 것도하지 않습니다. 나는 그것의 엉망으로 코드의 하단에있는 printPicture 메서드를 생각합니다. 나는 그것을 해결하기 위해 무엇을 해야할지 잘 모릅니다.

입력 파일은 printPicture 방법이 아닌 표준 입력 파일에서 읽어야이

4 3 3 -1 
2 4 1 1 2 -1 
1 1 1 1 1 2 1 1 1 -1 
0 3 2 1 1 3 -1 
5 1 4 -1 
2 3 1 3 1 -1 
1 5 1 3 -1 
1 3 1 1 1 1 1 1 -1 
1 5 1 3 -1 
2 3 1 3 1 -1 
+0

새 스캐너 (System.in); - 사용자 입력을 인쇄 하시겠습니까? – MGorgon

+0

예. 'printPicture' 메쏘드는 어떻게해야할까요? 왜 그것이 "엉망으로 만든다"고 생각하니? –

답변

0

hasNextLine()hasNextInt()은 입력을 예상하는 차단 방법입니다. 파일에서 입력을 제공한다고 가정하지만 대신 printPicture() 메서드에서 스캐너를 Scanner key = new Scanner(System.in);으로 설정하면 메서드가 영원히 중지됩니다.

0

처럼 보인다. 즉, printPicture에서 잘못된 스캐너를 사용하고 있습니다. key 스캐너를 제거하고 대신 fileScanner을 사용하십시오.

관련 문제