程序没有完成整个过程

时间:2014-07-06 22:34:57

标签: java filewriter printwriter

我试图创建一个程序,该程序采用一个输入文件,该文件包含指示多个空格的整数行,并且可选择打印要创建图片的字符。一行中的第一个整数将始终是要打印的空格数(可能为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方法正在弄乱它。我只是不知道如何解决它。

输入文件如下所示

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

2 个答案:

答案 0 :(得分:0)

printPicture方法应该从文件中读取,而不是从标准输入中读取。换句话说,您在printPicture中使用了错误的扫描仪。移除key扫描仪,然后使用fileScanner

答案 1 :(得分:0)

hasNextLine()hasNextInt()是阻止输入的阻止方法。您可能希望提供文件中的输入,而是在方法Scanner key = new Scanner(System.in);中将扫描仪设置为printPicture() - 这就是该方法永远挂起的原因。