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();

}

}

[4680 byte] By [adamkfoxa] at [2007-11-27 11:04:36]
# 1

Please......anyone?x

adamkfoxa at 2007-7-29 13:01:38 > top of Java-index,Java Essentials,New To Java...
# 2

> Please......anyone?x

1) Unformatted code.

2) Vague description of problem (e.g., "It's not working").

3) Extraneous code not related to the problem.

These are among the reasons why some people may not be interested in answering your question.

Please be a little more explicit about what you're doing, what you expect to happen, and what you actually observe.

Please post a short, concise, executable example of what you're trying to do. Don't post the actual code you are using (I can't emphasize this strongly enough). Just write a small example that demonstrates the problem, and only that. Wrap the code in a class and give it a main method that runs it - if we can just copy and paste the code into a text file, compile it and run it without any changes, then we can be sure that we haven't made incorrect assumptions about how you are using it.

Post your code between [code] and [/code] tags. Cut and paste the code, rather than re-typing it (re-typing often introduces subtle errors that make your problem difficult to troubleshoot). Please preview your post when posting code.

Please assume that we only have the core API. We have no idea what SomeCustomClass is, and neither does our collective compiler.

If you have an error message, post the exact, complete error along with a full stack trace, if possible. Make sure you're not swallowing any Exceptions.

Read this: http://mindprod.com/jgloss/sscce.html

Help us help you solve your problem.

~

yawmarka at 2007-7-29 13:01:38 > top of Java-index,Java Essentials,New To Java...