How do I make my method count the character of 'a' in the main char array?

KennyatJava

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++;
                   
               }
           }
       }
TheComputerKing

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I make my C program count more than just one character?

How do I find a character followed by a character in a char array Java?

Java program. How do I loop my "isLucky method through the array? and how do I print the result in the main method?

How do I make my JFrame class my main class?

Java program. How do I loop my "isLucky" method through my array? Also, how do I print my results correctly in the main method?

How do I make my JButton count and change my display?

If I declare a char array do I have to count the null character too?

What changes to my HTML and JavaScript code do I need to make, in order for my character_count to work?

How do I make my character jump using JavaFx?

How do I make a trail behind my character in SpriteKit?

How do I Implement swap array method in main class?

How do I return an array of int to a main method

How can I make backspace not count as a character?

How do I make a method in my Java class return findViewById?

How do i make my method more Object Oriented?

How do I make my loading screen stop and go into my main page?

How do I successfully implement my .h file to my main .cpp file to make it run without errors

How can I access a helper method for a boolean array in my main method?

how do i count the replaced char in a string?

How do I make my array available to other functions?

How do I make an array of objects conform to my protocol?

How do I pass an ArrayList of Objects from my main method to a method in another class (to then be sorted by a specific property)

How do I get both values from my property to be returned to my main method?

How do I delete duplicates in my array merging method?

How do I fix this memory leak in my resize array method?

I have an array of regex I am replacing with the same character. How would I use or "|" with my regex to lower array count?

How can I make my main if, elseIf and else to do the same thing as my main if does without getting "duplicate" errors?

How do I return a character array from char *functionName(int arrayname[SIZE]);

How do I make a Null character in Kotlin