Solved

user-defined method

Posted on 2003-10-29
1
1,465 Views
Last Modified: 2008-03-04
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
Comment
Question by:crystalfish0318
1 Comment
 
LVL 1

Accepted Solution

by:
sre23 earned 20 total points
Comment Utility
>> 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now