Take the 2-minute tour ×

I have been trying all weekend to find a way to get my program to answer a question using a sting with a scanner class. For example I need to get my program to answer a question like

Who is on the 5 dollar bill?

Input would be Lincoln and other inputs would be invalid the question will have 3 choices so the logic has to work.

Can you point me in the right direction on how to get this to work in Java? I want to understand the material but I have really tried all weekend.

share|improve this question
The question is quite confusing. Could you clarify a bit? So you read input from somewhere with your Scanner and want to respond to a hardcoded number of questions? Anyhow you'll need to use something else to write your answer out as Scanner is only there to read input. –  Voo Feb 13 '11 at 19:37
add comment

2 Answers

up vote 4 down vote accepted

If I understood your question properly, then this should point you in the right direction:

Import the Scanner:

import java.util.Scanner;

Then, here is the method you'd want to call:

public void myScanner () {
     Scanner scan = new Scanner(System.in); //Creates a new scanner
     System.out.println("Who is on the 5 dollar bill?"); //Asks question
     String input = scan.nextLine(); //Waits for input
     if (input.equalsIgnoreCase("Lincoln")) { //If the input is Lincoln (or any case variant of Lincoln)
     else { //If the input is anything else
share|improve this answer
add comment

If you don't want to encode all the actual word solutions (like "Lincoln") you can also just ask the user to pick a number/letter solution since you only have 3.

 Scanner input = new Scanner(System.in);
 System.out.println("Who is on the 5 dollar bill?  1. Lincoln 2. Somebody 3. Someone");
 String userChoice = scan.nextInt();    //get a number from user
 if(userChoice == 1)
       System.out.println("Correct answer!");
       System.out.println("Wrong answer!");

This would make it easy to keep track of the answer key.

share|improve this answer
If you go that route I'd suggest using try-catch statements as well. –  Johannes Feb 13 '11 at 20:17
add comment

Your Answer


