pearbear
pearbear

Reputation: 1055

Java cannot find symbol for loops, logic problems?

Ok, my program in this specific section takes a line of data from a studentAnswer string array, the value of which would be something like TTFFTFTFTF. I am supposed to take this, and compare it against a key array, which might look like TFFFTFTFTF. A student takes a quiz, and my program calculates the points correct.

My intention is to use a separate points array to find the numeric grade for the student. The index of studentAnswer refers to a specific student. So studentAnswer[i] is TTFFTFTFTF. I use substrings to compare each individual T/F against the correct answer in key[], which would have a single T/F in each index. Then, if they are correct in their answer, I add a 1 to the correlating index in points[] and will later find the sum of points[] to find the numeric grade out of ten.

My problem here is that String origAns, used to define the student's original answer string, is getting a Java Error cannot find Symbol. I have tried placing the instantiation of origAns within each different for loop, but I can't get the program to work. Int i is meant to follow each specific student- I have four parallel arrays that will all log the student's ID number, numeric grade, letter grade, and original answers. So that is the intention of i, to go through each student. Then j should be used to go through each of these original student answer strings and compare it to the correct answer... Logically, it makes sense to me where I would put it, but java doesn't agree. Please help me to understand this error!

for (int i = 0; i < studentAnswer.length; i++){
    String origAns = studentAnswer[i];
    for (int j = 0; j < key.length; j++){
        if (origAns.substring[j] == key[j]){
            //substring of index checked against same index of key
            points[j] = 1;
        }
        if (origAns.substring[j] != key[j]){
            points[j] = 0;
        }
    }
 }

Upvotes: 1

Views: 624

Answers (5)

John Eipe
John Eipe

Reputation: 11228

If you are using a String use charAt function

String studentAnswer = "TTFFTFTFTF";
for (int i = 0; i < studentAnswer.length(); i++)
{
   char origAns = studentAnswer.charAt(i);
        }

Else if you are using an char array then

char studentAnswer[] = "TTFFTFTFTF".toCharArray();
        for (int i = 0; i < studentAnswer.length; i++){
            char origAns = studentAnswer[i];
        }

Upvotes: 0

abelito
abelito

Reputation: 1104

Substring is a function, not a member, of String objects. Check out the example at the top of this page:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Notice the use of parenthesis instead of brackets.

Upvotes: 0

Jack Edmonds
Jack Edmonds

Reputation: 33171

When you call a method in Java, you use parentheses () instead of brackets [].

Since substring is a method, you should call it like so

if (origAns.substring(j) == key[j])

A few other notes, you should use the equals method for comparisons (especially those comparisons involving Strings.)

if (origAns.substring(j).equals(key[j]))

Also, you should use charAt to extract a single character at some position in a string. substring(j) will return a string of characters starting at position j.

if (origAns.charAt(j).equals(key[j]))

Upvotes: 1

AlexR
AlexR

Reputation: 115338

Your explanation is very long and I have not read it from the beginning to end. But I can see at least one problem in your code:

if (origAns.substring[j] == key[j])

You are comparing strings using == instead of using method equals():

if (origAns.substring[j].equals(key[j]))

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500835

It sounds like you're trying to call the substring method - but you're trying to access it as if it were a field. So first change would be:

if (origAns.substring(j) == key[j])

Except that will be comparing string references instead of contents, so you might want:

if (origAns.substring(j).equals(key[j]))

Actually, I suspect you want charAt to get a single character - substring will return you a string with everything after the specified index:

if (origAns.charAt(j) == key[j])

... where key would be a char[] here.

You can also avoid doing the "opposite" comparison by using an else clause instead.

You should also indent your code more carefully, for readability. For example:

for (int i = 0; i < studentAnswer.length; i++) {
    String origAns = studentAnswer[i];
    for (int j = 0; j < key.length; j++) {
        if (origAns.charAt(j) == key[j]) {
           points[j] = 1;
        } else {
           points[j] = 0;
        }
    }
}

And now, you can change that to use a conditional expression instead of an if/else:

for (int i = 0; i < studentAnswer.length; i++) {
    String origAns = studentAnswer[i];
    for (int j = 0; j < key.length; j++) {
        points[j] = origAns.charAt(j) == key[j] ? 1 : 0;
    }
}

Upvotes: 2

Related Questions