Need some Java help

fbrdphreak

Lifer
Apr 17, 2004
17,556
1
0
Here's the part of the method I'm trying to get to work in my program:

try {
FileReader fr = new FileReader(filename);
StreamTokenizer st = new StreamTokenizer(fr);
st.eolIsSignificant(false); //Allow tokenizing to continue until EOF

while((brstring = st.nextToken) != StreamTokenizer.TT_EOF)
{
brAddr = Integer.parseInt(brstring, 16); //Parse branch address into int
brtaken = st.nextToken; //Read t/f value
simulate_trace(brtaken, brAddr);
}

}

Now here's the error I'm getting when I compile using JDK 1.5.0:

brn_predictor.java:97: cannot find symbol
symbol : variable nextToken
location: class java.io.StreamTokenizer
while((brstring = st.nextToken) != StreamTokenizer.TT_EOF)
^
brn_predictor.java:97: operator != cannot be applied to java.lang.String,int
while((brstring = st.nextToken) != StreamTokenizer.TT_EOF)
^
brn_predictor.java:100: cannot find symbol
symbol : variable nextToken
location: class java.io.StreamTokenizer
brtaken = st.nextToken; //Read t/f value

First of all, why can it not find nextToken??? I looked over the API and I think I'm using the right syntax, any ideas?
Second, I need to read the values from the file in as Strings, but then how am I supposed to check for end of file? Obviously the way I'm doing it up there doesn't work, what's another way to have the while loop repeat until end of file?

Thanks in advance guys
 

Templeton

Senior member
Oct 9, 1999
467
0
0
try something like this. remember, nextToken is a method, you need to include parens. Also, nextToken returns an integer value indicating the token type read, look at the constants that start with TT_ in the class. The actual token read is placed in either sval or nval depending on whether a string or decimal value was read. Read through the java doc to understand better.
 

fbrdphreak

Lifer
Apr 17, 2004
17,556
1
0
Thank you so much! That clarifies a lot; I'm writing this program for a micrprocessor class to simulate a branch predictor and the prof isn't too helpful with Java.

BTW reading the next token in the loop is what I need to do as the input file is in this format:

01234ab t
01234ac f
<etc etc>

The first value is a hex address and the second indicates true or false; hence the parseInt to convert the hex to int.

Thanks I'll try this out soon!
 

fbrdphreak

Lifer
Apr 17, 2004
17,556
1
0
I followed your suggestions and added that to my test program, but I keep getting a null exception. I'm gonna keep playing with this but any help you can provide would be appreciated

 

fbrdphreak

Lifer
Apr 17, 2004
17,556
1
0
Error:

eos% java FileReadTest2
First token: null
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:414)
at FileReadTest2.readMyFile(FileReadTest2.java:68)
at FileReadTest2.main(FileReadTest2.java:44)
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
you should use hasMoreTokens() check if there are more tokens left in the file.

while(st.hasMoreTokens()){
string tp = st.nextToken();

//Process string

}

This should stop you from ever geting a null for your string.
 

fbrdphreak

Lifer
Apr 17, 2004
17,556
1
0
Gotcha, and because I set st.eolIsSignificant(false) it will read end of line as just another token and thus continue until end of file right?

Thanks all, helping me out a bunch
 

fbrdphreak

Lifer
Apr 17, 2004
17,556
1
0
OK guys, still in a bit of a pickle. I can get it to read through the file properly, but it does not process the tokens properly or something. You can see the code attached, here is the test file I'm using:

302d28 n
102d28 y
202d28 n

And here is the output I'm getting:

eos% java FileReadTest2
First Token type: -2
First token: null
Second Token type: -3
Second Token: d28
*******************
First Token type: -3
First token: n
Second Token type: -2
Second Token: null
*******************
First Token type: 32
First token: null
Second Token type: -3
Second Token: y
*******************
First Token type: -3
First token: d28
Second Token type: 32
Second Token: null
*******************
Why is it not reading the whole first word (hex address actually) and then the single y/n at the end of the line? It seems to be missing the first part or something, I have no clue WTF its doing!!

HELP!!
 

AmigaMan

Diamond Member
Oct 12, 1999
3,644
1
0
it looks like you're incrementing your tokenizer while in the loop and it's throwing things off. I rewrote it a bit and got the following:
token type: TT_NUMBER
token: 302.0
token type: TT_WORD
token: d28
token type: TT_WORD
token: n
token type: TT_NUMBER
token: 102.0
token type: TT_WORD
token: d28
token type: TT_WORD
token: y
token type: TT_NUMBER
token: 202.0
token type: TT_WORD
token: d28
token type: TT_WORD
token: n
is this what you're expecting?

 

fbrdphreak

Lifer
Apr 17, 2004
17,556
1
0
That is closer to what I'm looking for, I should've explained a little better what I'm doing.

I'm going to have a file with line after line like I gave an example of before:

302d28 n
102d28 y
202d28 n

I need to read in that first part "302d28" into a string to be parsed from a hex value to an int (got the parsing part down). Then I need to read the "n" or "y" into another string in the same loop. After that, I pass those two values to a simulate_trace function which does some stuff with that data, comes back to the loop, and then the loop starts again reading from the next line. Essentially I need the StreamTokenizer to read in the first token up to the first whitespace, then read the next token ("n" or "y") up to the end of line, call the function, and keep doing that until end of file.

Thanks all
 

Brassman

Golden Member
Nov 6, 2000
1,984
0
0
Why not use something like this? Too much overhead?


BTW - you can parse the line read from the StringTokenizer to get what you want and then just call your method after you parse both or one token..
 

fbrdphreak

Lifer
Apr 17, 2004
17,556
1
0
Overhead doesn't matter for this program!

Now my question is does that separate values to read by whitespace as I described in my previous post? I don't care how I do this, but I need to do the following:
*Read first value on line up until whitespace, store as string (ultimately to be converted from hex address to int)
*Read second value on line until end of line/whitespace, store as string.
*Call separate function to process those two values
*Loop and repeat on each line until end of file

I assume that code you posted runs? I'll play with that and my test file, thanks!
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |