0

(first day of trying to learn java)

I have a basic code which takes user input and checks if it is and integer, a double, or a string (in that order). Currently, if the input is integer or string, it works perfectly but if it is a double/decimal, the code for the string "if" statement is also triggered. If i switch the places of the "if" statements for the double and the integer, the integer one starts to trigger the string statement instead of the double. I tried placing the string statement at the very top so nothing is above it but this just makes it so that the string statement overrides the statement below it.

For some reason, instead of using "if" statement for the string, using "else" works (and also removing the statement all together results in the program running as expected) but i want to understand why this is happening for future projects.

import java.util.Scanner;

public class second {
    static Scanner userInput = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.print("Enter a number real quick: ");

        if (userInput.hasNextInt()) {
            int numberEntered = userInput.nextInt();
            System.out.println("U owe me $" + numberEntered);
        }

        if (userInput.hasNextDouble()) {
            double doubleEntered = userInput.nextDouble();
            System.out.println("U owe me $" + doubleEntered);

        }
        if (userInput.hasNextLine()) {
            String stringEntered = userInput.nextLine();
            System.out.println("'" + stringEntered + "'" + " ain't a number dummy");
        }
    }
}

I expect "U owe me $2.1" when I put in 2.1 but i instead get "U owe me $2.1 '' ain't a number dummy"

*edit: deleted closing } when deleting a comment

Cuga
  • 16,782
  • 29
  • 106
  • 156
  • 2
    You're missing a closing ```}``` after the ```if(userInput.hasNextInt()```. – Michael Bianconi Jul 05 '19 at 18:10
  • 3
    use else if condition – Vishal Patel Jul 05 '19 at 18:14
  • 1
    This code doesn't even compile. It outputs nothing – Thomas Weller Jul 05 '19 at 18:14
  • Offtopic, you have formatting issues. This may be a good start for you: https://google.github.io/styleguide/javaguide.html#s4-formatting. If you use an IDE, it should have a shortcut which auto-formats your code. –  Jul 05 '19 at 18:17
  • When you input `2.1`, you also press Enter, so the input is now `2.1`. Calling `nextInt()` consumes the `2.1`, leaving ``, so when you then call `nextLine()`, you get an empty string. --- Related question: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/5221149) – Andreas Jul 05 '19 at 18:17

1 Answers1

0

Couple of things to point out; first it is a good practice to use else if rather than if for multiple conditions. I think is more clean when looking at it or debugging it as it communicates to a programmer reading the code that the group of 'else if' statements are mutually exclusive.

    if (userInput.hasNextInt()) {
        int numberEntered = userInput.nextInt();
        System.out.println("U owe me $" + numberEntered);
    } else if (userInput.hasNextDouble()) {
        double doubleEntered = userInput.nextDouble();
        System.out.println("U owe me $" + doubleEntered);
    } else if (userInput.hasNextLine()) {
        String stringEntered = userInput.nextLine();
        System.out.println("'" + stringEntered + "'" + " ain't a number dummy");
    }

works well with the intended result you want. When working with Scanner have in mind that:

Scanner.nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline.

Cuga
  • 16,782
  • 29
  • 106
  • 156
Hasan Patel
  • 397
  • 4
  • 15
  • 1
    in this case, `else if` is what the OP was missing. for his sake, though, use it whenever appropriate. there will be times when he wants a regular `if` without 'else' as well – Cuga Jul 05 '19 at 19:11