java - 程序没有完全完成

标签 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

最佳答案

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

关于java - 程序没有完全完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24600943/

相关文章:

java - 是否可以从 REST 请求中获取 POST 参数?

java - android使用pcap库

java - 即使测试失败,如何始终写入 Jenkins Groovy 管道中的 .csv 数据文件

java PrintWriter 无法解析

java - 当我从 @RestControler 返回对象时,如何在 json 中保留 map 键顺序

java - 处理谷歌日期字符串并递增它们

java - 为什么引入FileWriter会删除文件中的所有内容?

java - 写入文本文件 - 写入下一行

java - 将 FileWriter 作为参数传递给方法

java - 为什么此 PrintWriter 不填充此文件?