Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1478
  • Last Modified:

user-defined method

I'm trying to write a program that prompts the user to input a sequence of characters and outputs the number of vowels.
I have to use the method isVowel.

and i got 2 errors:
1) public static boolean isVowel(char ch)  --> illegal start of expression
2) if (isVowel(userInput.charAt(i)) == true) --> cannot resolve symbol

here's my codes:
import java.io.*;

public class Vowel
{
      static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
      
      public static void main(String[] args)throws IOException
      {
            //declares variables
            int stringLength;
            int counter = 0;
            int i;

        String inputLine;

            System.out.println("enter a sequence of characters: ");
            System.out.flush();
            inputLine = keyboard.readLine();
            
            //evaluates the length of user's input
            stringLength = inputLine.length();

            for(i = 0; i <= (stringLength - 1); i++)
            {
                  //calls method isVowel
                  if (isVowel(userInput.charAt(i)) == true)
                  {
                        counter++;
                  }
        }

        System.out.println("The number of vowels in this sequence: " + counter);

            //creates method isVowel
            public static boolean isVowel(char ch)
            {
                  if((ch == 'a')||(ch == 'e')||(ch == 'i')||(ch == 'o')||(ch == 'u'))
                  {
                        return true;
                   }
              
                    else
                   {
                    return false;
              }
          };
      }
}

please teach me.....
0
crystalfish0318
Asked:
crystalfish0318
1 Solution
 
sre23Commented:
>> if (isVowel(userInput.charAt(i)) == true)
This should be:

if(isVowel(inputLine.charAt(i)) == true)


And you second problem is you just have a parenthesis in the wrong place.  Your first method main() doesn't have an ending parenthesis.  Here is the correct code:

import java.io.*;

public class Vowel
{
     static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
     
     public static void main(String[] args)throws IOException
     {
          //declares variables
          int stringLength;
          int counter = 0;
          int i;

        String inputLine;

          System.out.println("enter a sequence of characters: ");
          System.out.flush();
          inputLine = keyboard.readLine();
         
          //evaluates the length of user's input
          stringLength = inputLine.length();

          for(i = 0; i <= (stringLength - 1); i++)
          {
               //calls method isVowel
               if (isVowel(inputLine.charAt(i)) == true)
               {
                    counter++;
               }
        }

        System.out.println("The number of vowels in this sequence: " + counter);
    }

          //creates method isVowel
          public static boolean isVowel(char ch)
          {
               if((ch == 'a')||(ch == 'e')||(ch == 'i')||(ch == 'o')||(ch == 'u'))
               {
                    return true;
                 }
             
                  else
                 {
                  return false;
             }
         };
     
}




0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now