Jamin2112
- 973
- 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.
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.
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.
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.
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.
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++;
}
}
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++;
}
}
}
} // what is this closing?
else if ((k + 7) % 7 == 4 || (k + 7) % 7 == 5)
DavidSnider said:Code:} } // what is this closing? else if ((k + 7) % 7 == 4 || (k + 7) % 7 == 5)
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++;
}
}
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?
(k + 7) % 7
k % 7