爪哇;从输入文本文件到输出文本文件的换行符
首先,这是我参加的第一堂编程课,所以如果这是一个非常简单的问题,我深表歉意。对于家庭作业,我们必须编写一个程序来扫描文本文件,提示用户输入由 << 标识的单词。 >,然后将结果写入另一个文本文件。
我遇到的问题是输出文件全部写在一行上,而不是保留输入文件中的换行符。
例如:
输入
看起来像
这样的文本。
输出看起来像这样的文本。
这是我在相关方法中的代码。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能不想使用
out.print
,而是使用out.println
,它执行相同的操作,但会在行尾附加一个换行符。您还可以在要插入换行符的位置手动连接换行符 (\n
)。Instead of using
out.print
, you may want to useout.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.是的,乔纳森的答案会起作用。
所以我投票给乔纳森。
yep, Jonathan's answer will work.
so i vote for Jonathan's,.