4 Replies - 1415 Views - Last Post: 29 November 2006 - 08:25 PM Rate Topic: -----

#1 Cannix75   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-November 06

Help w/ programmging Writing to and reading from file

Posted 29 November 2006 - 10:21 AM

This is what i'm trying to do:
Generate a file with a random number of circle objects
each with a random radius.
Read from the file and compute average area.
Using exception handling, no detection of end of file.

but i keep getting an error on
 		output.write(radius[curCircle]);



the error is
java:18: cannot resolve symbol
symbol : method write (double)
location: class java.io.FileOutputStream
output.write(radius[curCircle]);
^
1 error


import java.io.*;
public class TestRandomCircle{

	public static void main(String[] args) throws IOException{

	FileOutputStream output = new FileOutputStream("temp.dat");


		double radius[] = new double[20];

		for(int curCircle = 0; curCircle < 20; curCircle++){

		radius[curCircle] = Math.random();


		output.write(radius[curCircle]);

		output.close();
		}





	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Help w/ programmging Writing to and reading from file

#2 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Help w/ programmging Writing to and reading from file

Posted 29 November 2006 - 10:35 AM

that's because it only accepts ints or char[] as output.
If you want to use a FileOutputStream you will need to convert it to a String then use .toCharArray() and then write the result.

or create a Double object instead of the double primative and use you can use my example under: Output an object
Was This Post Helpful? 0
  • +
  • -

#3 Cannix75   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-November 06

Re: Help w/ programmging Writing to and reading from file

Posted 29 November 2006 - 10:47 AM

View PostWilliam_Wilson, on 29 Nov, 2006 - 10:35 AM, said:

that's because it only accepts ints or char[] as output.
If you want to use a FileOutputStream you will need to convert it to a String then use .toCharArray() and then write the result.

or create a Double object instead of the double primative and use you can use my example under: Output an object

ya i changed it to objectoutputstream because i am reading the circle objects from my circle class. Thx for the help tho
Was This Post Helpful? 0
  • +
  • -

#4 Cannix75   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-November 06

Re: Help w/ programmging Writing to and reading from file

Posted 29 November 2006 - 03:17 PM

Ok now i have run into a problem with creating my random circle objects.

import java.io.*;
public class TestRandomCircle{

	public static void main(String[] args) throws IOException{

	ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("object.dat"));


		double radius[] = new double[20];

		for(int curCircle = 0; curCircle < 20; curCircle++)
		{
			radius[curCircle] = Math.random()*10;

			output.writeDouble(radius[curCircle]);
		}


		Circle cCircles[] = new Circle[20];
		double totalRad = 0;
		for(int testCirlce = 0; testCircle < 20; testCircle++)
		{
			cCircles[testCircle] = new Circle(radius);
			totalRad+= radius;
			output.writeDouble(cCircles[testCircle]);
		}

		double avgRad = totalRad / 20;


		//ObjectInputStream input = new ObjectInputStream(new FileInputStream("object.dat"));

		//double [] newRadius = (double [])(input.readObject());


	}
}



i get a syntex error that it does not reconize circle this is my circle class

public class Circle
{
	public double radius = 0.0;
	public Circle()
	{

	}

	public Circle(double in_Rad)
	{
		radius = in_Rad;
	}


	public void setRad(double in_Rad)
	{
		radius = in_Rad;
	}

	public double getRad()
	{
		return radius;
	}


}



also i do not know how to get input fromt he file once i write to it.
Was This Post Helpful? 0
  • +
  • -

#5 salindor   User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 304
  • Joined: 10-November 06

Re: Help w/ programmging Writing to and reading from file

Posted 29 November 2006 - 08:25 PM

I know dumb question, but you got to ask it at least once.

are the two source files in the same folder and is the class path set to include the current directory?

If not, you need to set the class path to each directory (assuming you don't want to deal with packaging).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1