What is the correct way to prompt a user for input in Java, and how can I handle both strings and numbers effectively?

I’m just starting out and trying to build a simple coin flip game. I come from a JavaScript background and was hoping to apply some of that knowledge here. Everything works so far except for handling user input in Java.

I’ve imported the Scanner class to read input, but I’m confused about how to properly store user input and then compare it to a randomly generated value. Here’s a simplified version of my code:

package test;
import java.util.Scanner;

public class testclass {
    
  public static void main(String[] args) {
    System.out.println("hi");
    int bob = (int) Math.floor(Math.random() * 2);
    System.out.println(bob);

    System.out.println("Enter heads or tails?");
    // Missing Scanner logic and variable declaration here
    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!");
    }
  }
}

What am I doing wrong with input handling, and how should I use Scanner to read user input properly and make it compatible with logic involving both strings and integers?

Here’s a clean way to get input using Scanner and compare strings safely:

import java.util.Scanner;

public class CoinFlipGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int bob = (int) (Math.random() * 2);

        System.out.println("Enter heads or tails:");
        String userChoice = scanner.nextLine().trim().toLowerCase();

        System.out.println("Computer flipped " + (bob == 0 ? "heads" : "tails"));

        int userChoiceInt = userChoice.equals("heads") ? 0 : 1;

        if (userChoiceInt == bob) {
            System.out.println("You win!");
        } else {
            System.out.println("Sorry, you lost!");
        }

        scanner.close();
    }
}

:speech_balloon: Why this works:

  • You’re using scanner.nextLine() to capture the full input.

  • .equals() is used instead of == (important for comparing strings in Java).

  • Input is cleaned with .trim().toLowerCase() to handle cases like “Heads” or " HEADS ".

This one’s a bit more readable and lets you treat the values in parallel:

import java.util.Scanner;

public class CoinFlipGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String[] options = {"heads", "tails"};
        int bob = (int) (Math.random() * 2);
        String computerChoice = options[bob];

        System.out.println("Enter heads or tails:");
        String userChoice = scanner.nextLine().trim().toLowerCase();

        System.out.println("Computer flipped " + computerChoice);

        if (userChoice.equals(computerChoice)) {
            System.out.println("You win!");
        } else {
            System.out.println("Sorry, you lost!");
        }

        scanner.close();
    }
}

You avoid converting strings to ints manually and directly compare the actual string values.

Let’s take it one step further and make sure the user can only enter “heads” or “tails”:

import java.util.Scanner;

public class CoinFlipGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String userChoice;

        do {
            System.out.println("Enter heads or tails:");
            userChoice = scanner.nextLine().trim().toLowerCase();
        } while (!userChoice.equals("heads") && !userChoice.equals("tails"));

        int bob = (int) (Math.random() * 2);
        String computerChoice = (bob == 0) ? "heads" : "tails";

        System.out.println("Computer flipped " + computerChoice);

        if (userChoice.equals(computerChoice)) {
            System.out.println("You win!");
        } else {
            System.out.println("Sorry, you lost!");
        }

        scanner.close();
    }
}

This keeps your program from breaking if someone types something random like “coin” or “123”.