0

I'm trying to make a basic 8 ball program, and got it all working, but I want to try changing it so if people ask certain questions there is a predefined answer.

package Main;

import java.util.Random;
import java.util.Scanner;

public class eightBall {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Ask a yes or no question");

        String question = input.next();

        double rnd = Math.random();
        int random = (int)(rnd*10);
        int numberOfChoices = 5;

        if(question.equalsIgnoreCase("Will i win the lottery?")){
            System.out.println("The almigty 8 ball can not tell you this answer.");
        }else{
            switch(random % numberOfChoices){


            case 0:
                System.out.println("No");
                break;
            case 1:
                System.out.println("Yes");
                break;
            case 2:
                System.out.println("Maybe");
                break;
            case 3:
                System.out.println("Definitely");
                break;
            case 4:
                System.out.println("Definitely Not");
                break;

            }
        }


    }

}

I'm not really sure why my if statement there isn't working, because if I run it and ask "Will I win the lottery?", I get something from that switch statement.

SiHa
  • 5,520
  • 12
  • 23
  • 37
Donplonex
  • 19
  • 4
  • 1
    My guess is that `question` contains a linebreak at the end (`\n`, `\r` or `\r\n` depending on the OS). What happens if you try it with `question.toLower().startsWith("will i win the lottery?")`? – Michael Dodd Jan 18 '17 at 12:18
  • Possible duplicate: http://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class –  Jan 18 '17 at 12:29

3 Answers3

8

next() returns a single word, not a whole line.

To be more specific, it reads the buffer until it reaches the separator character, which by default is a space (' ').

You will end up getting "Will" instead of "Will i win the lottery?".

Use nextLine() if you want the whole line of input.

Try printing out things or using a debugger if you think something isn't working properly.

Moira
  • 9,571
  • 3
  • 40
  • 61
  • But that will contain the `\n` as well, will it not. You would then need to `trim` it as well. – D. Ataro Jan 18 '17 at 12:10
  • 1
    @D.Ataro No, it won't. From the [docs](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()): "This method returns the rest of the current line, excluding any line separator at the end." – Moira Jan 18 '17 at 12:10
  • @D. Atraro nextLine() method returns the rest of the current line, excluding any line separator at the end. – nail fei Jan 18 '17 at 12:13
  • Thank you very much, this worked. It does seem to be ignoring my .equalsIgnoreCase but that isn't a big deal i suppose, this works when i copy the exact thing, thank you very much :) I think i selected this as the answer, i'm still a bit new to the site here. **EDIT** - It just didn't work once, the ignore case does work, my bad sorry. thanks again – Donplonex Jan 18 '17 at 12:49
0

Should be String question = input.nextLine();

Strelok
  • 46,161
  • 8
  • 92
  • 112
0

Use this code

String question = input.nextLine();

Instead of

String question = input.next();

It happens because of next() method of Scanner Class. So, if you enter "Will i win the lottery?", the question variable holds only "Will". It can hold only one string so, nextLine() method is used to hold String until line not ends.

  • See both methods can hold String.But diff is that next() holds String until White Space is not found and nextLine() will not terminate until it not found any escape character like '\n' – Parmar Bharat Jan 18 '17 at 12:33