文件编写器和空格?

发布于 2024-12-09 07:52:38 字数 1778 浏览 1 评论 0原文

我被要求编写一个作业,其中将提示用户输入键和/或值。

到目前为止,这是我的代码:

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

class bTree
{
    //Fields


    static Scanner input = new Scanner(System.in);

    static boolean done = false;

    public static void main(String args[])throws Exception
    {
        FileWriter fWriter = new FileWriter("data.txt");
        do
        {
            System.out.print("Enter command: ");
            String enter[] = input.nextLine().split(" ", 3);

            if(enter[0].toLowerCase().equals("insert"))
            {

                fWriter.write(enter[1] + "\n" + enter[2] + "\n");
                fWriter.flush();
            }
            else if(enter[0].toLowerCase().equals("select"))
            {
                FileReader fReader = new FileReader("data.txt");
                Scanner fileInput = new Scanner(fReader);

                while(fileInput.hasNext() && done == false)
                {
                    if(fileInput.nextLine().equals(enter[1]))
                    {
                        System.out.println(fileInput.nextLine());
                        done = true;
                    }
                    else
                    {
                        fileInput.nextLine();
                    }
                }
                done = false;
            }
            else if(enter[0].toLowerCase().equals("update"))
            {

                fWriter.write(enter[2]);
                fWriter.flush();
            }
            else if(enter[0].toLowerCase().equals("exit"))
            {
                System.exit(0);
            }
        }
        while(true);
    }
}

问题:当我打开 data.txt 时,没有空格。因此,如果我在记事本中输入“插入 1001 gen”和“10001 genny”,它将显示为“1001gen10001genny”。有什么建议吗?

I was asked to write an assignment wherein the user would be prompted to input a key and/or a value.

So far, here is my code:

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

class bTree
{
    //Fields


    static Scanner input = new Scanner(System.in);

    static boolean done = false;

    public static void main(String args[])throws Exception
    {
        FileWriter fWriter = new FileWriter("data.txt");
        do
        {
            System.out.print("Enter command: ");
            String enter[] = input.nextLine().split(" ", 3);

            if(enter[0].toLowerCase().equals("insert"))
            {

                fWriter.write(enter[1] + "\n" + enter[2] + "\n");
                fWriter.flush();
            }
            else if(enter[0].toLowerCase().equals("select"))
            {
                FileReader fReader = new FileReader("data.txt");
                Scanner fileInput = new Scanner(fReader);

                while(fileInput.hasNext() && done == false)
                {
                    if(fileInput.nextLine().equals(enter[1]))
                    {
                        System.out.println(fileInput.nextLine());
                        done = true;
                    }
                    else
                    {
                        fileInput.nextLine();
                    }
                }
                done = false;
            }
            else if(enter[0].toLowerCase().equals("update"))
            {

                fWriter.write(enter[2]);
                fWriter.flush();
            }
            else if(enter[0].toLowerCase().equals("exit"))
            {
                System.exit(0);
            }
        }
        while(true);
    }
}

Problem: When i open the data.txt, there are no spaces. So if i enter "insert 1001 gen" and "10001 genny", in notepad, it would come out as "1001gen10001genny". Any suggestions?

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

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

发布评论

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

评论(4

明媚如初 2024-12-16 07:52:38

问题是 notepad.exe 对行结尾很挑剔,并且有很多可能性。当您将 "\n" 写入 FileWriter 时,它会写入一个字符,即 '\n'。但记事本需要序列 "\r\n" 。它将单个“\n”显示为

这是您的代码,稍作修改以解决一些陷阱。

package so7696816;

import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.Scanner;

public class Excercise {

  public static void main(String args[]) throws Exception {
    final Scanner input = new Scanner(System.in);
    PrintWriter fWriter = new PrintWriter("data.txt");

    while (true) {
      System.out.print("Enter command: ");
      String enter[] = input.nextLine().split(" ", 3);
      final String command = enter[0].toLowerCase(Locale.ROOT);

      if (command.equals("insert")) {
        fWriter.println(enter[1]);
        fWriter.println(enter[2]);
        fWriter.flush();

      } else if (command.equals("select")) {
        FileReader fReader = new FileReader("data.txt");
        Scanner fileInput = new Scanner(fReader);
        while (fileInput.hasNextLine()) {
          String key = fileInput.nextLine();
          String value = fileInput.nextLine();
          if (key.equals(enter[1])) {
            System.out.println(value);
            break;
          }
        }
        fReader.close(); // don't leave files open

      } else if (command.equals("update")) {
        fWriter.write(enter[2]);
        fWriter.flush();

      } else if (command.equals("exit")) {
        return;

      } else {
        System.err.println("Unknown command: " + command);
      }
    }
  }
}

备注:

  • 我使用 PrintWriter 而不是 FileWriter 来获得正确的行结尾。
  • 对于select命令,我在使用后关闭了fReader
  • 我避免多次输入 enter[0].toLowerCase()
  • 我使用了 toLowerCase 的正确变体。
  • 我添加了对未知命令的错误处理。
  • 我重写了 select 命令,使其更加简洁。

The problem is that notepad.exe is picky about line endings, and there are many possibilities. When you write "\n" to a FileWriter, it writes a single character, namely '\n'. But notepad expects the sequence "\r\n" instead. It shows a single "\n" as nothing.

Here is your code, slightly modified to work around some pitfalls.

package so7696816;

import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.Scanner;

public class Excercise {

  public static void main(String args[]) throws Exception {
    final Scanner input = new Scanner(System.in);
    PrintWriter fWriter = new PrintWriter("data.txt");

    while (true) {
      System.out.print("Enter command: ");
      String enter[] = input.nextLine().split(" ", 3);
      final String command = enter[0].toLowerCase(Locale.ROOT);

      if (command.equals("insert")) {
        fWriter.println(enter[1]);
        fWriter.println(enter[2]);
        fWriter.flush();

      } else if (command.equals("select")) {
        FileReader fReader = new FileReader("data.txt");
        Scanner fileInput = new Scanner(fReader);
        while (fileInput.hasNextLine()) {
          String key = fileInput.nextLine();
          String value = fileInput.nextLine();
          if (key.equals(enter[1])) {
            System.out.println(value);
            break;
          }
        }
        fReader.close(); // don't leave files open

      } else if (command.equals("update")) {
        fWriter.write(enter[2]);
        fWriter.flush();

      } else if (command.equals("exit")) {
        return;

      } else {
        System.err.println("Unknown command: " + command);
      }
    }
  }
}

Remarks:

  • I used a PrintWriter instead of a FileWriter to get the line endings correct.
  • For the select command I closed the fReader after using it.
  • I avoided to type enter[0].toLowerCase() multiple times.
  • I used the proper variant of toLowerCase.
  • I added error handling for unknown commands.
  • I rewrote the select command to be a little more concise.
小ぇ时光︴ 2024-12-16 07:52:38

问题是 String Enter[] = input.nextLine().split(" ", 3); ,它杀死了空格。因此,每次使用 fWriter.write 时,请在每个数组条目后附加一个空格或写入一个附加的“”。

看这里

The problem is String enter[] = input.nextLine().split(" ", 3);, it kills the Spaces. So append a space after each array entry or write an additional " " everytime you use fWriter.write.

look here

我还不会笑 2024-12-16 07:52:38

如前所述,换行符对于记事本来说是不正确的。或者,您可以将该 FileWriter 包装在 BufferedWriter 中,并使用 newLine 方法始终插入正确的换行符。

As already stated the line feed character is incorrect for notepad. Alternatively you could wrap that FileWriter in a BufferedWriter and use the newLine method to always insert the correct line feed.

墨落成白 2024-12-16 07:52:38

我认为您正在 UNIX 中运行您的程序。在unix系统中“\r\n”是换行符。

如果您在 Windows 中运行程序,我认为该文件应该包含类似这样的内容。

1001

10001

I think you are running your program in UNIX. In unix system "\r\n" is the line feed.

If you are running your program in Windows, I think the file should contain something like this.

1001

gen

10001

genny

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