954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem facing with StringSelection to save into txt file

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);}
doha786
Light Poster
33 posts since Mar 2009
Reputation Points: 10
Solved Threads: 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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You