该程序似乎正在运行,但是产生了一个奇怪的输出。我试图弄清楚如何解决此问题,甚至是什么问题。我应该在哪里更改文件格式,以便每种方法都有自己的解释行?而且似乎该文件已显示多次。这是代码:
import java.util.*;
import java.io.*;
public class App {
public static void main(String[] args) {
String bridesName = getBridesName();
String groomsName = getGroomsName();
int numberOfGuests = getNumberOfGuests();
double squareFootage = getSquareFootage();
ArrayList <String> namesOfSongs = getNamesOfSongs();
int guestsPerSquarefoot = getGuestPerSquarefoot(numberOfGuests, squareFootage);
try {
//add all the data to the text file
FileWriter dataToFile = new FileWriter (new File("wedding.txt"), true);
dataToFile.write(" Name of the bride: " + bridesName + "\n");
dataToFile.write(" Name of the groom: " + groomsName + "\n");
dataToFile.write(" Number of guests: " + numberOfGuests + "\n");
dataToFile.write(" Square footage of the location: " + (Double.toString(squareFootage) + "\n"));
//add songs to the text file
for (String song : namesOfSongs)
{
dataToFile.write(song);
}
dataToFile.write(" Number of guests per square footage: " + guestsPerSquarefoot + "\n");
dataToFile.close();
//diplay data from the text file
Scanner scan = new Scanner (new File("wedding.txt"));
while (scan.hasNextLine())
{
System.out.println(scan.nextLine());
}
scan.close();
}
catch (Exception e) {
System.out.println("You got an error.");
}
}
Scanner scan = new Scanner(System.in);
public static String getBridesName(){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the name of the bride: ");
String brideName = scan.nextLine();
//scan.close();
return brideName;
}
public static String getGroomsName(){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the name of the groom: ");
String groomName = scan.nextLine();
//scan.close();
return groomName;
}
public static Integer getNumberOfGuests(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the total number of guests at the wedding: ");
int numberOfGuests = scan.nextInt();
//scan.close();
return numberOfGuests;
}
public static Double getSquareFootage(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the square footage of the location: ");
double squareFootage = scan.nextDouble();
//scan.close();
return squareFootage;
}
public static ArrayList <String> getNamesOfSongs(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the names of each song in the DJ's playlist: ");
System.out.println("When finished, enter done.");
ArrayList <String> NamesOfSongs = new ArrayList<>();
boolean loop = true;
while(loop) {
String input = scan.nextLine();
if(input.equals("done")) {
/* Stop the input => stop while loop*/
loop = false;
} else {
/* add song */
NamesOfSongs.add(input);
}
}
//scan.close();
return NamesOfSongs;
}
public static int getGuestPerSquarefoot(int numberOfGuests, double squareFootage){
int GuestsPerSquarefoot = (int)numberOfGuests/(int)squareFootage;
return GuestsPerSquarefoot;
}
}
因此,程序输出如下:
Please enter the name of the bride:
susan
Please enter the name of the groom:
mike
Enter the total number of guests at the wedding:
12
Enter the square footage of the location:
12
Enter the names of each song in the DJ's playlist:
When finished, enter done.
kkk
jjj
iii
done
fg23.2ss"222.2 susanmike
22.3 susanmike
jjj
iii
done
fg23.2ss"222.2 susanmike 12.5Name of
the bride: susanName of22.3 susanmikekeNumber of guests: 12Square footage of the location: 12.0kkkjjjiiiNumber of guests per square footag12.4susanmikehe bride: susanName of the groom: mikeNumber of guests: 12Square footage of the location: 12.4kkkjjji12.56susanmikeests per square footage: 1 Name of the bride: susan Name of the groom: mike Number of guests: 12 Squa13.56 susanmikehe location: 24.0kkkjjjiiinnn Number of guests per square footage: 0 Name of the bride: susan 12.5Name of
the bride: susanName of the groom: mikeNumber of guests: 12Square footage of the location: 12.0kkkjjjiiiNumber of guests per square footage: 1Name of the bride: susanName of the groom: mikeNumber of guests: 12Square footage of the location: 12.4kkkjjjiiiNumber of guests per square footage: 1 Name of the bride: susan Name of the groom: mike Number of guests: 12 Square footage of the location: 24.0kkkjjjiiinnn Number of guests per square footage: 0 Name of the bride: susan
Name of the groom: mike
Number of guests: 12
Square footage of the location: 12.0
kkkjjjiii Number of guests per square footage: 1
是否只是输出中缺少换行符?通常,在打印人类可读的输出时,可以使用诸如PrintWriter之类的东西包装Writer,并使用println()或format()。这是一个像您一样的小样本:
public static void main(String[] args) throws Exception {
String bridesName = "susan";
String groomsName = "mike";
int numberOfGuests = 12;
double squareFootage = 12;
ArrayList<String> namesOfSongs = new ArrayList<>();
namesOfSongs.add("kkk");
namesOfSongs.add("lll");
namesOfSongs.add("iii");
int guestsPerSquarefoot = 1;
//add all the data to the text file
try (PrintWriter dataToFile = new PrintWriter(new FileWriter("wedding.txt", true)))
{
dataToFile.println("Name of the bride: " + bridesName);
dataToFile.println("Name of the groom: " + groomsName);
dataToFile.println("Number of guests: " + numberOfGuests);
dataToFile.println("Square footage of the location: " + (Double.toString(squareFootage)));
//add songs to the text file
for (String song : namesOfSongs)
{
dataToFile.println(song);
}
dataToFile.write("Number of guests per square footage: " + guestsPerSquarefoot);
dataToFile.close();
}
try (Scanner scan = new Scanner (new File("wedding.txt"))) {
while (scan.hasNextLine())
{
System.out.println(scan.nextLine());
}
}
}
输出为:
Name of the bride: susan
Name of the groom: mike
Number of guests: 12
Square footage of the location: 12.0
kkk
lll
iii
Number of guests per square footage: 1
创建文件编写器时,使用append
标志。在每次运行时,新内容将附加到现有内容之后。
/**
* Constructs a {@code FileWriter} given the {@code File} to write and
* a boolean indicating whether to append the data written, using the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param file the {@code File} to write
* @param append if {@code true}, then bytes will be written
* to the end of the file rather than the beginning
* @throws IOException if the file exists but is a directory rather than
* a regular file, does not exist but cannot be created,
* or cannot be opened for any other reason
* @since 1.4
*/
public FileWriter(File file, boolean append)
将每首歌曲换行,在每首书面歌曲\n
或[]之后附加[C0
dataToFile.write(song + "\n");
输出
dataToFile.write("\t");
dataToFile.write(song);
dataToFile.write("\n");