Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

This error (Java) is confusing me

  1. Mar 5, 2013 #1
    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
     
  2. jcsd
  3. Mar 5, 2013 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  4. Mar 5, 2013 #3

    Borek

    User Avatar

    Staff: Mentor

    ((())(()))) - parentheses don't match in the if statement. Can be the error message is misleading.
     
  5. Mar 5, 2013 #4

    Borg

    User Avatar
    Gold Member

    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?
     
  6. Mar 5, 2013 #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: Mar 5, 2013
  7. Mar 5, 2013 #6

    DavidSnider

    User Avatar
    Gold Member

    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.
     
  8. Mar 5, 2013 #7
    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++;
    }
    }

     
  9. Mar 5, 2013 #8
    Wait, do I have to put parentheses around the 'if' statement?
     
  10. Mar 5, 2013 #9
    It now says

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

    is an

    illegal start of expression
     
  11. Mar 5, 2013 #10

    DavidSnider

    User Avatar
    Gold Member

    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 ]
     
  12. Mar 5, 2013 #11
    Code (Text):

                                             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++;
                            }
                        }
     
     
  13. Mar 5, 2013 #12

    DavidSnider

    User Avatar
    Gold Member

    look at the first "} else if "

    What is that bracket closing?
     
  14. Mar 5, 2013 #13
    I think I copied it wrong.

    Code (Text):

                                            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++;
                            }
                        }
     
     
  15. Mar 5, 2013 #14

    DavidSnider

    User Avatar
    Gold Member

    Code (Text):

    }
    } // what is this closing?
    else if ((k + 7) % 7 == 4 || (k + 7) % 7 == 5)
     
     
  16. Mar 5, 2013 #15
    Ohhhhh .... I see the problem. I was trying to nest an if-elseif inside an if-elseif. Can I do that somehow?
     
  17. Mar 5, 2013 #16

    DavidSnider

    User Avatar
    Gold Member

    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 (Text):

            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: Mar 5, 2013
  18. Mar 5, 2013 #17
    "char cannot be dereferenced," it said
     
  19. Mar 5, 2013 #18
    This is just one more drop into the bucket of problems in the original code, but:

    Code (Text):
    (k + 7) % 7
    could be written more clearly as:

    Code (Text):
    k % 7
     
  20. Mar 6, 2013 #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.
     
  21. Mar 6, 2013 #20

    harborsparrow

    User Avatar
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: This error (Java) is confusing me
Loading...