Output from while loop not working
Apologies for posting about this topic twice today, this one is a
different question. So I am working on a java problem at the moment where
I am creating a program that simulates the old TV quiz show, You Bet Your
Life. The game show host, Groucho Marx, chooses a secret word, then chats
with the contestants for a while. If either contestant uses the secret
word in a sentence, he or she wins $100.00.
My program is meant to check for this secret word.
Here is my code:
import java.util.Scanner;
public class Groucho{
String secret;
Groucho(String secret){
this.secret = secret;
}
public boolean saysSecret(String line){
if(secret.equals(line)){
return(true);
}
else{
return(false);
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String line = in.nextLine();
Groucho g = new Groucho(line);
while (in.hasNextLine()) {
String guess = in.nextLine();
/*Not sure about these next two lines:
*String answer = g.saysSecret(guess);
*/System.out.println(answer);
}
}
}
When I run it nothing happens. I thought it should be returning true or
false? What I would actually like it to do is if the line contains the
secret word, it prints a message that says "You have won $100" and tells
what the secret word is. Could anyone point me in the right direction?
Many thanks Miles
No comments:
Post a Comment