Manu
Manu

Reputation: 45

if/else statement is not being read correctly by the program

This is a program that list several facts about an integer input from a Scanner object. However, I'm having some trouble with the if/else statement at the end.

The problem is if the input is a positive integer other than 0, the program always reads this statement: System.out.println("j) It is smaller than its reverse value: " + reverse);. If it's a negative int, it always prints System.out.println("j) It is bigger than its reverse value: " + reverse);.

I think it's because the data that's stored in reverse is 0, because int reverse = 0; is declared before the while loop. However, the program properly prints the reverse of the input.

import java.util.Scanner;

public class Integer {

    public static void main(String[] args) {
        System.out.println("This program will:");
        System.out.println("1) Prompt you for an integer then \n2) List several facts about that integer");
        Scanner keyboard = new Scanner (System.in); // define a Scanner object attached to a keyboard
        System.out.print("\nEnter an integer: "); // prompt the user to enter an integer

        while ( ! keyboard.hasNextInt()) { // is the first input value an int?
            String badInput; // assign non-integer inputs to badInput
            badInput = keyboard.next();
            System.out.println("Error: expected an integer, encountered: " + badInput);
            System.out.print("Please enter an integer: ");
        }

        int integer = keyboard.nextInt(); // assign the input to the integer variable
        System.out.println("A list of several facts about the number: " + integer); // safe to read first input value

        System.out.println("================================================================");

        // print the input with space betweeen each digits
        System.out.print("a) The digit(s) in it is/are: ");
        String number = String.valueOf(integer);
        for ( int count = 0; count < number.length(); count++) {
            char counted = number.charAt(count); // read each digit in the input
            System.out.print(counted + " ");
        }

        System.out.println(); // skip a line

        // determine whether the input is negative or positive
        if ( integer >= 0 ) {
            System.out.println("b) It is positive");
        }
        else {
            System.out.println("b) It is negative");
        }

        // determine whether the input is even or odd
        if (integer % 2 == 0) {
            System.out.println("c) It is even");
        }
        else {
            System.out.println("c) It is odd");
        }
        int countOdd = 0;
        int countEven = 0;       
        int countDigit = 0; 
        int countZero = 0;
        int reverse = 0;
        int sum = 0;
        int product = 1;
        int readRightMost;

        while(integer != 0) {
            readRightMost = integer % 10; // read rightmost digit in the input   
            reverse = reverse * 10 + readRightMost;
            integer /= 10; // drop the rightmost digit in the input
            ++countDigit;
            sum += readRightMost;
            product *= readRightMost;                                               

            if (readRightMost % 2 == 0){ // count how many even digits are in the input
                ++countEven;
            }
            else { // count how many odd digits are in the input
                ++countOdd;
            }
            if (readRightMost == 0) { // count how many zero digits are in the input
                ++countZero;
            }              
        }
        System.out.println("d) It has " + countDigit + " digit(s)");       
        System.out.println("e) It has " + countOdd + " odd digit(s)");
        System.out.println("f) It has " + countEven + " even digit(s)");
        System.out.println("g) It has " + countZero + " zero digit(s)");
        System.out.println("h) The sum of the digits in it is " + sum);
        System.out.println("i) The product of the digits in it is " + product);

        if (integer < reverse) { // if the reverse value of an int is greater than its original value
            System.out.println("j) It is smaller than its reverse value: " + reverse);
        }
        else { // if the reverse value of an int is lesser than its original value
            System.out.println("j) It is bigger than its reverse value: " + reverse);             
        }  

        System.out.println("================================================================");
    }

}

Upvotes: 1

Views: 66

Answers (1)

At the end of your while(integer != 0) loop, you know integer must be 0, and you never change it again before your if (integer < reverse), so it may as well be if (0 < reverse), which has exactly the behavior you're seeing. To fix it, make your loop operate on a different variable than you test later.

Upvotes: 2

Related Questions