1

I am getting the "Must be an array type but it resolved to string" error in my code. It also says that i (in the code below) cannot be resolved to a variable which I don't get.

    public class DNAcgcount{

        public double ratio(String dna){
        int count=0;
        for (int i=0;i<dna.length();i++);
            if (dna[i]== "c"){
            count+= 1;
            if (dna[i]=="g"){
            count+=1;
        double answer = count/dna.length();
        return answer;

    }

    }


}

}

Could you guys please help me figure out where the problem lies? I'm new to coding in Java so I am not entirely comfortable with the format yet.

Thanks a lot, Junaid

7
  • Remove the semicolon after your for statement, for a starter. Commented Jan 14, 2014 at 9:53
  • You should align subordinate clauses, and properly embed them in { ... } so that one can see what you even mean. Commented Jan 14, 2014 at 9:55
  • @Ingo Actually, OP should read some introductory tutorials, to be honest :) This is the worst case of programming by trial-and-error I have seen in quite some time. Commented Jan 14, 2014 at 9:56
  • 1
    @Vash Please don't alter the question by fixing the errors in the code! Commented Jan 14, 2014 at 10:03
  • @tobias_k, The alter was made to focus on the error. As the { issues ware not related to it. I have done this as the question will stay on SO and can be important for other user in future. Commented Jan 14, 2014 at 10:13

7 Answers 7

5

You cannot access a String's character using subscript (dna[i]). Use charAt instead:

dna.charAt(i) == 'c'

Also, "c" is a String, 'c' is a char.

One more thing - integer division ( e.g. int_a / int_b ) results in an int, and so you lose accuracy, instead - cast one of the ints to double:

double answer = count/(double)dna.length();
Sign up to request clarification or add additional context in comments.

4 Comments

So I must use 'c' instead of "c"? Also, it still says that i cannot be resolved to a variable? Thanks a lot.
@user2904796 - yes. And some of your errors are not displayed in the question, as someone already edited it. Look at the edits he performed, modify your code, and try again.
declaring count as double count = 0; would also do the job.
@pshemek - sure, I prefer to do it this way, but I guess it's a matter of taste..
2

Use {} to define the scope of the loop. Also, as others already pointed out, use charAt instead of [] and use ' for characters, and use floating point division for the ratio.

for (int i = 0; i < dna.length(); i++) {
    if (dna.charAt(i) == 'c') {
        count += 1;
    }
    if (dna.charAt(i) == 'g') {
        count += 1;
    }
}

Or a bit shorter, use || to or the two clauses together

if (dna.charAt(i) == 'c' || dna.charAt(i) == 'g') {
    count += 1;
}

Comments

2

I think you are currently a bit weak at brackets , this is what i understood from your code and corrected it;

public class DNAcgcount{

    public double ratio(String dna){
    int count=0;
    for (int i=0;i<dna.length();i++){
        if (dna.charAt(i)== 'c')
        count+= 1;
        if (dna.charAt(i)=='g')
        count+=1;
    }
        double answer = count/(double)dna.length();
        return answer;
    }
}

After if we have to close the brackets when what you want in if is finished . I think you wanted count to be the number of time c or g is present in the dna. You also did some other mistakes like you have to use 'c' and 'g' instead of "c" and "g" if you are using .charAt(i) because it will be treated like a character and then only you can compare . You may view this link http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

and you may also have a look at works you can do with string like charAt.

Comments

1

It seems like that you have a few problems with the main syntax of basic java functions like loops or if-else statement. Click here for a good tutorial on these.

You must correct your for-loop and your if-statement:

for(int i=0;i<dna.length();i++){
    if(...){
       ...;
    }
    if(...){
       ...;
    }
}

Now you wont get the Cant be resolved to a variable... exception.

Second thing is the usage of your string. You have to use it like this:

for(int i=0;i<dna.length();i++){
    if(dna.charAt(i) == 'c'){
       count += 1;
    }
    if(dna.charAt(i) == 'g'){
       count += 1;
    }
}

Now all your exceptions should be eleminated.

Comments

0

Your problem is with syntax dna[i], dna is a string and you access it as it would be an array by []. Use dna.charAt(i); instead.

Comments

0

You using String incorrectly. Instead of accessing via [] use dna.charAt(i).

Altough logically a string is an array of characters in Java a String type is a class (which means it has attributes and methods) and not a typical array.

And if you want to compare a single character to another enclose it with '' instead of "":

if (dna.charAt(i) == 'c')
.
.

Comments

0

There are two errors:

count should be double or should be casted do double answer = (double)count / dna.length();

and as mentioned above you should replace dna[i] with dna.charAt(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.