用于关闭 FileWriter 的哨兵值 (Java)

发布于 2024-09-28 15:17:13 字数 1043 浏览 1 评论 0原文

我正在尝试制作一个基本程序,允许用户创建文本文件并向其中添加单词列表。当用户写入“stopnow”时 - 文件应该关闭。不幸的是,现在它只是一个无限循环。我做错了什么:

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


    public class WordList 
    {
 public static void main(String[] args) throws IOException
 {
  System.out.println("What would you like to call your file? (include extension)");

  Scanner kb = new Scanner(System.in); // Create Scanner

  String fileName = kb.nextLine(); // assign fileName to name of file

  PrintWriter outputFile = new PrintWriter(fileName); // Create the file

  String input;

  System.out.println("Please enter the next word or stopnow to end"); //Prompt user for a word

  input = kb.nextLine(); // assign input as the word

  while (input != "stopnow")

  {
   outputFile.println(input); // print input to the file

   System.out.println("Please enter the next word or stopnow to end"); //Prompt user for a word

   input = kb.nextLine(); // assign input as the word

  } 

  outputFile.close(); //Close the File

  System.out.println("done!");




}

}

I'm trying to make a basic program that allows a user to create a text file and add a list of words to it. When the user writes "stopnow" - the file should close. Unfortunately, right now it's just an infinite loop. What am I doing wrong:

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


    public class WordList 
    {
 public static void main(String[] args) throws IOException
 {
  System.out.println("What would you like to call your file? (include extension)");

  Scanner kb = new Scanner(System.in); // Create Scanner

  String fileName = kb.nextLine(); // assign fileName to name of file

  PrintWriter outputFile = new PrintWriter(fileName); // Create the file

  String input;

  System.out.println("Please enter the next word or stopnow to end"); //Prompt user for a word

  input = kb.nextLine(); // assign input as the word

  while (input != "stopnow")

  {
   outputFile.println(input); // print input to the file

   System.out.println("Please enter the next word or stopnow to end"); //Prompt user for a word

   input = kb.nextLine(); // assign input as the word

  } 

  outputFile.close(); //Close the File

  System.out.println("done!");




}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2024-10-05 15:17:13

要检查字符串是否相等,请使用 .equals

 while (!input.equals("stopnow"))    
  {
   outputFile.println(input); // print input to the file    
   System.out.println("Please enter the next word or stopnow to end"); //Prompt user for a word
   input = kb.nextLine(); // assign input as the word    
  }

您当前正在做的是比较引用。

To check for String equality, use .equals

 while (!input.equals("stopnow"))    
  {
   outputFile.println(input); // print input to the file    
   System.out.println("Please enter the next word or stopnow to end"); //Prompt user for a word
   input = kb.nextLine(); // assign input as the word    
  }

What you are currently doing is comparing references.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文