Search code examples
javavariablesimportintjava.util.scanner

How do you prompt a user for an input in java


So I just started learning Java, its literally like my 1st day and I wanted to try to make a coinflip game. I already know a decent amount of Javascript and so i was trying to apply that knowledge to java. So everything has been working so far except one thing: Prompting a user for a choice. So read online that i have to import a scanner so i did that as you can see from my code. I also tried some code where you can have the user import a string but you can see a bit later in my program i change the variable userChoice into a number. So basically i just need help with this. If there is some way to have a variable type that can store both numbers or strings that would be best. But im tottaly open to other ways of doing this! Thanks in advanced! Here is the code:

package test;
import java.util.Scanner;
public class testclass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("hi");
        int bob;
        bob = (int) Math.floor(Math.random()*2);
        System.out.println(bob);


          System.out.println("Enter heads or tails?");
          System.out.println("You entered "+ userChoice);

          if (bob == 0) {
            System.out.println("Computer flipped heads"); 
          }

          else {
              System.out.println("Computer flipped tails");
          }



          if(userChoice == "Heads") {
              userChoice = 0;

          }

          else {
              userChoice = 1;
          }



          if (userChoice == bob) {
              System.out.println("You win!");
          }


          else {
              System.out.println("Sorry you lost!")

          }


          }

    }

Solution

  • Use a scanner, as you said:

    Scanner in = new Scanner(System.in);
    

    Then, prompt the user to enter something in:

    String userChoice = in.nextLine();
    

    Also, when you compared strings:

    if(userChoice == "Heads") {...
    

    that's bad to do for none-primitive objects. It's best to only use the == to compare values that are ints or enums. If you compare a String like this, it won't work, because it's checking if the objects are the same. Instead, compare like this:

    if(userChoice.equals("Heads")) {...
    

    Also, to convert to an int (NOTE: You can't convert one type of object to another that aren't related in any way! You'll have to create a new object if you're wanting to do that), do this:

    int myInt = Integer.parseInt(myString); // NOTE: Can throw NumberFormatException if non-number character is found.
    

    So your program should look somewhat like:

        package test;
        import java.util.Scanner;
    
        public class testclass {
    
            public static void main(String[] args) {
                //System.out.println("hi");
                Scanner in = new Scanner(System.in);
                int bob;
                int userChoice;
                String input;
                bob = (int) Math.floor(Math.random()*2);
                System.out.println(bob);
    
                System.out.println("Enter heads or tails?");
                input = in.nextLine(); // waits for user to press enter.
                System.out.println("You entered "+ input);
    
                if (bob == 0) {
                    System.out.println("Computer flipped heads"); 
                }
    
                else {
                    System.out.println("Computer flipped tails");
                }
    
                if(input.equals("Heads")) {
                    userChoice = 0;
                }
                else {
                    userChoice = 1;
                }
    
                if (userChoice == bob) {
                    System.out.println("You win!");
                }
                else {
                    System.out.println("Sorry you lost!");
                }
    
                in.close(); // IMPORTANT to prevent memory leaks
            }
        }