+ 1

Lottery Winner Generator problem

Hi. I am having problem with my homework about this. Basicly this is lottery winner generator or random number generator using file.txt. There are 2 inputs, for the lottery number and the participant's name. The output is the name of the Winner. Here's the code I made: https://code.sololearn.com/cBHUBpaO4PD1/#java The problem is with the 3rd option. My plan with it is to generate a random number from 1-5 and turn its value to String so it can read the text file. I use while loop so it will keep scanning every line. The 'a' variabel will contain the line's value. if 'a' equals to generated random number, it will print the name under the number's line. But this one just skipped it and return to the option menu. What did I miss?

30th Sep 2020, 11:22 AM
M.O.HONOR
M.O.HONOR - avatar
2 Answers
+ 1
I can't run your code right now, so i'm not really sure, but you can try to replace this comparison: a == val with this one: a.equals(val) to actually compare values of the strings. == operator compares references and returns true only if both of them are pointing to the same object. Another thing, if you want to generate number from 1 to 5 you should change generator's bound to 5, because it is not included. Like this: rand.nextInt(5) + 1. Hope this helps.
30th Sep 2020, 11:42 AM
Aleksandrs
Aleksandrs - avatar
+ 1
@Aleksandrs thx a lot, man. It worked!!
30th Sep 2020, 11:58 AM
M.O.HONOR
M.O.HONOR - avatar