6 Replies - 666 Views - Last Post: 26 February 2010 - 04:59 AM Rate Topic: -----

#1 Guest_Milo*


Reputation:

Write result of calculation to file

Posted 26 February 2010 - 02:42 AM

I have made a simple calculation, and the result is printed out fine.
But I would like to store the result in an external file. This file is created ok, but the result is not shown in the file.
What is wrong?
Thank you.

import java.util.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.math.*;


public class Addition {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int n = 5;
		int k = 7;
		
		int result = n+k;
		
		System.out.println( + result );
		
		
		
		try{
		    // Create file 
		    FileWriter fstream = new FileWriter("calculation.txt");
		        BufferedWriter out = new BufferedWriter(fstream);
		    out.write(result);
		    //Close the output stream
		    out.close();
		    }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		    }		

	}

}





Is This A Good Question/Topic? 0

Replies To: Write result of calculation to file

#2 aniri   User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Re: Write result of calculation to file

Posted 26 February 2010 - 02:50 AM

You need to convert the result to string before writing to the file.

Try
out.write(""+result);

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Milo*


Reputation:

Re: Write result of calculation to file

Posted 26 February 2010 - 02:55 AM

You are right:)
Thank you.
Was This Post Helpful? 0

#4 Guest_Milo*


Reputation:

Re: Write result of calculation to file

Posted 26 February 2010 - 03:08 AM

In case there are more results, is there a way also of separating (with the "little square") the results that are saved in the file?


import java.util.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.math.*;


public class Addition {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int n = 28;
		int k = 7;
		
		int result = n+k;
		
		int result_1 = n -k;
		
		System.out.println( + result );
		System.out.println( + result_1 );
		
		
		try{
		    // Create file 
		    FileWriter fstream = new FileWriter("calculation.txt");
		        BufferedWriter out = new BufferedWriter(fstream);
		        out.write(""+result);
		        out.write(""+result_1);
		    //Close the output stream
		    out.close();
		    }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		    }		

	}

}



Was This Post Helpful? 0

#5 aniri   User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Re: Write result of calculation to file

Posted 26 February 2010 - 03:13 AM

You could use the "\n" (end of line) separator to print results on different lines in the file.

out.write(result+"\n");
out.write(result_1+"\n");


Was This Post Helpful? 0
  • +
  • -

#6 Guest_Milo*


Reputation:

Re: Write result of calculation to file

Posted 26 February 2010 - 03:23 AM

That is right.
Thank you.
Was This Post Helpful? 0

#7 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Write result of calculation to file

Posted 26 February 2010 - 04:59 AM

Don't hardcode linefeeds - use a PrintWriter
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1