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
1) Next time use a more meaningful subject line.2) What exact problem are you having?
jverda at 2007-7-12 19:15:58 > top of Java-index,Java Essentials,New To Java...
# 2
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 . . .
DenisKa at 2007-7-12 19:15:58 > top of Java-index,Java Essentials,New To Java...
# 3
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html
CaptainMorgan08a at 2007-7-12 19:15:58 > top of Java-index,Java Essentials,New To Java...
# 4

> 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 > top of Java-index,Java Essentials,New To Java...
# 5
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 > top of Java-index,Java Essentials,New To Java...
# 6
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/
CaptainMorgan08a at 2007-7-12 19:15:58 > top of Java-index,Java Essentials,New To Java...
# 7
How do i wrap a FileWriter into a PrintWriter?somethig like this?pW = new PrintWriter( new FileWriter( "output4.txt" ) )
DenisKa at 2007-7-12 19:15:58 > top of Java-index,Java Essentials,New To Java...
# 8
> 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 > top of Java-index,Java Essentials,New To Java...
# 9
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 > top of Java-index,Java Essentials,New To Java...
# 10

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"));

^

DenisKa at 2007-7-12 19:15:58 > top of Java-index,Java Essentials,New To Java...
# 11
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 > top of Java-index,Java Essentials,New To Java...
# 12
got cha.
DenisKa at 2007-7-12 19:15:58 > top of Java-index,Java Essentials,New To Java...
# 13
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?
DenisKa at 2007-7-12 19:15:58 > top of Java-index,Java Essentials,New To Java...
# 14
Did you look at the docs for PrintWriter?
jverda at 2007-7-12 19:15:58 > top of Java-index,Java Essentials,New To Java...
# 15
print. instead of write. got cha.
DenisKa at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 16
cool :-)
jverda at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 17

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);

}

}

}

DenisKa at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 18
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?
CaptainMorgan08a at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 19
yeah i do, isn't the for loop doing that? i guess i should change the or operator to an AND?
DenisKa at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 20

> 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).

CaptainMorgan08a at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 21
should i just Math.Random(num) or num++?
DenisKa at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 22
> should i just Math.Random(num) or num++?Why not try each and see? Do you have access to a compiler?
Hippolytea at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 23

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 :)

DenisKa at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 24

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?

DenisKa at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 25
> Shouldn't this print?What happens when you run it?
Hippolytea at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 26
Nothing.
DenisKa at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 27
What is in random.txt? Are you sure no exception is being thrown?
Hippolytea at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 28
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.
DenisKa at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 29

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);

}

}

DenisKa at 2007-7-21 22:17:24 > top of Java-index,Java Essentials,New To Java...
# 30

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);

}

}

}

Hippolytea at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 31
> 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...
Hippolytea at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 32
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?
Hippolytea at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 33
You're right, it was the close. Soon as i closed it. it works. It's always the small things. :(
DenisKa at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 34
> 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.
DenisKa at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 35
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.
Hippolytea at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 36
>The test for num doesn't make sense? The if you mean? It works?.. lol.Well, I suppose it works *nearly* all the time...
Hippolytea at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 37

> 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();

}

DenisKa at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 38

> >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.

DenisKa at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 39
Have you looked at the API for file?
Hippolytea at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 40
> 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 > top of Java-index,Java Essentials,New To Java...
# 41

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();

}

floundera at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 42
> Yes it works becuase it will always be true.Actually I take that back. A small percentage of the time it will be false.
floundera at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 43

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();

^

.

DenisKa at 2007-7-21 22:17:29 > top of Java-index,Java Essentials,New To Java...
# 44
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 > top of Java-index,Java Essentials,New To Java...
# 45
RIght, but my files called pW. How else can i delete it? I'm using the File.Delete method.
DenisKa at 2007-7-21 22:17:34 > top of Java-index,Java Essentials,New To Java...
# 46

> 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?

~

yawmarka at 2007-7-21 22:17:34 > top of Java-index,Java Essentials,New To Java...
# 47
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?
DenisKa at 2007-7-21 22:17:34 > top of Java-index,Java Essentials,New To Java...
# 48
File f = new File("random.txt");f.delete(); http://java.sun.com/docs/books/tutorial/essential/io/~
yawmarka at 2007-7-21 22:17:34 > top of Java-index,Java Essentials,New To Java...
# 49

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");

}

DenisKa at 2007-7-21 22:17:34 > top of Java-index,Java Essentials,New To Java...
# 50
> 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!~
yawmarka at 2007-7-21 22:17:34 > top of Java-index,Java Essentials,New To Java...
# 51

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);

}

DenisKa at 2007-7-21 22:17:35 > top of Java-index,Java Essentials,New To Java...
# 52
> 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?~
yawmarka at 2007-7-21 22:17:35 > top of Java-index,Java Essentials,New To Java...
# 53
i want it to read the data from random.txt and print it out.
DenisKa at 2007-7-21 22:17:35 > top of Java-index,Java Essentials,New To Java...
# 54
> i want it to read the data from random.txt and print it out.Okay. That's not a problem.~
yawmarka at 2007-7-21 22:17:35 > top of Java-index,Java Essentials,New To Java...
# 55
yeah but i'm getting the tokenizer error. :(
DenisKa at 2007-7-21 22:17:35 > top of Java-index,Java Essentials,New To Java...
# 56

> 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.

~

yawmarka at 2007-7-21 22:17:35 > top of Java-index,Java Essentials,New To Java...
# 57
nvm i got it. removed the tokenizer and did this.while ((number = br.readLine()) != null)Sheesh, i'm an idiot.
DenisKa at 2007-7-21 22:17:35 > top of Java-index,Java Essentials,New To Java...
# 58
> Sheesh, i'm an idiot.Was there a reason you were using StringTokenizer in the first place?~
yawmarka at 2007-7-21 22:17:35 > top of Java-index,Java Essentials,New To Java...
# 59
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.
DenisKa at 2007-7-21 22:17:35 > top of Java-index,Java Essentials,New To Java...