嘿伙计们我刚开始学习Java(第一语言),我可能会问一个非常简单的问题,但我一直在写一本基本的地址簿。我的问题是,当我每次在回答我的一个问题时留下一个空格保存到.txt文件时,它会将其保存为.txt文件中的新行,我无法弄清楚原因。
很抱歉,如果这对你们中的一些人来说是浪费时间,但如果有人要快速查看,我将非常感激。
非常感谢!
Scanner address = new Scanner(System.in);
Writer saveFile = new FileWriter("addressSave.txt");
// Address Questions.
String Name;
System.out.println("Whats your name?");
Name = address.next();
saveFile.write (Name + "\r\n");
String country;
System.out.println("What country do you live in?");
country = address.next();
saveFile.write (country + "\r\n");
String Area;
System.out.print("Okay now what area do you live in?" + "\r\n");
Area = address.next();
saveFile.write (Area + "\r\n");
String Road;
System.out.print("Okay now what road do you live on?" + "\r\n");
Road = address.next();
saveFile.write (Road + "\r\n");
String Postcode;
System.out.print("And finally whats your postcode?" + "\r\n");
Postcode = address.next();
saveFile.write (Postcode + "\r\n");
if(address != null)
address.close();
// Closing save file
saveFile.close();
System.exit(1);
答案 0 :(得分:0)
虽然评论正确提及您不应该使用\r\n
(\n
应该足够,或者使用PrintWriter
和println(...)
代替),我认为您的错误是使用address.next()
而不是更合适的address.nextLine()
。