Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

in java how do i make user input actually apply to making a choice, like choosing 'a' over 'b'?

i am new to programming and horrible at it.

i know how to use Scanner and everything i just don't know how to make user input apply to the program. i can ask them to enter 'A' or 'yes' but how do i take that and direct it to a certain method in java.

basically what would i put in the code after:

System.out.println ("please enter your choice")

a = scan.nextInt();

2 Answers

Relevance
  • 9 years ago
    Favorite Answer

    Well if you're asking the user to input "yes" or "no", you don't want to use nextInt(). nextInt() would be used if you asked the user something like, "Enter a number: ".

    Anyway, here's what we do for yes/no.

    String a = scan.nextLine();

    if (a.toLowerCase().equals("yes")) {

    //do something here for yes

    } else if (a.toLowerCase().equals("no")) {

    //do something for no

    } else {

    System.out.println("Sorry, you entered invalid input.");

    }

    The reason we use toLowerCase is so that "yes" "YES" "Yes" "yEs" all match up with "yes" in our if check. And we compare two strings by using stringOne.equals(stringTwo). This will return true if they are equal, and false if not.

    If you're asking the user for a number, here is how you would evaluate the input:

    int a = sc.nextInt();

    if (a == 1) {

    //do something

    } else if ....... etc.

  • 4 years ago

    i could frequently pass mutually with your group. certainly Kallis over Sobers. Richards is a greater perfect batman than Lara. On a speedy song i could drop Murali and play Lillee rather. a million. Jack Hobbs 2. S.Gavaskar 3. D.Bradman 4. S.Tendulkar 5. Viv Richards 6. J.Kallis 7. A.Gilchrist 8. Imran Khan 9. S.Warne 10.D.ok.Lillee 11. M.Marshall 5 bowlers, batting to eight. no longer a undesirable group.

Still have questions? Get your answers by asking now.