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]
# 1

I really dont know what you are talking about. The >> operator means shifting on primitive types. It has nothing to do with writing text files.

To solve you problem, you need some kind of Writer. You can try something like this:

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));

out.println("foobar");

out.close;

bytoa at 2007-7-29 14:20:50 > top of Java-index,Java Essentials,New To Java...
# 2

lol i love it when people just copy my code :Pp

http://forum.java.sun.com/thread.jspa?threadID=5197013&messageID=9778686#9778686

anyways do you know what this part of code is doing?

private Quadratic quadratic; //what is this? :)

Hint remove it before the teacher catch you. my methadology was not to write a program so you can copy. My intentions of writing the code was so that you can learn something from it. anyways its your choice. Do you know anything about fileinput and output?

lrngjavaa at 2007-7-29 14:20:50 > top of Java-index,Java Essentials,New To Java...
# 3

instantiating an object of quadratic? or is that a variable declaration?

pberardi1a at 2007-7-29 14:20:50 > top of Java-index,Java Essentials,New To Java...
# 4

> instantiating an object of quadratic? or is that a

> variable declaration?

no object is created yet. so remove it.

lrngjavaa at 2007-7-29 14:20:50 > top of Java-index,Java Essentials,New To Java...
# 5

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

}

}

}

lrngjavaa at 2007-7-29 14:20:50 > top of Java-index,Java Essentials,New To Java...
# 6

> I really dont know what you are talking about. The >>

> operator means shifting on primitive types. It has

> nothing to do with writing text files.

>

He's obviously a C++ programmer. the >> and << operators are overloaded in C++, they can be used to redirect data to streams. I expect that's what he's trying to do. OP, this is Java, not C++!

georgemca at 2007-7-29 14:20:50 > top of Java-index,Java Essentials,New To Java...
# 7

Your program produces no output, so you can't redirect it anywhere.

Are you sure you are supposed to be writing a GUI instead of a program that uses the standard streams (System.in, System.out)?

System.out.println("This is a test");

starfalla at 2007-7-29 14:20:50 > top of Java-index,Java Essentials,New To Java...
# 8

> He's obviously a C++ programmer. the >> and <<

> operators are overloaded in C++, they can be used to

> redirect data to streams. I expect that's what he's

> trying to do. OP, this is Java, not C++!

I would say he's obviously trying to redirect System.out to a file via the command line.

His problem is (as has been stated) that his program doesn't produce any output via System.out.

dwga at 2007-7-29 14:20:50 > top of Java-index,Java Essentials,New To Java...