-4

I'm new to java and I cant find the answer to this problem.

If I have two character arrays like this with counters:

Character[] abc = {'A','B','C'};
int countABC = 0;

Character[] def = {'D','E','F'};
int countDEF = 0;

And a string like this:

String something = "ABCDEFGHABAB";

How to increase the counters?

Spangen
  • 4,420
  • 5
  • 37
  • 42
Zemmel
  • 1
  • 3
    "How to increase the counters?": `count = count + 1;` or, shorter, `count++;`. – assylias Oct 11 '17 at 12:19
  • 1
    Can you elaborate a little more? What is the purpose and paste the code if you have tried anything – sSaroj Oct 11 '17 at 12:20
  • Either use a built-in function (like in the linked duplicate) or create a `HashMap`, iterate the `String` from left to right and, for each character, update the corresponding counter in the map. – Zabuzard Oct 11 '17 at 12:20
  • You haven't mentioned how the String `something` relates to your counters. We can guess you might be counting individual instances of A,B,C or D,E,F or perhaps you wand to count how many times the sequence "ABC" or "DEF" occurs – Spangen Oct 11 '17 at 12:23

1 Answers1

0

If you loop through each of the characters in the string and then within the loop use 2 'if' functions, one for each array then you can use the indexOf() function to search an array for that value. If the value is negative then it wasn't found and you don't increase the counter.

Here is some more info on indexOf(): https://www.tutorialspoint.com/java/java_string_indexof.htm

Dean
  • 396
  • 3
  • 10