Mike
Mike

Reputation: 2313

User input asked twice

The code below asks to input the numbers two times.

public class Palindrome {

  public static void main(String[] args) {
     boolean x;
     boolean display;

     x = check();
     display = display();
  }

  private static int getUserInput() {
     int inputNumber = 0;
     String answer = JOptionPane.showInputDialog(null, "Please enter a five digit number","Enter a number");
     inputNumber = Integer.parseInt(answer);
     return inputNumber;
  }

  private static boolean check(){
     int inputNumber = getUserInput();
     int number = inputNumber;
     int[] myArray = new int[5];

     for (int i = 0; i < myArray.length; i++) {
        myArray[i] = (int) (number /(Math.pow(10,i)) % 10);
     }

     if(myArray[0] == myArray[4] && myArray[1] == myArray[3])
        return true;
     else
        return false;
  }

  public static boolean display(){
     if (check() == true) {
        JOptionPane.showMessageDialog(null, "This number is a Palindrome",
           "Excellent!",
           JOptionPane.INFORMATION_MESSAGE);
     } else
        JOptionPane.showMessageDialog(null,
           "Number is not a Palindrome!",
           "Sorry",
           JOptionPane.ERROR_MESSAGE);
        return false;
     }
}

I want it to ask just once.

Thanks

Upvotes: 0

Views: 658

Answers (4)

Suraj Chandran
Suraj Chandran

Reputation: 24791

You have called the check() method twice. Each time, the input dialog is shown.

Upvotes: 1

Andrzej Doyle
Andrzej Doyle

Reputation: 103807

It's because you call check() at the top level (which asks for input), but then you call check() again as part of display(). Because of this, the first input is completely ignored (try it, only the second will affect the message). Your call tree looks like this:

main()
 |- check()
 |  |- getUserInput(...)
 |  |   |- *(show input dialog)*
 |
 |-display()
 |  |- check()
 |  |   |- getUserInput(...)
 |  |   |   |- *(show input dialog)*
 |  |
 |  |- (show message dialog)

I suspect that what you mean to do is pass the result of the previous execution into display() as a variable, rather than calling the method again. This might look something like this:

public static void main(String[] args) {
    boolean x = check(); // No real need to declare the variable separately
    boolean display = display(x);
}

...

public static boolean display(boolean check) {
    if (check) { // P.S. no need to explicitly use the "== true"
        ...
    } else
    ...
}

Alternatively, you could drop the call to check() within main, and only make the one from the display method. In my opinion though, it's better programming practice to have the "take input" and "display output" methods as two separate ones. Output methods that also handle input are the stuff of nightmares in non-trivial systems.

Upvotes: 4

Yasser
Yasser

Reputation: 1808

You are first calling check() in the main function:

x = check();

and then once again in the display function

if(check() == true)

If you just call display from main, your code should be fine.

Upvotes: 1

SLaks
SLaks

Reputation: 887509

Your display() method calls check() a second time.

Upvotes: 2

Related Questions