The program seems to be working but it's producing a weird output. I'm trying to figure out how to fix this issue or what the issue even is. Where should I change to formatting of the file so that each method would have it's own line with explanation? And it also seems the the file is displayed more than once. Here's the code:
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;
}
}
The program output therefore is the following :
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
Is it just the lack of newlines in your output? Generally when printing human-readable output, you could wrap your Writer with something like PrintWriter, and use println() or format(). Here's a small sample like yours:
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());
}
}
}
The output is:
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