Moving output to text file
I need to direct the output of my program to a text file. I tried using the >>syntax, but I don't know if I used it correctly or put it in the correct place. I just tried to put java Quadratic>>output.txt at the end of the program. I think I will also need the input, a1, b1, and c1 in the text file too. Here's the program.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
publicclass Quadraticextends JFrameimplements ActionListener{
private JButton calculate =new JButton("Solve Quadratic for X");
private JTextField a1 =new JTextField("20"),
b1 =new JTextField("20"),
c1 =new JTextField("20");
private JPanel p1, p2;
private Quadratic quadratic;
private JLabel result1 =new JLabel("X = "),
result2 =new JLabel(" or ");
privatedouble d, ans1, ans2;
publicstaticvoid main(String[] args){
new Quadratic().setVisible(true);
}
public Quadratic(){
initComponents();
}
privatevoid initComponents(){
Container contentPane = getContentPane();
setSize(350,135);
setTitle("Quadratic Equation Solver");
calculate.addActionListener(this);
a1.setText("1.00");
b1.setText("4.00");
c1.setText("1.00");
p1 =new JPanel(new FlowLayout());
p1.add(a1);
p1.add(b1);
p1.add(c1);
p1.add(calculate);
p2 =new JPanel(new FlowLayout());
p2.add(result1);
p2.add(result2);
contentPane.add(p1, BorderLayout.NORTH);
contentPane.add(p2, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicvoid actionPerformed(ActionEvent e){
double a = Double.parseDouble(a1.getText());
double b = Double.parseDouble(b1.getText());
double c = Double.parseDouble(c1.getText());
d = Math.sqrt((b * b) - (4 * a * c));
if(d > 0){
ans1 = (-b + d)/(2 * a);
ans2 = (-b - d)/(2 * a);
}
result1.setText("X = " + ans1);
result2.setText(" or " + ans2);
}
}
[4547 byte] By [
an23dya] at [2007-11-27 11:16:44]

i would urge you to learn something from the code. its an easy piece of cake. anyone can do it. So its your choice learn it or leave it. if you have questions please ask.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class QuadraticGui extends JFrame implements ActionListener{
private JButton calculate = new JButton("Calculate"),
save = new JButton("save");
private JTextField a1 = new JTextField(5),
b1 = new JTextField(5),
c1 = new JTextField(5),
saveField = new JTextField(10);
private JPanel textPanel, resultLabel, savePanel;
private JLabel result1 = new JLabel("result1"),
result2 = new JLabel("result2");
private double discr, ans1, ans2;
public static void main(String[] args) {
new QuadraticGui().setVisible(true);
}
public QuadraticGui() {
initComponents();
}
private void initComponents() {
Container contentPane = getContentPane();
setSize(350,200);
setTitle("Quadratic Calculator");
calculate.addActionListener(this);
save.addActionListener(this);
a1.setText("1.0");
b1.setText("4.0");
c1.setText("2.0");
saveField.setText("outputs.txt");
textPanel = new JPanel(new FlowLayout());
resultLabel = new JPanel(new FlowLayout());
savePanel = new JPanel();
textPanel.add(a1);
textPanel.add(b1);
textPanel.add(c1);
textPanel.add(calculate);
//textPanel.add(saveField);
//textPanel.add(save);
savePanel.add(saveField);
savePanel.add(save);
resultLabel.add(result1);
resultLabel.add(result2);
contentPane.add(textPanel, BorderLayout.NORTH);
contentPane.add(savePanel, BorderLayout.SOUTH);
contentPane.add(resultLabel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String filename = saveField.getText();
if(e.getSource()==calculate) {
double a = Double.parseDouble(a1.getText());
double b = Double.parseDouble(b1.getText());
double c = Double.parseDouble(c1.getText());
discr = Math.sqrt((b * b) - (4 * a * c));
if(discr > 0) {
ans1 = (-b + discr)/2 * a;
ans2 = (-b - discr)/2 * a;
}
result1.setText("Answer 1: " + ans1);
result2.setText("Answer 2: " + ans2);
} if(e.getSource()==save) {
writeToFile(result1, result2, filename);
}
}
public void writeToFile(JLabel l, JLabel l1, String filename) {
try {
FileWriter writer = new FileWriter(filename);
writer.write(l.getText());
writer.write(l1.getText());
writer.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}