burgoyne
burgoyne

Reputation: 163

How to create a method to count characters in an array

So I have a method that creates an array of 100 randomly generated characters.

Here it is:

//method to generate random character between ch1 and ch2
   public static char getRandomCharacter(char ch1, char ch2)
   {

      return (char) (ch1 + Math.random() * (ch2 -ch1 +1));

   }

//==========================================   

   //method to assign generated characters (between a and z) to a 100 character array
   public static char[] createArray()
   {

      //declare a 100 character array
      char[] character = new char[100];

      //for loop assigning the random characters to the array using getRandomCharacter method
      for (int x = 0; x < character.length; x++)
      character[x] = getRandomCharacter('a', 'z');

      //for loop outputting the characters in the array
      for (int x = 0; x < character.length; x++)
      System.out.println(character[x]);

      return character;

   }

Now I need to create a method that takes the 100 generated characters, and counts how many times each vowel was generated. I am stuck with this one.

This is what I have so far:

public static void countArray()
   {

      int vowelA, vowelE, vowelI, vowelO, vowelU, vowelY;
      int elsePlaceHolder = 0;

      for (int x = 0; x < 100; x++)
      {

         if ((createArray()) == ('a'))
         vowelA++;

         else if ((createArray()) == ('e'))
         vowelE++;

         else if ((createArray()) == ('i'))
         vowelI++;

         else if ((createArray()) == ('o'))
         vowelO++;

         else if ((createArray()) == ('u'))
         vowelU++;

         else if ((createArray()) == ('y'))
         vowelY++;

         else
         elsePlaceHolder++;

      }

I believe I am correct with using a for loop to do this, but I don't think I'm executing it correctly. Once that executes I can display the amount of times the vowels were counted by using the int variables. That elsePlaceHolder variable is there because I did not know what to do with the else statement.

Can anyone point me in the right direction with my countArray() method? It would be much appreciated!

Upvotes: 0

Views: 1211

Answers (5)

Cho Seongil Bruce
Cho Seongil Bruce

Reputation: 1

hi i just changed your code a little. hope that helps you. check this out!

private static int vowelA=0;
private static int vowelE=0;
private static int vowelI=0; 
private static int vowelO=0;
private static int vowelU=0;
private static int vowelY=0;
private static int elsePlaceHolder = 0;

public static void main(String[] args) {

        char[] chs = createArray();
        System.out.println("vowelA : "+vowelA);
        System.out.println("vowelE : "+vowelE);
        System.out.println("vowelI : "+vowelI);
        System.out.println("vowelO : "+vowelO);
        System.out.println("vowelU : "+vowelU);
        System.out.println("vowelY : "+vowelY);
}

//method to generate random character between ch1 and ch2
   public static char getRandomCharacter(char ch1, char ch2)
   {

      return (char) (ch1 + Math.random() * (ch2 -ch1 +1));

   }

//==========================================   

   //method to assign generated characters (between a and z) to a 100 character array
   public static char[] createArray()
   {

      //declare a 100 character array
      char[] character = new char[100];

      //for loop assigning the random characters to the array using getRandomCharacter method
      for (int x = 0; x < character.length; x++){
      character[x] = getRandomCharacter('a', 'z');
      countArray(character[x]);
      }
      //for loop outputting the characters in the array
      for (int x = 0; x < character.length; x++)
      System.out.println(character[x]);

      return character;

   }

   public static void countArray(char ch)
   {

         if (ch == ('a')){
         vowelA++;}
         else if (ch == 'e'){
         vowelE++;}
         else if (ch == 'i'){
         vowelI++;}
         else if (ch == 'o'){
         vowelO++;}
         else if (ch == 'u'){
         vowelU++;}
         else if (ch == 'y'){
         vowelY++;}
         else{
         elsePlaceHolder++;}
   }

Upvotes: 0

Walter R
Walter R

Reputation: 543

Same as with the getRandomCharacter() method, you could try to make use of the int value of the characters and have an int array build for that with the size of the range between ch1 and ch2, then increase the value for each occurrence and then print the count for the vowels, something like:

public static void countArray(char ch1, char ch2, char[] character)
    {
        // Create an array with the letters you want to compare with: vowels
        int[] vowels = {(int) 'a', (int) 'e', (int) 'i', (int) 'o', (int) 'u'};

        // Create the array for the range between the two letters
        int[] dictionary = new int[(int) ch2 - (int) ch1 + 1];

        // Fill up the array with the ocurrence of each character
        for(int i = 0; i < character.length; i++){
            dictionary[((int)character[i] - (int) ch1)]++;
        }

        // Print the occurrence of each vowel
        for(int j = 0; j < vowels.length; j++){
            System.out.println((char) vowels[j] + ": " + dictionary[vowels[j] - (int)'a']);
        }
    }

Upvotes: 0

Jason
Jason

Reputation: 150

    char[] randomc = new char[]{'a','b','i','i','o','u','u','e','e','e','e','e','e','z','b','f'};
    int[] voweltable = new int[] {0, 0, 0, 0, 0, 0}; // Last element is for non-vowels
    for (int i = 0; i <= randomc.length - 1; i++)
    {
        switch(randomc[i])
        {
            case 'a':
                voweltable[0]++;
                break;
            case 'e':
                voweltable[1]++;
                break;
            case 'i':
                voweltable[2]++;
                break;
            case 'o':
                voweltable[3]++;
                break;
            case 'u':
                voweltable[4]++;
                break;
            default:
                voweltable[5]++;
                break;
        }
    }
    for (int i = 0; i <= voweltable.length -1; i++)
        System.out.println(voweltable[i]);

Upvotes: 0

Sidharth Patnaik
Sidharth Patnaik

Reputation: 93

I think you should modify the above code to this !

public static void countArray()
{

  int vowelA=0, vowelE=0, vowelI=0, vowelO=0, vowelU=0, vowelY=0;
  int elsePlaceHolder = 0;
  char [] arr = new char[100];
  arr=createArray();
  for (int x = 0; x < 100; x++)
  {

     if (arr[x] == 'a')
     vowelA++;

     else if (arr[x] == 'e')
     vowelE++;

     else if (arr[x] == 'i')
     vowelI++;

     else if (arr[x] == 'o')
     vowelO++;

     else if (arr[x] == 'u')
     vowelU++;

     else if (arr[x] == 'y')
     vowelY++;

     else
     elsePlaceHolder++;

  }
  System.out.print(vowelA+" "+vowelE+" "+vowelI+" "+vowelO+" "+vowelU+" "+vowelY);
}

It will work. Plus you havent initialized those vowel iterators but you have incremented them !

Upvotes: 1

smttsp
smttsp

Reputation: 4191

You will create the array once and count the number of elements.

char[] chars = createArray();

for(int i = 0; i < chars.length; i++){
    if(chars[i] == 'a')
        increment number of A's
    else if ...
}

instead of creating 5 elements, I'd create an array of size 5 and a would be arr[0], e is arr[1] etc.

In your code

int[] arr = new int[5];
for(int i = 0; i < chars.length; i++){
    if(chars[i] == 'a')
        arr[0]++;
    else if(chars[i] == 'e')
        arr[1]++;
    //etc
}

Upvotes: 0

Related Questions