0

In this program, I'm supposed to count the amount of times the letters 'A','B' and 'C' are in the string and return that as an array. The input is ABBACCCCAC and I'm supposed to get an output of [3, 2, 5] but I'm getting [1, 1, 2, 2, 1, 2, 3, 4, 3, 5]

import java.util.Arrays;
public class HelloWorld{

 public static void main(String []args){
    String str = "ABBACCCCAC";
    int[] arr = new int[str.length()];
    int acount = 0, bcount = 0, ccount = 0;
    
    for(int i =0; i<str.length();i++){
        if(str.charAt(i) == 'A'){
            acount++;
            arr[i] = acount;
        }
        else if(str.charAt(i) == 'B'){
            bcount++;
            arr[i] = bcount;
        }
        else if(str.charAt(i) == 'C'){
            ccount++;
            arr[i] = ccount;
        }
    }
    System.out.print(Arrays.toString(arr));
 }
}
2
  • What is your array even supposed to do? You store the current count for whatever letter is at that index up until that point. Ditch the array, and do System.out.print(Arrays.toString(new int[]{acount, bcount, ccount })); Commented Jul 23, 2020 at 23:54
  • What is your question? How to return an array? What is wrong with the counting in your array? Commented Jul 24, 2020 at 0:09

2 Answers 2

2

You were close. Just change your array size to 3 and the indiced to 0,1, and 2.

   String str = "ABBACCCCAC";
    int[] arr = new int[3];
    int acount = 0, bcount = 0, ccount = 0;
    
    for(int i =0; i<str.length();i++){
        if(str.charAt(i) == 'A'){
            acount++;
            arr[0] = acount;
        }
        else if(str.charAt(i) == 'B'){
            bcount++;
            arr[1] = bcount;
        }
        else if(str.charAt(i) == 'C'){
            ccount++;
            arr[2] = ccount;
        }
    }
    System.out.print(Arrays.toString(arr));

Your could also just increment the array locations directly.

arr[0]++; // like this.
Sign up to request clarification or add additional context in comments.

3 Comments

That being the case, why bother to update the array in the loop at all, when you can construct it after from the counters?
There are lots of ways to improve this and make it accommodate any number of characters. I was just helping the OP as they may be new at Java.
I'm not sure that helping them do something illogical is really helping them
0

Right now, you are assigning the counts to the array at each step of the counting, whereas you should have done this at the end of the count. Also, note that you created an array the same length of the string, rather than the expected 3.

You should just move the creation of the array to the end, with an array creation expression:

String str = "ABBACCCCAC";
int acount = 0, bcount = 0, ccount = 0;

for(int i =0; i<str.length();i++){
    if(str.charAt(i) == 'A'){
        acount++;
    }
    else if(str.charAt(i) == 'B'){
        bcount++;
    }
    else if(str.charAt(i) == 'C'){
        ccount++;
    }
}

int[] arr = new int[] { acount, bcount, ccount }; // <--- here!
System.out.print(Arrays.toString(arr));

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.