Java This error (Java) is confusing me

AI Thread Summary
The discussion revolves around a confusing Java error related to an 'else if' statement, with users suggesting that the actual issue may stem from a missing open brace in a previous statement. Participants emphasize the importance of posting code snippets instead of images for better clarity and debugging. Suggestions include simplifying the code by factoring out common functionality and ensuring proper use of parentheses around 'if' statements. The conversation also highlights the need for clearer flow control and structure in the code to avoid confusion and potential errors. Overall, the thread underscores the importance of code readability and organization in programming.
Jamin2112
Messages
973
Reaction score
12
It's saying I don't have an 'if' statement for my 'else if' statement, but I do. You can see clearly from this picture that I do.

screen-capture-7-8_zps140ce03d.png
 
Technology news on Phys.org
Your image doesn't show what's to the right; it's cut off.

Did you forget to type the open brace the previous "else if", three lines above the one highlighted? If you did, the two close braces that immediately precede the highlighted "else if" will close of the "if" and the "for" statements. If that's the case, the compiler will complain about the highlighted line, but the real error is three lines above.
 
((())(()))) - parentheses don't match in the if statement. Can be the error message is misleading.
 
I'm with Borek on that - it looks like you have an extra right bracket at the end. If you copy the code instead of showing an image, anyone could drop it into an IDE and figure it out quickly. Does your IDE highlight what it thinks is the other end of a group when you place the cursor at one end?
 
Still getting the error ... I've looked a thousand times and don't see anything unbalanced.

screen-capture-8-6_zps6474e669.png
 
Last edited:
Please post the relevant code instead of a screenshot.

Also, when your code looks like this it is time to simplify it by factoring out common functionality.
 
DavidSnider said:
Please post the relevant code instead of a screenshot.

Also, when your code looks like this it is time to simplify it by factoring out common functionality.

I factored out common functionality (I think). Now it looks like


if (k + 7) % 7 == 1 {
if answers.charAt(k - 1) == 'a' || answers.charAt(k - 1) == 'A' {
introvertQuestionsAns++;
} else if answers.charAt(k - 1) == 'b' || answers.charAt(k - 1) == 'B' {
introvertNum++;
introvertQuestionsAns++;
}
} else if answers.charAt(k - 1) == 'a' || answers.charAt(k - 1) == 'A' {
intuitionQuestionsAns++;
} else if answers.charAt(k - 1) == 'b' || answers.charAt(k - 1) == 'B' {
intuitionNum++;
intuitionQuestionsAns++;
}
} else if (k + 7) % 7 == 4 || (k + 7) % 7 == 5 {
if answers.charAt(k - 1) == 'a' || answers.charAt(k - 1) == 'A' {
feelingQuestionsAns++;
} else if answers.charAt(k - 1) == 'b' || answers.charAt(k - 1) == 'B' {
feelingNum++;
feelingQuestionsAns++;
}
} else {
if answers.charAt(k - 1) == 'a' || answers.charAt(k - 1) == 'A' {
perceivingQuestionsAns++;
} else if answers.charAt(k - 1) == 'b' || answers.charAt(k - 1) == 'B' {
perceivingNum++;
perceivingQuestionsAns++;
}
}

 
Wait, do I have to put parentheses around the 'if' statement?
 
It now says

if (k + 7) % 7 == 1 {

is an

illegal start of expression
 
  • #10
Yes you have to put parens around the if statements. I don't think you quite understand what I meant about factoring out common functionality. Let's worry about that later. Revert your code to the way it was and when you post code on this site wrap it in tags like this: [ CODE ] code goes here [ /CODE ]
 
  • #11
Code:
                                         if (answers[i].charAt(k - 1) == 'a' || answers[i].charAt(k - 1) == 'A') { 
							introvertQuestionsAns++;
						} else if (answers[i].charAt(k - 1) == 'b' || answers[i].charAt(k - 1) == 'B') { 
							introvertNum++;
							introvertQuestionsAns++;
						}
					}  else if (answers[i].charAt(k - 1) == 'a' || answers[i].charAt(k - 1) == 'A') { 
							intuitionQuestionsAns++;
						} else if (answers[i].charAt(k - 1) == 'b' || answers[i].charAt(k - 1) == 'B') { 
							intuitionNum++;
							intuitionQuestionsAns++;
						}
					}  else if ((k + 7) % 7 == 4 || (k + 7) % 7 == 5) { 
						if (answers[i].charAt(k - 1) == 'a' || answers[i].charAt(k - 1) == 'A') {
							feelingQuestionsAns++;
						} else if (answers[i].charAt(k - 1) == 'b' || answers[i].charAt(k - 1) == 'B') { 
							feelingNum++;
							feelingQuestionsAns++;
						}
					} else { 
						if (answers[i].charAt(k - 1) == 'a' || answers[i].charAt(k - 1) == 'A') { 
							perceivingQuestionsAns++;
						} else if (answers[i].charAt(k - 1) == 'b' || answers[i].charAt(k - 1) == 'B') { 
							perceivingNum++;
							perceivingQuestionsAns++;
						}
					}
 
  • #12
look at the first "} else if "

What is that bracket closing?
 
  • #13
I think I copied it wrong.

Code:
                                        if ((k + 7) % 7 == 1) { 
						if (answers[i].charAt(k - 1) == 'a' || answers[i].charAt(k - 1) == 'A') { 
							introvertQuestionsAns++;
						} else if (answers[i].charAt(k - 1) == 'b' || answers[i].charAt(k - 1) == 'B') { 
							introvertNum++;
							introvertQuestionsAns++;
						}
					}  else if (answers[i].charAt(k - 1) == 'a' || answers[i].charAt(k - 1) == 'A') { 
							intuitionQuestionsAns++;
						} else if (answers[i].charAt(k - 1) == 'b' || answers[i].charAt(k - 1) == 'B') { 
							intuitionNum++;
							intuitionQuestionsAns++;
						}
					}  else if ((k + 7) % 7 == 4 || (k + 7) % 7 == 5) { 
						if (answers[i].charAt(k - 1) == 'a' || answers[i].charAt(k - 1) == 'A') {
							feelingQuestionsAns++;
						} else if (answers[i].charAt(k - 1) == 'b' || answers[i].charAt(k - 1) == 'B') { 
							feelingNum++;
							feelingQuestionsAns++;
						}
					} else { 
						if (answers[i].charAt(k - 1) == 'a' || answers[i].charAt(k - 1) == 'A') { 
							perceivingQuestionsAns++;
						} else if (answers[i].charAt(k - 1) == 'b' || answers[i].charAt(k - 1) == 'B') { 
							perceivingNum++;
							perceivingQuestionsAns++;
						}
					}
 
  • #14
Code:
}
} // what is this closing?
else if ((k + 7) % 7 == 4 || (k + 7) % 7 == 5)
 
  • #15
DavidSnider said:
Code:
}
} // what is this closing?
else if ((k + 7) % 7 == 4 || (k + 7) % 7 == 5)

Ohhhhh ... I see the problem. I was trying to nest an if-elseif inside an if-elseif. Can I do that somehow?
 
  • #16
I have a feeling the entire structure of this code isn't doing what you want it to. I would rethink it.

Lets just say you decided to refactor it like this:
Code:
        int offset = (k + 7) % 7;
        char answer = Character.toUpperCase(answers[i].charAt(k - 1));

        if (offset == 1)
        {
            if (answer == 'A')
            {
                introvertQuestionsAns++;
            }
            else if (answer == 'B')
            {
                introvertNum++;
                introvertQuestionsAns++;
            }
        }
        else if (answer == 'A')
        {
            intuitionQuestionsAns++;
        }
        else if (answer == 'B')
        {
            intuitionNum++;
            intuitionQuestionsAns++;
        }
        else if (offset == 4 || offset == 5)
        {
            if (answer == 'A')
            {
                feelingQuestionsAns++;
            }
            else if (answer == 'B')
            {
                feelingNum++;
                feelingQuestionsAns++;
            }
        }
        else
        {
            if (answer == 'A')
            {
                perceivingQuestionsAns++;
            }
            else if (answer == 'B')
            {
                perceivingNum++;
                perceivingQuestionsAns++;
            }
        }

The flow control doesn't seem to make much sense, does it?
 
Last edited:
  • #17
DavidSnider said:
I have a feeling the entire structure of this code isn't doing what you want it to. I would rethink it.

Lets just say you decided to refactor it like this:
Code:
        int offset = (k + 7) % 7;
        char answer = Character.toUpperCase(answers[i].charAt(k - 1));

        if (offset == 1)
        {
            if (answer == 'A')
            {
                introvertQuestionsAns++;
            }
            else if (answer == 'B')
            {
                introvertNum++;
                introvertQuestionsAns++;
            }
        }
        else if (answer == 'A')
        {
            intuitionQuestionsAns++;
        }
        else if (answer == 'B')
        {
            intuitionNum++;
            intuitionQuestionsAns++;
        }
        else if (offset == 4 || offset == 5)
        {
            if (answer == 'A')
            {
                feelingQuestionsAns++;
            }
            else if (answer == 'B')
            {
                feelingNum++;
                feelingQuestionsAns++;
            }
        }
        else
        {
            if (answer == 'A')
            {
                perceivingQuestionsAns++;
            }
            else if (answer == 'B')
            {
                perceivingNum++;
                perceivingQuestionsAns++;
            }
        }

The flow control doesn't seem to make much sense, does it?

"char cannot be dereferenced," it said
 
  • #18
This is just one more drop into the bucket of problems in the original code, but:

Code:
(k + 7) % 7

could be written more clearly as:

Code:
k % 7
 
  • #19
I have a strong feeling the logic in those if statements is not going to do what you want. Generally if code looks like this it is a strong indication that you are approaching the problem in the wrong way.

If you stated what this is intended to do we might be able to help you better.
 
  • #20
As a coding style, this qualifies as "spaghetti code". Add some spacing, some comments on what you're trying to accomplish, and try with formatting to clearly delineate the control structure.

Some of the inner "if-else" parts could be calls to a subroutine to make the code more readable. If you're concerned about performance, you can remove those later after it all works correctly.
 
Back
Top