Reading in text and casting it

N

neosenate

Hi all,

I have to write a program that will take in a txt file containing a
number of floats, parse into individual numbers and then cast the
floats into binary, before writing all the binary numbers to another
file.

At the moment i've got

StreamTokenizer in = new StreamTokenizer(new
FileReader(filename.txt));

to read in the file and to convert the floats i'm going to use,

Integer.toBinaryString(Float.floatToRawIntBits(yourFloat));

The problem i'm coming across is that i'm trying to use a tokenizer to
break up the text, but when i try and cast it, i get an error saying
that i cannot cast a non-static variable.

some of the input data in the file looks like this... "13.5 8.2
8.8 3.4".


Any help would be greatly appreciated,

Cheers
 
T

Tor Iver Wilhelmsen

The problem i'm coming across is that i'm trying to use a tokenizer to
break up the text, but when i try and cast it, i get an error saying
that i cannot cast a non-static variable.

1) You cannot cast String to int. You need to use Double.parseDouble()
and the like.

2) "non-static variable"? Please provide the EXACT error message and
the line it occurs on.
 
F

Frank

Hi all,

I have to write a program that will take in a txt file containing a
number of floats, parse into individual numbers and then cast the
floats into binary, before writing all the binary numbers to another
file.

At the moment i've got

StreamTokenizer in = new StreamTokenizer(new
FileReader(filename.txt));

to read in the file and to convert the floats i'm going to use,

Integer.toBinaryString(Float.floatToRawIntBits(yourFloat));

The problem i'm coming across is that i'm trying to use a tokenizer to
break up the text, but when i try and cast it, i get an error saying
that i cannot cast a non-static variable.

some of the input data in the file looks like this... "13.5 8.2
8.8 3.4".


Any help would be greatly appreciated,

Cheers

Sounds like you're not using your tokenizer right, but without posing
your code it's impossible to tell. It should, however, look something like:

while (in.nextToken() != in.TT_EOF) {
if (in.ttype != in.TT_NUMBER) throw new NumberFormatException();
float f=(float) in.nval;}
in.close();

in terms of "writing binary numbers", this generally does not mean
converting them to a stream, but instead writing them as they are
represented in memory, eg:

FileOutputStream fos=new FileOutputStream("whatever.dat");
DataOutputStream dos=new DataOutputStream(fos);
dos.writeFloat(f);

Hope this helps!

Frank
 

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,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top