0

I'm trying to use this to print out only part of an array. My array is 5 elements long - {6, 4, 2, 6, 2} - and I'd like to print just {6, 4, 2, 6, 2}. But using my current code, it's printing out [4, 2, 6, 2] - indexes 1 through 4, not indexes 0 through 3. Why might this be happening?

String nucList = CCATT-AATGATCA-CAGTT
int[] counter = new int[5];
for (int i = 0; i < nucList.length(); i++) {
    if (nucList.charAt(i) == 'A') {
    } else if (nucList.charAt(i) == 'C') {
        counter[0]++;
    } else if (nucList.charAt(i) == 'G') {
        counter[1]++;
    } else if (nucList.charAt(i) == 'T') {
        counter[2]++;
    } else if (nucList.charAt(i) == '-') {
        counter[3]++;
    }
int[] counterNucs = Arrays.copyOfRange(counter, 0,  4);
filePrint.println("Nuc. Counts: " + Arrays.toString(counterNucs));

Thanks!

EDIT: This seems to even be an issue with the arrays in my project that should be printing in full as well. Should I post more of my code?

2 Answers2

1

You seem to be adjusting your counters incorrectly. For the 'A' character, you are not adjusting the counter at all. At index 0 (the first array element) you are counting the 'C' character and so on until only index 3 (the fourth element). This leaves the last element undefined.

I believe you want your code to resemble this:

String nucList = CCATT-AATGATCA-CAGTT
int[] counter = new int[5];
for (int i = 0; i < nucList.length(); i++) {
    if (nucList.charAt(i) == 'A') {
        counter[0]++;
    } else if (nucList.charAt(i) == 'C') {
        counter[1]++;
    } else if (nucList.charAt(i) == 'G') {
        counter[2]++;
    } else if (nucList.charAt(i) == 'T') {
        counter[3]++;
    } else if (nucList.charAt(i) == '-') {
        counter[4]++;
    }
int[] counterNucs = Arrays.copyOfRange(counter, 0,  4);
filePrint.println("Nuc. Counts: " + Arrays.toString(counterNucs));
incapacitated
  • 78
  • 1
  • 5
  • Oh yeah, I just left out that `counter[]++` part for ''A''. Thanks for pointing that out! – Molly Taylor Nov 22 '17 at 04:39
  • @incapacitated: I think you also need to change Arrays.copyOfRange(counter, 0, 4) to Arrays.copyOfRange(counter, 0, 5) – Zenith Nov 22 '17 at 04:48
1

You missed the statement inside if block.

You also have to change the Arrays.copyOfRange(counter, 0, 4) to Arrays.copyOfRange(counter, 0, 5) Your code should be the below.

String nucList = "CCATT-AATGATCA-CAGTT";
    int[] counter = new int[5];
    for (int i = 0; i < nucList.length(); i++) {
        if (nucList.charAt(i) == 'A') {
            counter[0]++;
        } else if (nucList.charAt(i) == 'C') {
            counter[1]++;
        } else if (nucList.charAt(i) == 'G') {
            counter[2]++;
        } else if (nucList.charAt(i) == 'T') {
            counter[3]++;
        } else if (nucList.charAt(i) == '-') {
            counter[4]++;
        }
    }
    int[] counterNucs = Arrays.copyOfRange(counter, 0, 5);
    System.out.println("Nuc. Counts: " + Arrays.toString(counterNucs));

For the above code result is Nuc. Counts: [6, 4, 2, 6, 2]

Zenith
  • 1,037
  • 1
  • 10
  • 21