【问题标题】:Sentinel not working in Java while-loop; and printwriter not writing to text fileSentinel 在 Java while-loop 中不起作用;和打印机不写入文本文件
【发布时间】:2017-02-07 17:00:31
【问题描述】:
package addlinenumbers;

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class AddLineNumbers {

    public static void main(String[] args) 
    {
    Scanner input = new Scanner(System.in);
    String sentinel = new String();
    int i=0;
        try 
        {
            FileOutputStream fos = new FileOutputStream
                   ("dataInput.txt", true); //true means we will be appending to dataInput.txt
        
            PrintWriter pw = new PrintWriter (fos);
        
            //write data to the file
            while(!(sentinel.equals("-1")))
            { 
                System.out.println("Please enter strings to prepend to 'dataInput.txt'; -1 to EXIT: ");
                pw.print(input.nextLine());
                i++;
            }
        }
        
        catch (FileNotFoundException fnfe)
        {
            System.out.println("Unable to find dataInput.txt...");
        }
        catch (IOException ioe) 
        {
            ioe.printStackTrace(System.out);
        }
        finally 
        { 
                System.out.println("# of objects: " + i);
                System.out.println("Closing file...");
                input.close();
        }
    }
}

目前,我的输出将无休止地要求我将字符串输入到“dataInput.txt”(位于相应的项目文件夹中),但它不会使用 Java 字符串的正确哨兵退出 while 循环。我在这里错过了什么吗?我没有使用==。 "-1" 什么都不做,只是再次循环。 应该退出,以前置方式将输入写入文本文件,然后关闭文件。

还有!事实证明,没有从 while 循环输入中获取任何内容并传输到“dataInput.txt”文件中。我不知道为什么。

任何帮助将不胜感激!

编辑:仅供参考,我必须使用带有哨兵的 while 循环。再次感谢所有/已经/将在这个问题上帮助我的人。

编辑 #2:考虑到 MadProgrammer 的出色建议,我在输出中留下了一个小问题:

run:
Please enter strings to prepend to 'dataInput.txt'; -1 to EXIT: 
David
Goliath
Please enter strings to prepend to 'dataInput.txt'; -1 to EXIT: 
Delilah
Samson
Please enter strings to prepend to 'dataInput.txt'; -1 to EXIT: 
-1
# of objects prepended: 2
Closing file...
BUILD SUCCESSFUL (total time: 18 seconds)

如您所见,它只接收两个对象,它们是“Goliath”和“Samson”,它们是写入文本文件的唯一字符串。从技术上讲,它应该有 4 个对象,并且“David”和“Delilah”应该也在文本文件中,但它们不是。

任何帮助将不胜感激。

【问题讨论】:

  • 你永远不会在你的循环中更新sentinel,所以它永远不会改变
  • “还有!事实证明,没有从 while 循环输入中获取任何内容并传输到 'dataInput.txt' 文件。我不知道为什么。” i> 可能是因为蒸汽从不关闭
  • 看看try-with-resources,它会让你的生活更轻松
  • 为什么哨兵需要更新?它是一个固定字符串,等于除“-1”之外的所有内容?另外..我将如何“更新”它?你提出了一个有趣的提示。谢谢先生。
  • 实际上是一个空字符串,或者""。但你明白了。您需要做的是将输入存储在变量中。例如,sentinel = input.nextLine(); pw.print(sentinel); 尽管正如您所指出的,sentinel 不是这个变量的好名字。

标签: java printwriter sentinel


【解决方案1】:

while(!(sentinel.equals("-1"))) 永远不能是false(用于循环条件),因为sentinel 永远不会改变,它总是""

从概念上讲,您需要读取用户输入并决定如何处理它,然后您将使用此值来确定是否需要退出循环

因此,这是一个“非常快速”的示例(未经测试),您可以做什么......

Scanner input = new Scanner(System.in);
int i = 0;
try (FileOutputStream fos = new FileOutputStream("dataInput.txt", true)) {
    try (PrintWriter pw = new PrintWriter(fos)) {
        String userInput = "";
        do {
            userInput = input.nextLine();
            if (!userInput.equals("-1")) {
                pw.print(input.nextLine());
                i++;
            }
        } while (!userInput.equals("-1"));
    }
} catch (FileNotFoundException fnfe) {
    System.out.println("Unable to find dataInput.txt...");
} catch (IOException ioe) {
    ioe.printStackTrace(System.out);
} finally {
    System.out.println("# of objects: " + i);
}

仅供参考:input.close(); 不是关闭“文件”,而是关闭标准输入,这绝不是一个好主意

注意:复合 try-with 块是多余的,您可以使用单个语句将其全部包含在内,但我想围绕类似的代码结构来演示这个概念

【讨论】:

  • 我认为(如果我在这里错了,请纠正我)因为它代表第二个 try 块内的某些内容导致输出在每个 do-while 中接受两行而不是一个,然后仅在前面添加文本文件的第二行。我试图找出如何消除第一个空行。在其中输入任何内容并按回车键,它只会转到下一行并将其作为行信息。
  • 将第一个nextLine改为next,使其按顺序输入,但不会将信息写入文本文件。
  • 根据我的测试,它运行良好。了解next 将在空白处中断。我也会考虑使用pw.println
  • 就目前而言,我需要输入两次字符串值才能注册。仍在寻找 while 循环以寻找线索。再次感谢您与我 MP。
  • 另外,我在String userInput = ""; 收到一个“未使用的作业”...不知道该怎么办。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
相关资源
最近更新 更多