Home > Enterprise >  the counter is not updating with my loop and I don't know why
the counter is not updating with my loop and I don't know why

Time:11-24

I'm trying to practice for a techniqual test where I have to count the number of characters in a DNA sequence, but no matter what I do the counter won't update, this is really frustrating as I learnt code with ruby and it would update, but Java seems to have an issue. I know there's something wrong with my syntaxt but for the life of me I can't figure it out.

public class DNA {

  public static void main(String[] args) {
    String dna1 = "ATGCGATACGCTTGA";
    String dna2 = "ATGCGATACGTGA";
    String dna3 = "ATTAATATGTACTGA";
    String dna = dna1;
    int aCount = 0;
    int cCount = 0;
    int tCount = 0; 

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

      if (dna.substring(i) == "A") {
        aCount = 1;
      }
      else if (dna.substring(i) == "C") {
        cCount  ;
      } 
      else if (dna.substring(i) == "T") {
        tCount  ;
      }
      System.out.println(aCount);
    } 
  }
}

It just keeps returning zero instead of adding one to it if the conditions are meet and reassigning the value.

CodePudding user response:

Good time to learn some basic debugging!

Let's look at what's actually in that substring you're looking at. Add System.out.println(dna.substring(i)); to your loop. You'll see:

ATGCGATACGCTTGA
TGCGATACGCTTGA
GCGATACGCTTGA
CGATACGCTTGA
GATACGCTTGA
ATACGCTTGA
TACGCTTGA
ACGCTTGA
CGCTTGA
GCTTGA
CTTGA
TTGA
TGA
GA
A

So, substring doesn't mean what you thought it did - it's taking the substring starting at that index and going to the end of the string. Only the last character has a chance of matching your conditions.

Though, that last one still won't match your condition, which is understandably surprising if you're new to the language. In Java, == is "referential equality" - when applied to non-primitives, it's asserting the two things occupy the same location in memory. For strings in particular, this can give surprising and inconsistent results. Java keeps a special section of memory for strings, and tries to avoid duplicates (but doesn't try that hard.) The important takeaway is that string1.equals(string2) is the correct way to check.

It's a good idea to do some visibility and sanity checks like that, when your program isn't doing what you think it is. With a little practice you'll get a feel for what values to inspect.

CodePudding user response:

Edward Peters is right about misuse of substring that returns a String. In Java, string must be places between double quotes. A String is an object and you must use method equals to compare 2 objects:

String a = "first string";
String b = "second string";
boolean result = a.equals(b));

In your case, you should consider using charAt(int) instead. Chars must be places between simple quotes. A char is a primitive type (not an object) and you must use a double equals sign to compare two of them:

char a = '6';
char b = 't';
boolean result = (a==b);

So, your code should look like this:

public class DNA {
    
        public static void main(String[] args) {
            String dna1 = "ATGCGATACGCTTGA";
            String dna2 = "ATGCGATACGTGA";
            String dna3 = "ATTAATATGTACTGA";
            String dna = dna1;
            int aCount = 0;
            int cCount = 0;
            int tCount = 0;
    
            for (int i = 0; i < dna.length(); i  ) {
                if (dna.charAt(i) == 'A') {
                    aCount  = 1;
                } else if (dna.charAt(i) == 'C') {
                    cCount  ;
                } else if (dna.charAt(i) == 'T') {
                    tCount  ;
                }
                System.out.println(aCount);
            }
        }
    }

CodePudding user response:

substring(i) doesn't select one character but all the characters from i to the string length, then you also made a wrong comparison: == checks 'object identity', while you want to check that they are equals.

You could substitute

if (dna.substring(i) == "A")

with:

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

this works because charAt(i) returns a primitive type, thus you can correctly compare it to 'A' using ==

CodePudding user response:

One of the problems, as stated, was the way you are comparing Strings. Here is a way that uses a switch statement and a iterated array of characters. I put all the strings in an array. If you only have one string, the outer loop can be eliminated.

public class DNA {

    public static void main(String[] args) {
        String dna1 = "ATGCGATACGCTTGA";
        String dna2 = "ATGCGATACGTGA";
        String dna3 = "ATTAATATGTACTGA";

        String[] dnaStrings =
            {dna1,dna2,dna3};
        int aCount = 0;
        int cCount = 0;
        int tCount = 0;
        int gCount = 0;
        for (String dnaString : dnaStrings) {
            for (char c : dnaString.toCharArray()) {
                switch (c)  {
                    case 'A' -> aCount  ;
                    case 'T' -> tCount  ;
                    case 'C' -> cCount  ;
                    case 'G' -> gCount  ;
                }
            }
        }
        System.out.println("A's = "   aCount);
        System.out.println("T's = "   tCount);
        System.out.println("C's = "   cCount);
        System.out.println("G's = "   gCount);
}

prints

A's = 14
T's = 13
C's = 6
G's = 10
  •  Tags:  
  • java
  • Related