intro and ? on writing double to file as char

P

picaza

hi,
i am trying to learn java and i am sitting in on a couple of classes at
my alma...(btw: i am not a for credit student so these q's are not to
get out of homework). this seemingly simple point has me stuck. i want
to write out several "doubles" to a file such that they appear as
char/string. i have tried various castings to no avail. here is the
latest code i have. any hints?

also i can use any dev tool i wish and i choose eclipse because it seems
to be well used in the job market. would you concur that eclipse is the
best ide to use?

thanks,
peter, unemployeed UNIX/C guy
+++++++++++++++++++++++++++


public void GenerateRamdomDoubleNumbers(int howMany, String fName)
{
File outFile = new File(fName);
Double number;

FileWriter fw = new FileWriter(outFile);
try
{
for (int x = 0; x<howMany; x++)
{
number = (Double)( 1 + (Math.random() * howMany) );
fw.write((number.toString()) );
}
}
catch ( IOException e)
{
}
}
 
K

klynn47

I would create a PrintWriter associated with the FileWriter, and then
use the method println.

PrintWriter output = new PrintWriter(fw);

for (int x=0;x<howMany;x++)
output.println(1+Math.random()*howMany);

output.close();
 
S

Steve W. Jackson

picaza said:
hi,
i am trying to learn java and i am sitting in on a couple of classes at
my alma...(btw: i am not a for credit student so these q's are not to
get out of homework). this seemingly simple point has me stuck. i want
to write out several "doubles" to a file such that they appear as
char/string. i have tried various castings to no avail. here is the
latest code i have. any hints?

also i can use any dev tool i wish and i choose eclipse because it seems
to be well used in the job market. would you concur that eclipse is the
best ide to use?

thanks,
peter, unemployeed UNIX/C guy
+++++++++++++++++++++++++++


public void GenerateRamdomDoubleNumbers(int howMany, String fName)
{
File outFile = new File(fName);
Double number;

FileWriter fw = new FileWriter(outFile);
try
{
for (int x = 0; x<howMany; x++)
{
number = (Double)( 1 + (Math.random() * howMany) );
fw.write((number.toString()) );
}
}
catch ( IOException e)
{
}
}

There's one critical flaw, which is that the "Double" is an object while
"double" is a primitive type. So the computation is going to produce a
value of type "double" which cannot then be cast to Double.

Also, as a matter of convention, class names should always begin with
capital letters, while method and variable names should always begin
with lower case letters, so you should rename the above method to fit
with convention. FYI.

And I think I concur with another respondent that this particular kind
of output is probably best done using a PrintWriter.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top