Start New Discussion within our Software Development Community

hi,

i have a TextField in myGUI where some data(results) are shown after calculation. I have a 'save' button to save this result into a particular txt file..

i almost done without error....but inside my txt file it shows this kind of things:

ava.awt.datatransfer.StringSelection@157f0dc

Please anybody help me to figure out...

else if (e.getSource()==saveButton){
 
 int x=Integer.parseInt(inputField.getText());
 String str=Integer.toString(x*x);
 resultField.setText(str)  
  
       try {
	
// String selection =selection.getSelectedText();
  String selection=resultField.getText();
  StringSelection data= new StringSelection(selection);
  
File outFile= new File("c:/Java/math.txt");
FileOutputStream fos=new FileOutputStream(outFile);
PrintWriter pr=new PrintWriter(fos);

pr.print(data);
pr.close();}

catch (IOException ioe) {	
     ioe.printStackTrace();
     System.exit(0);}

Why do you need a StringSelection Object? You should just need the String, i.e. the one that is stored in your variable called 'selection'.

Btw, the error is that you're using PrintWriter's "print" method, and you are passing it a StringSelection Object. But if you look at the class documentation for PrintWriter, no such method exists. However, PrintWriter does have a method that takes a String, so you should get rid of the StringSelection variable and do:

pr.print(selection);

http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintWriter.html
^ Here is the PrintWriter javadoc, take a look. No "print" method exists that takes a StringSelection Object as a param.

This question has already been answered. Start a new discussion instead.