How would i do this....
I want num to be a random number between 0 and .99999, and it has to print and write 10 of them.
hm.
{
double num = Math.random();
publicvoid writeData()
{
FileWriter f1 =new FileWriter("random.txt");
if (num >= 0 || num <=.99999)
{
for (int i=1; i<10; i++)
{
f1.write(num);
System.out.println(num);
}
}
}
[851 byte] By [
DenisKa] at [2007-11-27 7:35:27]

1) Next time use a more meaningful subject line.2) What exact problem are you having?
jverda at 2007-7-12 19:15:58 >

does everything look right there?this is what i get .FileProcessing.java:20: cannot find symbolsymbol : method write(double)location: class java.io.FileWriterf1.write(num); ^1 errorPress any key to continue . . .
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html
> FileProcessing.java:20: cannot find symbol
> symbol : method write(double)
> location: class java.io.FileWriter
> f1.write(num);
>^
> error
> Press any key to continue . . .
It's telling you exactly what's wrong. At line 20 of FileProcessing.java, you're trying to call a method in a FileWriter that doesn't exist. FileWriter has no write(double) method.
jverda at 2007-7-12 19:15:58 >

So you need to call one of the methods that does exist.For instance, write(String.valueOf(num)).Though I'd suggest wrapping that FileWriter in a PrintWriter and using a print or println method.
jverda at 2007-7-12 19:15:58 >

Whoops, didn't notice you already had the random part working. Try reading a tutorial to help you out. http://java.sun.com/docs/books/tutorial/essential/io/
How do i wrap a FileWriter into a PrintWriter?somethig like this?pW = new PrintWriter( new FileWriter( "output4.txt" ) )
> How do i wrap a FileWriter into a PrintWriter?> somethig like this?> pW = new PrintWriter( new FileWriter( "output4.txt" )> )Yup. :-)
jverda at 2007-7-12 19:15:58 >

And if you check out PW's docs, you'll see it has methods for printing and println-in all the primitive types
jverda at 2007-7-12 19:15:58 >

i'm getting a cannot find symbol when i used that line. I'm created it when i use this line, aren't i?
file = new PrintWriter(new FileWriter("random.txt"));
FileProcessing.java:15: cannot find symbol
symbol : variable file
location: class FileProcessing
file = new PrintWriter(new FileWriter("random.txt"));
^
You have to declare file's type. The error message is tell you the exact problem--it doesn't know what "file" is because you haven't told it.PrintWriter file = ...Also, file is not a great name for a PrintWriter.
jverda at 2007-7-12 19:15:58 >

now i have a the same double method error.i saw this from you: "For instance, write(String.valueOf(num))."but don't understand it?
Did you look at the docs for PrintWriter?
jverda at 2007-7-12 19:15:58 >

print. instead of write. got cha.
cool :-)
jverda at 2007-7-21 22:17:24 >

added the throws, hows it look?
double num = Math.random();
public void writeData() throws IOException
{
PrintWriter pW = new PrintWriter(new FileWriter("random.txt"));
if (num >= 0 || num <=.99999)
{
for (int i=1; i<10; i++)
{
pW.print(num);
System.out.println(num);
}
}
}
Your if statement is pointless (it will always be true, anyway). Also, you are taking one random number and printing it ten times. Don't you want to print ten different random numbers?
yeah i do, isn't the for loop doing that? i guess i should change the or operator to an AND?
> yeah i do, isn't the for loop doing that?
The for loop is taking the random number you made and printing it ten times. You need to set num to a new random number in the loop.
> i guess i
> should change the or operator to an AND?
You could, but [url=http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#random()]Math.random()[/url] will always return a number between 0 (inclusive) and 1 (exclusive).
should i just Math.Random(num) or num++?
> should i just Math.Random(num) or num++?Why not try each and see? Do you have access to a compiler?
0.3729156571687533
1.3729156571687533
2.372915657168753
3.372915657168753
4.372915657168753
5.372915657168753
6.372915657168753
7.372915657168753
8.372915657168754
thats with num++..
this is with num = Math.random()
0.6723590899096302
0.248535991672175
0.6762380773686422
0.5740173978763413
0.7918507097449482
0.9116662958972523
0.4572605335927178
0.4043243994741199
0.8040561948675174
#2 for the win :)
public void readData() throws IOException
{
FileReader fr = new FileReader("random.txt");
BufferedReader br = new BufferedReader(fr);
String inRecord, number;
while ((inRecord = br.readLine()) != null)
{
StringTokenizer tokenizer = new StringTokenizer(inRecord);
number = tokenizer.nextToken();
System.out.println(number);
}
}
Shouldn't this print? i do realize StringTokenizer it used to print letters or whatever after a delimeter, but can't it work in this situation?
> Shouldn't this print?What happens when you run it?
What is in random.txt? Are you sure no exception is being thrown?
Well the writeData writes to random.txt, then i'm suppost to read it and print it on console.hm.. yeah it's not writing to random.txt.
Not sure why it's not writing to the file.. it creates it. but makes it empty
double num = Math.random();
public void writeData() throws IOException
{
PrintWriter pW = new PrintWriter(new FileWriter("random.txt"));
if (num >= 0 && num <=.99999)
{
for (int i=1; i<10; i++)
{
pW.print(num);
num = Math.random();
}
}
}
public void readData() throws IOException
{
FileReader fr = new FileReader("random.txt");
BufferedReader br = new BufferedReader(fr);
String inRecord, number;
while ((inRecord = br.readLine()) != null)
{
StringTokenizer tokenizer = new StringTokenizer(inRecord);
number = tokenizer.nextToken();
System.out.println(number);
}
}
What is in random.txt? Have you looked in the file to verify this?
Are you sure no exception is being thrown?
This demo runs your read method, and it works for me.
import java.io.*;
import java.util.*;
public class IOExample {
public static void main(String[] args) throws IOException {
String filename = "random.txt";
write(filename);
read(filename);
}
public static void write(String filename) throws IOException {
PrintWriter out = new PrintWriter(filename);
try {
for(int i = 0; i < 10; ++i)
out.println(i);
} finally {
out.close();
}
}
public static void read(String filename) throws IOException {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String inRecord, number;
while ((inRecord = br.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(inRecord);
number = tokenizer.nextToken();
System.out.println(number);
}
}
}
> hm.. yeah it's not writing to random.txt.Do you .close() the stream? You should always close streams. It could be that your stream is buffering output which is being lost because closing will flush the buffer...
Yup, you forgot to close. The test for num in range doesn't make a whole lot of sense, by the way. What if it is false? Then no output?
You're right, it was the close. Soon as i closed it. it works. It's always the small things. :(
> Yup, you forgot to close. The test for num in range> doesn't make a whole lot of sense, by the way. What> if it is false? Then no output?The test for num doesn't make sense? The if you mean? It works?.. lol.
While you're at it, you should close the stream in your input method.Forgetting to close it isn't as bad as fir output streams, but it's good hygiene.
>The test for num doesn't make sense? The if you mean? It works?.. lol.Well, I suppose it works *nearly* all the time...
> While you're at it, you should close the stream in
> your input method.
> Forgetting to close it isn't as bad as fir output
> streams, but it's good hygiene.
That i did, Thank you sir :)
public void writeData() throws IOException
{
PrintWriter pW = new PrintWriter("random.txt");
if (num >= 0 && num <=.99999)
{
for (int i=1; i<10; i++)
{
pW.print(num +" \n ");
num = Math.random();
}
}
pW.close();
}
> >The test for num doesn't make sense? The if you
> mean? It works?.. lol.
>
> Well, I suppose it works *nearly* all the time...
Yeah but i don't think the assignment really focuses on the numbers, it's mostly just writing.. reading files.
By the way, what should i use for deleting a file?
In this case. I write to a file, read it and display it, and now i have to delete it.
Have you looked at the API for file?
> i do realize StringTokenizer it> used to print letters or whatever after a delimeter,No. StringTokenizer is not used for printing anything.
jverda at 2007-7-21 22:17:29 >

Couple of points. Didn't you want 10 numbers? Your code will generate 9. Secondly, you if statement is totally pointless as others have pointed out. Yes it works becuase it will always be true. Try this code instead.
public void writeData() throws IOException {
int num = -1;
PrintWriter pW = new PrintWriter("random.txt");
for (int i = 0; i < 10; i++) {
num = Math.random();
pW.print(num +" \n ");
}
pW.close();
}
> Yes it works becuase it will always be true.Actually I take that back. A small percentage of the time it will be false.
Thanks flounder. I'm trying to delete the file now but it gives me this.
try {
pW.delete();
System.out.println("Successfully deleted");
}
catch (SecurityException e)
{
System.out.println("Caught security exception trying to delete file");
}
Test.java:36: cannot find symbol
symbol : method delete()
location: class java.io.PrintWriter
pW.delete();
^
.
You should understand this error message by now. It's telling you that PrintWriter does not have a delete method. You need java.io.File.
jverda at 2007-7-21 22:17:29 >

RIght, but my files called pW. How else can i delete it? I'm using the File.Delete method.
> ava:36: cannot find symbol
> symbol : method delete()
> location: class java.io.PrintWriter
> pW.delete();
>^
http://java.sun.com/javase/6/docs/api/java/io/PrintWriter.html
I don't know why you'd try to use a delete() method of PrintWriter, since no such method exists. Did you mean to call delete() on a File reference instead?
~
Im confused because my file was created from pW.PrintWriter pW = new PrintWriter("random.txt");Thats the creation of the file, and i have to delete with File.Delete(). But don't know how?
File f = new File("random.txt");f.delete(); http://java.sun.com/docs/books/tutorial/essential/io/~
0.5452577623853672
0.4867849123685448
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at Test.main(Test.java:32)
Press any key to continue . . .
Doesn't print out the print statement.. and doesn't delete.
try {
File f = new File("random.txt");
f.delete();
System.out.println("Successfully deleted");
}
catch (SecurityException e)
{
System.out.println("Caught security exception trying to delete file");
}
> Doesn't print out the print statement.. and doesn'tRead the error message! You're calling nextToken() on a StringTokenizer object when there are no more tokens!~
Oh man, sorry,
heres the code, but when i take off the tokenizer stuff, it says number hasn't been instantiated, if i instantiate it with a value then it prints out the value on each.
FileReader fr = new FileReader("random.txt");
BufferedReader br = new BufferedReader(fr);
String inRecord, number;
while ((inRecord = br.readLine()) != null)
{
StringTokenizer tokenizer = new StringTokenizer(inRecord);
number = tokenizer.nextToken();
System.out.println(number);
}
> if i instantiate it with a value then it prints out the value on each.Your code is doing exactly what you tell it to. What's the problem?~
i want it to read the data from random.txt and print it out.
> i want it to read the data from random.txt and print it out.Okay. That's not a problem.~
yeah but i'm getting the tokenizer error. :(
> yeah but i'm getting the tokenizer error. :(
As mentioned previously, it's because you're calling nextElement() on a StringTokenizer that has no more elements. I recommend you have a look at the API javadocs for StringTokenizer and see if you can find a method that will ask the StringTokenizer object if it has any more elements. Only if it answers affirmatively should you call the nextElement() method.
~
nvm i got it. removed the tokenizer and did this.while ((number = br.readLine()) != null)Sheesh, i'm an idiot.
> Sheesh, i'm an idiot.Was there a reason you were using StringTokenizer in the first place?~
Nope. If i had to input 2 variables in the random.txt then it would have been put to use. But i have no idea why it was there in the first place.