File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes int vs Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int vs Strings" Watch "int vs Strings" New topic

int vs Strings

Mario Bonassin

Joined: Apr 08, 2004
Posts: 18
I'm trying to create a simple program that uses a text file to randomly generate a sequence of letters. Where I stuck is how to use the info in the file to choose the letters.
Heres the file
10 a
50 b
100 c
I roll a random number from 1-100 and then pick the letter that matches the number. I can read the file, and roll the number fine the problem is at the >>>>> marker.

How do I compare the int roll to the number in the String?
Thanks this is my first post on this new board. I am completely self taught in java. It probably shows
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ April 09, 2004: Message edited by: Dirk Schreckmann ]
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
use Integer.parseInt(st1.nextToken()) to convert String
into number then compare it with roll.
Mario Bonassin

Joined: Apr 08, 2004
Posts: 18
Thanks that works, but now I have another problem. When it does match the numbers and sets the variable letter, it doesn't break out of the for loop. It either beeps and stops or if the if statment was successful it goes again until its false.
Ok I added break out; and that works, the problem is when the if statment is false it doesn't go the next line and compare its number to the rolled number. it beeps and stops there. Should the while statment be something else?
[ April 08, 2004: Message edited by: Mario Bonassin ]
I think I got it, I just took away the while statement and left the rest the same and it seems to be working.
[ April 08, 2004: Message edited by: Mario Bonassin ]
I agree. Here's the link:
subject: int vs Strings
It's not a secret anymore!