0

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!

5 Answers 5

1

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 !

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for that, the only thing is that I have to have a separate method (createArray()) that creates the 100 character array and outputs the 100 characters.
Is there anyway for me to keep my createArray() and countArray() method?
Sorry bro couldn't get what you mean by keep ? For main function, you just need to: public static void main(String []as){ countArray(); } Inside countArray() you have already called createArray() .
Sorry, maybe I don't understand your answer correctly. You have me creating the 100 character array in the countArray() method, but I have to have that array created in the createArray() method. Or am I reading that wrong, and I just have to alter my countArray() method to be like yours above?
Never mind, I see that when you create the 100 character array, you call upon the createArray() method to fill it. Thank you for your reply, you fixed my issue.
|
0

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
}

1 Comment

I assume you mean my method createArray(), not generateChar()? And it does work. So if I am grasping what you are saying, you're saying I don't need my createArray() method? My instructions were to create a method (createArray()) that creates a 100 character array, then output those 100 characters. Then I have to create a method (countArray()) that counts the amount of times each vowel was generated in the array. So I can't omit that method..
0
    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]);

Comments

0

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']);
        }
    }

Comments

0

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

1 Comment

You should explain the code you've posted as your answer.

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.