爪哇;从输入文本文件到输出文本文件的换行符

发布于 2024-12-17 01:44:57 字数 1532 浏览 1 评论 0原文

首先,这是我参加的第一堂编程课,所以如果这是一个非常简单的问题,我深表歉意。对于家庭作业,我们必须编写一个程序来扫描文本文件,提示用户输入由 << 标识的单词。 >,然后将结果写入另一个文本文件。

我遇到的问题是输出文件全部写在一行上,而不是保留输入文件中的换行符。

例如:

输入

看起来像

这样的文本。

输出看起来像这样的文本。

这是我在相关方法中的代码。

public static void createMadLib(Scanner console) throws FileNotFoundException {
    Scanner input = getInput(console);
    System.out.print("Output file name: ");     
    PrintStream out = new PrintStream(new File(console.nextLine()));
    System.out.println();

    String text = input.next();
            while (input.hasNext()){


                    if (text.startsWith("<")){
                        text = text.substring(1, text.length()-1);
                        text = text.replace("-"," ");
                        if ((text.startsWith("a"))||(text.startsWith("e"))||(text.startsWith("i"))||(text.startsWith("o"))||(text.startsWith("u"))){                        
                            System.out.print ("Please type an " + text + ": ");
                            text = (console.nextLine());
                        }else {
                            System.out.print ("Please type a " + text + ": ");
                            text = (console.nextLine());
                        }
                    }
                    out.print(text + " "); 

                    text = input.next();

            }//ends while loop  
    out.print(text);
    prompt(console);
    }

对于任何格式错误,我深表歉意,再次强调,这是我的第一堂编程课。

感谢您的帮助

First, this is the first programming class I've taken, so I apologize if this is a pretty simple problem. for a homework assignment, we have to write a program which scans a text file, prompts the user for words identified by < >, and then writes the result to another text file.

The problem I'm having is that the output file is all written on a single line, instead of preserving the line breaks from the input file.

For example:

The input

looks like

this text.

the output looks like this text.

This is the code I have in the relevant method.

public static void createMadLib(Scanner console) throws FileNotFoundException {
    Scanner input = getInput(console);
    System.out.print("Output file name: ");     
    PrintStream out = new PrintStream(new File(console.nextLine()));
    System.out.println();

    String text = input.next();
            while (input.hasNext()){


                    if (text.startsWith("<")){
                        text = text.substring(1, text.length()-1);
                        text = text.replace("-"," ");
                        if ((text.startsWith("a"))||(text.startsWith("e"))||(text.startsWith("i"))||(text.startsWith("o"))||(text.startsWith("u"))){                        
                            System.out.print ("Please type an " + text + ": ");
                            text = (console.nextLine());
                        }else {
                            System.out.print ("Please type a " + text + ": ");
                            text = (console.nextLine());
                        }
                    }
                    out.print(text + " "); 

                    text = input.next();

            }//ends while loop  
    out.print(text);
    prompt(console);
    }

I apologize for any formatting faux pas, again, this is my first programming class.

Thanks for your help

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

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

发布评论

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

评论(2

爱要勇敢去追 2024-12-24 01:44:57

您可能不想使用 out.print,而是使用 out.println,它执行相同的操作,但会在行尾附加一个换行符。您还可以在要插入换行符的位置手动连接换行符 (\n)。

Instead of using out.print, you may want to use out.println, which does the same thing, but appends a newline character to the end of the line. You can also manually concatenate the newline character (\n) where you wish to insert linebreaks.

空宴 2024-12-24 01:44:57

是的,乔纳森的答案会起作用。
所以我投票给乔纳森。

yep, Jonathan's answer will work.
so i vote for Jonathan's,.

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