0

I'm working on writing a program that requires two methods based on the user option. When prompted, the user selects whether or not they want to count vowels or words. Once they select their option, I need to print their result, and keep asking for more inputs until they enter "q". One of the methods outputs the number of vowels in a word, and the other puts out the numbers of words in a sentence, but it isn't working. I'm new to programming and have a very basic understanding, so my options for answers are limited. Here's what I have so far:

*I've fixed my = versus == sign issue. The first thing that comes up when I try to compile is incompatible types between char and a java string. I'm not sure how to tell the computer to look for a letter as an option to begin the rest of the program.

    import java.util.Scanner;
    public class MethodTester
    {
       public static void main (String[] args)
       { 
   Scanner in = new Scanner (System.in);
   System.out.println ("Please enter 'v' to count vowels or 'w' to count words.");
   String count=in.nextLine();
   char choice = count.charAt(0);
   boolean done = false;

   while (!done)
   {
   if (choice == "v")
   { 
       Sytem.out.println("Please enter a single word or press q to quit.");
       string str = in.nextLine();

       System.out.println(str+" has "+str.countVowels+"Vowels.");
       return countVowels;

    } else if (str == "q")
        {done = true;

        }  else if (choice == "w")
       { System.out.println("Please enter a sentence or phrase or press q to quit.");
         string str = in.nextLine(); 

         System.out.println(str+ " has "+ str.countWords+"words.");
         return countWords;



        } else if (str == "q")
        {done = true;
        }
        }
        System.out.println("You have quit.");  
}


       public static int countVowels (String str)
       {char c=str.charAt(0);
   int length = str.length();
   int count = 0;
   int location = str.charAt(0);

   do{
       if (c=="a" || c=="e" || c=="i" || c=="o" || c=="u" || c=="A" || c=="E" || c=="I" || c=="O" || c=="U" ){
        location++;
        count++;
        c++;
    }}
    while(location<length);
    return count;
}
public static int countWords (String str)
{
    int countWords=0;
    boolean word = false;
    int endStr = str.length() -1;

    for (int i=0; i < str.length(); i++)
    {
        if (Character.isLetter(str.charAt(i)) && i != endStr)
        {
            word = true;
        }
        else if (!Character.isLetter(str.charAt(i)) && word)
        {
            countWords++;
            word = false;
        }
        else if (Character.isLetter(str.charAt(i)) && i ==endStr)
        {
            countWords++;
        }
    }
    return countWords;
}   
    }
Colleen
  • 13
  • 4

0 Answers0