0

hello there u said if any body need help u can help them
so plz i need ur help with this
i did a program in java gui (temperature converter) and i just need the program to print the result in a text file
so plz plz plz help me
the out put should look like this
welcome:
from f to c ="the result"
from c to f =" the result"
and if u can plz before friday

heres my code

/****************************************************************/
/* tconverter */
/* */
/****************************************************************/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Summary description for tconverter
*
*/

public class tconverter extends JFrame
{

String strFilePath = "TestFile.txt";

// Variables declaration
private JLabel jLabel1;
private JLabel jLabel2;
private JTextField jTextField1;
private JTextField jTextField2;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JButton jButton4;
private JPanel contentPane;
// End of variables declaration


public tconverter()
{
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//

this.setVisible(true);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent()
{
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jTextField1 = new JTextField();
jTextField2 = new JTextField();
jButton1 = new JButton();
jButton2 = new JButton();
jButton3 = new JButton();
jButton4 = new JButton();
contentPane = (JPanel)this.getContentPane();

//
// jLabel1
//
jLabel1.setText("convert what quantity?");
//
// jLabel2
//
jLabel2.setText("the result of convertation is");
//
// jTextField1
//
jTextField1.setText("");
jTextField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField1_actionPerformed(e);
}

});
//
// jTextField2
//
jTextField2.setText("");
jTextField2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField2_actionPerformed(e);
}

});
//
// jButton1
//
jButton1.setText("C");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}

});
//
// jButton2
//
jButton2.setText("F");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton2_actionPerformed(e);
}

});
//
// jButton3
//
jButton3.setText("Close");
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton3_actionPerformed(e);
}

});
//
// jButton4
//
jButton4.setText("About us...");
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton4_actionPerformed(e);
}

});
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 16,28,122,18);
addComponent(contentPane, jLabel2, 9,173,140,18);
addComponent(contentPane, jTextField1, 175,24,196,22);
addComponent(contentPane, jTextField2, 177,167,196,23);
addComponent(contentPane, jButton1, 20,90,83,28);
addComponent(contentPane, jButton2, 215,91,83,28);
addComponent(contentPane, jButton3, -1,247,83,28);
addComponent(contentPane, jButton4, 291,249,93,28);
//
// tconverter
//
this.setTitle("tconverter - extends JFrame");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(390, 300));
}

/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}

//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void jTextField1_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

}

private void jTextField2_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField2_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

}

private void jButton1_actionPerformed(ActionEvent e)
{
System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

int tempCele = (int)(((Double.parseDouble(jTextField1.getText()))- 32) * 0.56); 


jTextField2.setText(tempCele + " C");




}

private void jButton2_actionPerformed(ActionEvent e)
{
System.out.println("\njButton2_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
int tempFahr = (int)((Double.parseDouble(jTextField1.getText()))
* 1.8 + 32);



jTextField2.setText(tempFahr + " Fahrenheit");


}

private void jButton3_actionPerformed(ActionEvent e)
{
System.out.println("\njButton3_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

String t = "Thank You";
JOptionPane.showMessageDialog(null, " Thank You for using our Temperature converter");

System.exit(0);
}

private void jButton4_actionPerformed(ActionEvent e)
{
System.out.println("\njButton4_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
String t = "About me";
JOptionPane.showMessageDialog(null, "This is a Temperature converter tool");
}

//
// TODO: Add any method code to meet your needs in the following area
//
































//============================= Testing ================================//
//= =//
//= The following main method is just for testing this class you built.=//
//= After testing,you may simply delete it. =//
//======================================================================//
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new tconverter();
}
//= End of Testing =



}

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

4
Contributors
13
Replies
14
Views
7 Years
Discussion Span
Last Post by bahnini
0

Okay? So, as a simple soultion, open a PrintStream to a file and set that to System.out. As a slightly better solution, open a FileWriter and set that as an instance variable and have the actionlisteners write to that rather than system. out, as a thid solution, but one which the instructor probably doesn't want to see, simply redirect standard out on the command line. There are much more elegant solutions, but any of these will work. At least try.

0

thank you for your reply but i don't know how to do it
im still bigenner in java so can you plz add it to the code
i really apreciate it

0

sorry about that but i have aquestion
i've found a code of i/o and it has a try and cacth in it
my question is where i should put the try and catch in the previous code that i put

0

Every place where you write to the file, and the place where you open the file. I would also say to add a windowClosing method (which means using your own WindowListener) or a shutdown hook to close the file, but that is probably too much at the moment, and shouldn't be necessary, as it should automatically close, normally, when the program exits (but call flush after every write, to be sure).

0

this is the code that i have
but the problem is i dont know where to put it or how to use it with my program

and for the suggestions that u proposed we didnot take them and i really don't know how to use them cus im not programming student it's just a cource im taking in the university
this is the code:

import java.io.*;
public class MyClass {
public static void main(String[] args) {
String strFilePath = "TestFile.txt";
try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
String strContent = "Write File using Java FileOutputStream example
!";
fos.write(strContent.getBytes());
fos.close();
}
catch(FileNotFoundException ex)
{

System.out.println("FileNotFoundException : " + ex);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
}
}

0

If all that u need is to write some data into a text file, then here you go..

try
{
FileWriter writer = new FileWriter(new File("H:\\Madhusudhan\\Scripts\\AJADropDown\\Sample.txt"));
BufferedWriter buff = new BufferedWriter(writer);
buff.write("Hello World");
buff.flush();
buff.close();
return (1*2);
}catch(Exception e)
{

}

hello there u said if any body need help u can help them
so plz i need ur help with this
i did a program in java gui (temperature converter) and i just need the program to print the result in a text file
so plz plz plz help me
the out put should look like this
welcome:
from f to c ="the result"
from c to f =" the result"
and if u can plz before friday

heres my code

/****************************************************************/
/* tconverter */
/* */
/****************************************************************/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Summary description for tconverter
*
*/

public class tconverter extends JFrame
{

String strFilePath = "TestFile.txt";

// Variables declaration
private JLabel jLabel1;
private JLabel jLabel2;
private JTextField jTextField1;
private JTextField jTextField2;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JButton jButton4;
private JPanel contentPane;
// End of variables declaration


public tconverter()
{
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//

this.setVisible(true);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent()
{
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jTextField1 = new JTextField();
jTextField2 = new JTextField();
jButton1 = new JButton();
jButton2 = new JButton();
jButton3 = new JButton();
jButton4 = new JButton();
contentPane = (JPanel)this.getContentPane();

//
// jLabel1
//
jLabel1.setText("convert what quantity?");
//
// jLabel2
//
jLabel2.setText("the result of convertation is");
//
// jTextField1
//
jTextField1.setText("");
jTextField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField1_actionPerformed(e);
}

});
//
// jTextField2
//
jTextField2.setText("");
jTextField2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField2_actionPerformed(e);
}

});
//
// jButton1
//
jButton1.setText("C");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}

});
//
// jButton2
//
jButton2.setText("F");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton2_actionPerformed(e);
}

});
//
// jButton3
//
jButton3.setText("Close");
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton3_actionPerformed(e);
}

});
//
// jButton4
//
jButton4.setText("About us...");
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton4_actionPerformed(e);
}

});
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 16,28,122,18);
addComponent(contentPane, jLabel2, 9,173,140,18);
addComponent(contentPane, jTextField1, 175,24,196,22);
addComponent(contentPane, jTextField2, 177,167,196,23);
addComponent(contentPane, jButton1, 20,90,83,28);
addComponent(contentPane, jButton2, 215,91,83,28);
addComponent(contentPane, jButton3, -1,247,83,28);
addComponent(contentPane, jButton4, 291,249,93,28);
//
// tconverter
//
this.setTitle("tconverter - extends JFrame");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(390, 300));
}

/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}

//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void jTextField1_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

}

private void jTextField2_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField2_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

}

private void jButton1_actionPerformed(ActionEvent e)
{
System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

int tempCele = (int)(((Double.parseDouble(jTextField1.getText()))- 32) * 0.56); 


jTextField2.setText(tempCele + " C");




}

private void jButton2_actionPerformed(ActionEvent e)
{
System.out.println("\njButton2_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
int tempFahr = (int)((Double.parseDouble(jTextField1.getText()))
* 1.8 + 32);



jTextField2.setText(tempFahr + " Fahrenheit");


}

private void jButton3_actionPerformed(ActionEvent e)
{
System.out.println("\njButton3_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

String t = "Thank You";
JOptionPane.showMessageDialog(null, " Thank You for using our Temperature converter");

System.exit(0);
}

private void jButton4_actionPerformed(ActionEvent e)
{
System.out.println("\njButton4_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
String t = "About me";
JOptionPane.showMessageDialog(null, "This is a Temperature converter tool");
}

//
// TODO: Add any method code to meet your needs in the following area
//
































//============================= Testing ================================//
//= =//
//= The following main method is just for testing this class you built.=//
//= After testing,you may simply delete it. =//
//======================================================================//
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new tconverter();
}
//= End of Testing =



}
0

If all that u need is to write some data into a text file, then here you go..

try
{
FileWriter writer = new FileWriter(new File("Specify the full path of the file\\Sample.txt"));
BufferedWriter buff = new BufferedWriter(writer);
buff.write("Put the content of the file here");
buff.flush();
buff.close();
}catch(Exception e)
{
// Handle the exceptions here
}

hello there u said if any body need help u can help them
so plz i need ur help with this
i did a program in java gui (temperature converter) and i just need the program to print the result in a text file
so plz plz plz help me
the out put should look like this
welcome:
from f to c ="the result"
from c to f =" the result"
and if u can plz before friday

heres my code

/****************************************************************/
/* tconverter */
/* */
/****************************************************************/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Summary description for tconverter
*
*/

public class tconverter extends JFrame
{

String strFilePath = "TestFile.txt";

// Variables declaration
private JLabel jLabel1;
private JLabel jLabel2;
private JTextField jTextField1;
private JTextField jTextField2;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JButton jButton4;
private JPanel contentPane;
// End of variables declaration


public tconverter()
{
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//

this.setVisible(true);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent()
{
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jTextField1 = new JTextField();
jTextField2 = new JTextField();
jButton1 = new JButton();
jButton2 = new JButton();
jButton3 = new JButton();
jButton4 = new JButton();
contentPane = (JPanel)this.getContentPane();

//
// jLabel1
//
jLabel1.setText("convert what quantity?");
//
// jLabel2
//
jLabel2.setText("the result of convertation is");
//
// jTextField1
//
jTextField1.setText("");
jTextField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField1_actionPerformed(e);
}

});
//
// jTextField2
//
jTextField2.setText("");
jTextField2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField2_actionPerformed(e);
}

});
//
// jButton1
//
jButton1.setText("C");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}

});
//
// jButton2
//
jButton2.setText("F");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton2_actionPerformed(e);
}

});
//
// jButton3
//
jButton3.setText("Close");
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton3_actionPerformed(e);
}

});
//
// jButton4
//
jButton4.setText("About us...");
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton4_actionPerformed(e);
}

});
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 16,28,122,18);
addComponent(contentPane, jLabel2, 9,173,140,18);
addComponent(contentPane, jTextField1, 175,24,196,22);
addComponent(contentPane, jTextField2, 177,167,196,23);
addComponent(contentPane, jButton1, 20,90,83,28);
addComponent(contentPane, jButton2, 215,91,83,28);
addComponent(contentPane, jButton3, -1,247,83,28);
addComponent(contentPane, jButton4, 291,249,93,28);
//
// tconverter
//
this.setTitle("tconverter - extends JFrame");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(390, 300));
}

/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}

//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void jTextField1_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

}

private void jTextField2_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField2_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

}

private void jButton1_actionPerformed(ActionEvent e)
{
System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

int tempCele = (int)(((Double.parseDouble(jTextField1.getText()))- 32) * 0.56); 


jTextField2.setText(tempCele + " C");




}

private void jButton2_actionPerformed(ActionEvent e)
{
System.out.println("\njButton2_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
int tempFahr = (int)((Double.parseDouble(jTextField1.getText()))
* 1.8 + 32);



jTextField2.setText(tempFahr + " Fahrenheit");


}

private void jButton3_actionPerformed(ActionEvent e)
{
System.out.println("\njButton3_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

String t = "Thank You";
JOptionPane.showMessageDialog(null, " Thank You for using our Temperature converter");

System.exit(0);
}

private void jButton4_actionPerformed(ActionEvent e)
{
System.out.println("\njButton4_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
String t = "About me";
JOptionPane.showMessageDialog(null, "This is a Temperature converter tool");
}

//
// TODO: Add any method code to meet your needs in the following area
//
































//============================= Testing ================================//
//= =//
//= The following main method is just for testing this class you built.=//
//= After testing,you may simply delete it. =//
//======================================================================//
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new tconverter();
}
//= End of Testing =



}
0

thank for the reply
i've tried it and it worked but there is a problem ,the out put is displayed in one line no matter how many (buff.write()) i wrote
can you plz help me i want the out put to be in multiple lines or to be line breakers between the lines

thank you..

0

This will solve your problem. Use this for Getting the output on different lines.
try
{
FileWriter writer = new FileWriter(new File("Specify the full path of the file\\Sample.txt"));
BufferedWriter buff = new BufferedWriter(writer);
buff.write("Put the content of the file here");
buff.write(System.getProperty("line.separator"));
buff.flush();
buff.close();
}catch(Exception e)
{
// Handle the exceptions here
}

If all that u need is to write some data into a text file, then here you go..

try
{
FileWriter writer = new FileWriter(new File("Specify the full path of the file\\Sample.txt"));
BufferedWriter buff = new BufferedWriter(writer);
buff.write("Put the content of the file here");
buff.flush();
buff.close();
}catch(Exception e)
{
// Handle the exceptions here
}

0

This will solve your problem. Use this for Getting the output on different lines.
try
{
FileWriter writer = new FileWriter(new File("Specify the full path of the file\\Sample.txt"));
BufferedWriter buff = new BufferedWriter(writer);
buff.write("Put the content of the file here");
buff.write(System.getProperty("line.separator"));
buff.flush();
buff.close();
}catch(Exception e)
{
// Handle the exceptions here
}

thank for the reply
i've tried it and it worked but there is a problem ,the out put is displayed in one line no matter how many (buff.write()) i wrote
can you plz help me i want the out put to be in multiple lines or to be line breakers between the lines

thank you..

0

thank u all the problem and the program is solved
i knew i came to right place

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.