Isabelle
Isabelle

Reputation: 41

What's wrong with my 'else' statement?

When running the program, I get the error below:

Enter a number: 5 Your original number is 5.0 ERROR The input is not a number.

I tried everything I can think of, but I am stumped. Following is the code snippet,

import java.util.Scanner;

public class NegativeNumberConversion {

    public static void main(String[] args) {
        // Convert negative numbers to positive and display back to user

        // Create a console object for Scanner class
        Scanner input = new Scanner (System.in);

        // Declare variable
        double numberOne = 0;

        // Prompt user to enter number
        System.out.println("Enter a number: ");

        // Read in the number
        numberOne = input.nextDouble();

        // If the number is positive - display it
        if (numberOne > 0){

        // Display it - Explain that it is the original number
        System.out.println("Your original number is " + numberOne);

        }else  {

        // Report the error
        System.out.println("***ERROR*** The input is not a number.");

        // Terminate the error
        System.exit(1);

    }

        // If the number negative - convert it to positive - 
        if(numberOne < 0){


        // Display it - Explain that it was converted
        System.out.println("Your number was converted to " + (-1*numberOne) + " because it was negative.");

        }else{
        // Report the error
        System.out.println("***ERROR*** The input is not a number.");

        // Terminate the error
        System.exit(1);
    }

        // Close input
        input.close();
    }
}

Upvotes: 0

Views: 157

Answers (3)

arilence
arilence

Reputation: 506

The problem is that with the way your if statements are setup

To fix this, include the second case with an else if.

if (numberOne > 0) {
    System.out.println("Your original number is " + numberOne);
}
else if (numberOne < 0) {
    System.out.println("Your number was converted to " + (-1 * numberOne) + " because it was negative.");
}
else {
    // Report the error
    System.out.println("***ERROR*** The input is not a number.");
    // Terminate the error
    System.exit(1);
}

EDIT: To help you understand WHY it's not working (instead of just showing a solution). When someone enters an input, no matter if it's positive or negative, both if statements are compared and since a number cannot be both, one of the else statements will always run.

Upvotes: 6

Want2bExpert
Want2bExpert

Reputation: 527

Try this;

public class NegativeNumberConversion {

public static void main(String[] args) {
    // Convert negative numbers to positive and display back to user
    // Create a console object for Scanner class
    Scanner input = new Scanner(System.in);

    // Declare variable
    double numberOne = 0;

    // Prompt user to enter number
    System.out.println("Enter a number: ");

    // Read in the number
    numberOne = input.nextDouble();

    // If the number is positive - display it
    if (numberOne > 0) {
        // Display it - Explain that it is the original number
        System.out.println("Your original number is " + numberOne);
    } 

    // If the number negative - convert it to positive -
    else if (numberOne < 0) {
        // Display it - Explain that it was converted
        System.out.println("Your number was converted to " + (-1 * numberOne) + " because it was negative.");
    } else {
        // Report the error
        System.out.println("***ERROR*** The input is not a number.");
        // Terminate the error
        System.exit(1);
    }

    // Close input
    input.close();
}       

Upvotes: 1

Javy
Javy

Reputation: 964

The error is here , when input 5, the program will execute the else statement

 // If the number negative - convert it to positive - 
    if(numberOne < 0){


    // Display it - Explain that it was converted
    System.out.println("Your number was converted to " + (-1*numberOne) + " because it was negative.");

    }else{
    // Report the error
    System.out.println("***ERROR*** The input is not a number.");

    // Terminate the error
    System.exit(1);
}

Maybe the following code is what you want.

try {
    numberOne = input.nextDouble();
    if (numberOne > 0) {
        // Display it - Explain that it is the original number
        System.out.println("Your original number is " + numberOne);
    } else if (numberOne < 0) {
        // Display it - Explain that it was converted
        System.out.println("Your number was converted to " + (-1 * numberOne) + " because it was negative.");
    }
} catch (Exception ex) {
    System.out.println("***ERROR*** The input is not a number.");
    System.exit(1);
}

hope that helped.

Upvotes: 1

Related Questions