Aranya Ram
Aranya Ram

Reputation: 1

I am getting an else without if error and I don't understand why - JAVA

 public class ProblemFour {
    public static void main(String[] args) {
        boolean externalCheck = true;
        boolean internalCheck = true;
        String Str = new String();
        int prod = 0;
        int mod = 0;
        while (externalCheck == true) {
            for (int a = 999; a > 99; a--) {
                for (int b = 999; b > 99; b--) {
                    prod = a * b;
                    Str = Integer.toString(c);
                    if (internalCheck == true && mod < Str.length() / 2) {
                        if (c % 2 == 0) {
                            for (int i = 0; i < Str.length(); i++) {
                                if (Str.substring(i) != Str.substring(Str.length() - 1)) {
                                    internalCheck = false;
                                } else {
                                    mod++;
                                }
                            } else {
                                String S = Str.substring(0, (Str.length() - 1) / 2) + Str.substring(Str.length() / 2, Str.length());
                                for (int i = 0; i < S.length(); i++) {
                                    if (S.substring(i) != Str.substring(S.length() - 1)) {
                                        internalCheck = false;
                                    } else {
                                        mod++;
                                    }
                                }
                            }
                        } else {
                            return Str;
                            System.out.println(Str);
                            externalCheck = false;
                        }

I am getting an error that says I have an else without and if and I don't understand where this is coming from. Specifically on line 31. What more detail do i need. is this not enough detail

Upvotes: 0

Views: 83

Answers (2)

Andy Lowry
Andy Lowry

Reputation: 795

You're missing four closing braces: One just before:

else{
  String S= Str.substring(0,(Str.length()-1)/2) + Str.substring(Str.length()/2, Str.length());

And three at the end of your method.

Or maybe those aren't the right places... but they make everything balance. You have other problems after that.

I would strongly recommend that you learn how to use an editor that can assist you with writing Java code - e.g. the Eclipse IDE or Java Mode in Emacs, or whatever. Trying to track down issues like this when the code looks like yours does is torture. A good development editor will help you keep your braces all lined up.

Upvotes: 1

Vivin Paliath
Vivin Paliath

Reputation: 95518

You don't have a closing brace for if (c%2 ==0):

if (c%2 ==0) {
    for(int i =0; i<Str.length(); i++) {
        if(Str.substring(i) != Str.substring(Str.length()-1)) {
            internalCheck = false;
        } else {
            mod++;
        }
    }
} else { //You are missing the } here. What you have is just "else {"
   ...
}

Upvotes: 0

Related Questions