This error (Java) is confusing me

  • Context: Java 
  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Confusing Error Java
Click For Summary

Discussion Overview

The discussion revolves around a Java programming error related to the use of 'if' and 'else if' statements. Participants are troubleshooting a specific error message that indicates a missing 'if' statement for an 'else if' condition. The scope includes code structure, syntax issues, and potential refactoring strategies.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant claims they have an 'if' statement for their 'else if' but the compiler indicates otherwise.
  • Another participant suggests that the error may stem from a missing open brace in a preceding 'else if' statement, which could lead to misleading error messages.
  • A different participant points out that mismatched parentheses could be the source of the confusion.
  • Several participants recommend posting the relevant code instead of a screenshot for better clarity and troubleshooting.
  • There is a suggestion to simplify the code by factoring out common functionality, although there is uncertainty about how this should be done.
  • One participant questions whether parentheses are required around 'if' statements, indicating a potential misunderstanding of Java syntax.
  • Another participant identifies a structural issue in the code, suggesting that the nesting of 'if' and 'else if' statements may not be appropriate.
  • A later reply proposes a refactored version of the code, questioning the overall flow control and logic of the original structure.

Areas of Agreement / Disagreement

Participants express various viewpoints on the cause of the error, with no consensus reached on the specific issue. Multiple competing views on code structure and syntax remain present throughout the discussion.

Contextual Notes

Participants highlight potential limitations in the original code structure, including missing braces and parentheses, as well as the need for clearer syntax. There is also mention of the importance of code readability and refactoring, but these suggestions are not universally accepted.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 39 ·
2
Replies
39
Views
8K