This error (Java) is confusing me

In summary, the conversation is about troubleshooting an error message regarding an 'if' statement for an 'else if' statement. The participants discuss possible causes for the error, such as missing braces and unbalanced parentheses. One participant suggests refactoring the code to simplify it and make it easier to debug. The conversation ends with a suggestion to rethink the structure of the code and a proposed example of how it could be refactored.
  • #1
Jamin2112
986
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
  • #2
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.
 
  • #3
((())(()))) - parentheses don't match in the if statement. Can be the error message is misleading.
 
  • #4
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?
 
  • #5
Still getting the error ... I've looked a thousand times and don't see anything unbalanced.

screen-capture-8-6_zps6474e669.png
 
Last edited:
  • #6
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.
 
  • #7
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++;
}
}

 
  • #8
Wait, do I have to put parentheses around the 'if' statement?
 
  • #9
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.
 

Related to This error (Java) is confusing me

What is causing the error in my Java code?

The error in your Java code could be caused by a variety of factors, such as syntax errors, logic errors, or missing libraries. It is important to carefully review your code and error messages to identify the specific cause of the error.

How can I fix the error in my Java code?

The first step to fixing the error in your Java code is to identify the specific cause of the error. Once you have identified the cause, you can then make the necessary changes to your code to resolve the error. It may also be helpful to consult documentation or seek assistance from other developers.

Why am I getting this error in my Java code?

There are many reasons why you may be getting an error in your Java code. Some common reasons include syntax errors, incorrect data types, or using a method or variable that has not been declared. Carefully reviewing your code and error messages can help you determine the specific cause of the error.

How can I prevent errors in my Java code?

To prevent errors in your Java code, it is important to follow best practices and coding conventions. This includes properly formatting your code, using descriptive variable names, and testing your code regularly. It is also helpful to regularly review and debug your code to catch any potential errors early on.

Can I get help with fixing the error in my Java code?

Yes, there are many resources available to help you fix errors in your Java code. You can consult documentation, seek assistance from other developers, or use debugging tools to help identify and resolve errors. It is always helpful to have a second set of eyes to review your code and provide feedback.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
6K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top