0

I'm basically wondering why my conditional statement allows the variable exponent to become 0.0 if a number such as -0.5 is entered by the user. This is my code...

    Scanner num2 = new Scanner(System.in);
    System.out.println("Enter an integer value for n: ");

    int temp = 0;

    if(num2.hasNextInt()) {
        temp = num2.nextInt();
        if(temp >= 1.0 ){
            exponent = temp;
        }
        else {
            System.out.println("You have not entered a positive integer value for n.");
            exponent();
        }
    }
    return exponent;

The weird part occurs when I assign temp to the nextInt. By doing so, I hope to make the following conditional statement work as intended, (temp has to be greater than or equal to 1 for exponent to be set). However, when I call this function and put in -0.5, the function completes and makes exponent = 0. In this case, my goal is to have an exponent that is greater than or equal to 1. Thanks in advance!

2 Answers 2

1

If your input is -0.5, then the condition num2.hasNextInt() will be false, and the next executed instruction will be return exponent;, which seems to be returning 0 here (I don't know why since the code you provided is incomplete).

To check properly if the input is correct, try something like:

if(num2.hasNextInt() && (temp = num2.nextInt()) >= 1) {
    exponent = temp;
}
else {
    System.out.println("You have not entered a positive integer value for n.");
    exponent();
}

EDIT: I didn't know what your exponent() method did, but if it's the same method, I would also recommend using a simple for loop instead of recursive calls, as stated in the other answer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, my exponent method basically returns an integer called Exponent that has to be greater than or equal to one. By placing the argument of >= 1 in the first conditional, like you stated, -0.5 is no longer returning 0.0 as the exponent.
0

It appears that you are attempting to recursively change the local value temp. You aren't assigning temp = exponent();, but I would recommend you use a basic loop -

int temp = 0;
while (temp < 1) {
    if (num2.hasNextInt()) { // <-- do we have an int?
      temp = num2.nextInt();
    } else if (num2.hasNext()) { // <-- do we have anything?
      System.err.printf("%s is not an int%n", num2.next());
    } else { // <-- no more input! No valid temp ever received. ERROR and exit.
      System.err.println("No more input");
      System.exit(1);
    }
    System.err.flush();
    if(temp >= 1.0 ){
      exponent = temp;
    } else {
      System.out.println("You have not entered a positive integer value for n.");
      // exponent();
    }
}

Which if run with -0.5 outputs

-0.5 is not an int
You have not entered a positive integer value for n.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.