This was a problem involving an array of chars and uses recursion. I understand partly how to solve it, but I can't seem to output how many times the char a
appears in the char array. Here is the method that I came up with two variations I tried but to no success.
private static Int charCount(char[] test, int i, char c) {
int[] number = new int[26];
char c = 'a';
if (test.length == 0) {
return 0;
}
int count = 0;
if (test == a) {
count++;
}
return count + count(test[1], a);
}
for(c = 'a'; c <= 'z'; c++)
{
for( char x : test )
{
if( c == x )
{
c++;
}
}
}
So I think I understand this, but let me know if I don't. Your question is: How can I read the number of a value in an array? Correct? If so then here is some code which should work:
import java.util.*;
public class HowToReadCountOfDataInArray {
private static final char[] arr = newArr();
private static int ReadArrayValue(char[] arr, char value) {
int count = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] == value) {
count++;
}
}
return count;
}
public static void main(String[] args) {
System.out.println(ReadArrayValue(arr, 'a'));
System.out.println(arr);
}
}
P.S. Here is the "newArr()" method:
public static char[] newArr() {
char[] arr = new char[new Random().nextInt(100 + 1 - 1) + 1];
int aOrB = new Random().nextInt(2 + 1 - 1) + 1;
for(int i = 0; i < arr.length; i++) {
aOrB = new Random().nextInt(2 + 1 - 1) + 1;
if(aOrB == 1) {
arr[i] = 'a';
}else if(aOrB == 2) {
arr[i] = 'b';
}
}
return arr;
}