FileWriter question
Hi, I cant get my program to write to the text file properly. I've tried splitting up the FileWriter code to between the if statements and outside just just cant get it to write properly. Im looking for the program to write the name, rate, length and price to append to the text file in the format:
Name / Rate / Length / Price
Any help will be truly appreciated, thanks!
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class Marina extends JFrame
{
public Marina()
{
setTitle("Marina Charges");
setSize(350,250);
setLocation(800,20);
setBackground(Color.cyan);
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
add(topPanel, BorderLayout.CENTER);
JTextArea area = new JTextArea();
JScrollPane scrollpane = new JScrollPane();
scrollpane.getViewport().add(area);
scrollpane.setBounds(10,10,320,200);
topPanel.add(scrollpane, BorderLayout.CENTER);
area.append("Prices Per Metre:\n\n");
area.append("Hourly Rate 0.50\n");
area.append("Daily Rate 1.00\n");
area.append("Weekly Rate 6.30\n");
area.append("Monthly Rate 19.00\n");
area.append("Yearly Rate 132.00\n");
setVisible(true);
setEnabled(false);
}
public static void main(String[] args) throws IOException
{
new Marina();
String name, rate, length, stay;
double price, numberRate, numberLength, numberStay;
name = JOptionPane.showInputDialog("Enter your name.");
rate = JOptionPane.showInputDialog("Enter the rate you would like to use.\n 1. Hourly\n 2. Daily\n 3. Weekly\n 4. Monthly\n 5. Annualy");
if (rate.equals("1"))
{
length = JOptionPane.showInputDialog("Enter the length of the yacht (in meters).");
stay = JOptionPane.showInputDialog("Enter the amount of hour(s) you wish to stay");
numberLength = Double.parseDouble(length);
numberStay = Double.parseDouble(stay);
numberRate = Double.parseDouble(rate) - 0.5;
price = numberRate * numberLength * numberStay;
JOptionPane.showMessageDialog(null, "The price to pay is " + price,"Results",JOptionPane.PLAIN_MESSAGE);
}
else if (rate.equals("2"))
{
length = JOptionPane.showInputDialog("Enter the length of the yacht (in meters).");
stay = JOptionPane.showInputDialog("Enter the amount of day(s) you wish to stay");
numberLength = Double.parseDouble(length);
numberStay = Double.parseDouble(stay);
numberRate =Double.parseDouble(rate) - 1;
price = numberRate * numberLength * numberStay;
JOptionPane.showMessageDialog(null, "The price to pay is " + price,"Results",JOptionPane.PLAIN_MESSAGE);
}
else if (rate.equals("3"))
{
length = JOptionPane.showInputDialog("Enter the length of the yacht (in meters).");
stay = JOptionPane.showInputDialog("Enter the amount of week(s) you wish to stay");
numberLength = Double.parseDouble(length);
numberStay = Double.parseDouble(stay);
numberRate =Double.parseDouble(rate) + 4.30;
price = numberRate * numberLength * numberStay;
JOptionPane.showMessageDialog(null, "The price to pay is " + price,"Results",JOptionPane.PLAIN_MESSAGE);
}
else if (rate.equals("4"))
{
length = JOptionPane.showInputDialog("Enter the length of the yacht (in meters).");
stay = JOptionPane.showInputDialog("Enter the amount of month(s) you wish to stay");
numberLength = Double.parseDouble(length);
numberStay = Double.parseDouble(stay);
numberRate =Double.parseDouble(rate) + 15;
price = numberRate * numberLength * numberStay;
JOptionPane.showMessageDialog(null, "The price to pay is " + price,"Results",JOptionPane.PLAIN_MESSAGE);
}
else if (rate.equals("5"))
{
length = JOptionPane.showInputDialog("Enter the length of the yacht (in meters).");
stay = JOptionPane.showInputDialog("Enter the amount of year(s) you wish to stay");
numberLength = Double.parseDouble(length);
numberStay = Double.parseDouble(stay);
numberRate =Double.parseDouble(rate) + 127;
price = numberRate * numberLength * numberStay;
JOptionPane.showMessageDialog(null, "The price to pay is " + price,"Results",JOptionPane.PLAIN_MESSAGE);
}
File outFile = new File("RecordedCharges.txt");
FileWriter out = new FileWriter(outFile, true);
out.write(name + " / " + rate + " / " + length + " / " + stay + " / " + price + "\n");
out.close();
}
}

