0

I'm getting an error at the line

else break;

I think it might have to do with the location of the "{}"

public class Palindrome {

    private static String number;

    public static void main(String[] args) {
        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);

        if(number.length() !=5 ){
            JOptionPane.showMessageDialog(null,"Please enter a 5 digit number!","Try again",
            JOptionPane.PLAIN_MESSAGE);

        else break;
        }

        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;
        }
    }
}

Thanks

1
  • 4
    You'll find it a lot easier to fix these errors if you indent your code - you should find your IDE is willing to do this for you automatically. Commented Mar 9, 2011 at 18:02

5 Answers 5

4

else cannot be part of if. So, change this -

if(number.length() !=5 )
{
    JOptionPane.showMessageDialog(null,"Please enter a 5 digit number!","Try again",
    JOptionPane.PLAIN_MESSAGE);
}
else break;

Also, I don't understand why you use break here ?

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

2 Comments

There's no loop to break out of.
@Erik - That is what my doubt is. Why @Mike is using break here when there is no loop to come out of !!
1
if(/*some condition*/){
}else{
     break;//invalid use of break it won't compile see below
}

you are writting else without matching if

moreover you can't break without loop or switch case

2 Comments

Actually, you can break out of a named statement, too, but there is no such statement here, too. (And the break would need the name of the statement then.)
@Paŭlo You mean break with label ?
1

Also a note: code like "if (condition) return true; else return false;" can be substituted for "return condition;". Much cleaner.

Comments

1

There are two problems:

  1. else statement is not associated with an if statement. You need to move it outside of the {} for the else
  2. You are using a break outside of a while or for loop (or named statement).

If you are trying to exit the application, then you can use System.exit method. However a better approach is to return a value that states that the user did not select a value, e.g., -1 or null (change return type of method to Integer).

Comments

1

You cannot break; from a method, instead you must return from a method. However in your case it would appear you want a retry loop.

private static int getUserInput() {
  while(true) {
    String answer = JOptionPane.showInputDialog(null, "Please enter a five digit number","Enter a number");
    if(answer.length() == 5 ) 
        return Integer.parseInt(answer);
    JOptionPane.showMessageDialog(null,"Please enter a 5 digit number!","Try again",
        JOptionPane.PLAIN_MESSAGE);
 }
}

2 Comments

I'm getting an error at the bottom line "return inputNumber;" It says unreachable code.
When I plug in a number that is not equal to five it brings up the original box to enter the number, it doesn’t give me the error box. Now when I go and plug a number again that is not equal to 5 then it brings up the error box. So it’s only after the second wrong attempt is when it works correctly.

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.