- #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.
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
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.
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.
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.
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.
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.