Hi All,

First of all Here is the question and what I have done so far.

A) The helper method boolean isVowel (char c) should return true if the character it recieves is an uppercase or lowercase English vowel. For Our purposes, Vowels are the letters a, e, i, o, and u. The metod should do this by checking if the character c is contained in a string composed of vowel characters.

B) The public String shorthand(String in) method should use the class StringBuilder to build an output string by appending non-vowel characters from its argument in to the result it returns. You will need to identitfy vowels to be removed using the helper method you developed in part (a).

C) The run method of this class should contain a loop that repeatedly prompts the user to enter a line of text from the keyboard, using Scanner class to read the input.

After each line is entered and return is pressed , the method should repeat the text back to the user but with any vowels removed using shorthand method and the method should terminate if when user enters *

I have done all the code but not sure if it it correct and specially the last method.

import java.io.*;
import java.util.*;
import java.util.Scanner;

public class Shorthand
{    
   
    public boolean isVowel(char c)
   {
       
       if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A'|| c == 'E'||c == 'I'|| c == 'O'|| c == 'U')
       {
           return true;
       }
       else
       {
           return false;
       }
   }
    
    //TODO Complete the shorthand method
   public String shorthand(String in)  
{ 
  StringBuilder vowel = new StringBuilder(); 
 
  for (int i=0; i < in.length(); i++) 
  { 
      if (isVowel(in.charAt(i)) == false) 
      {  
        vowel.append(in.charAt(i));  
      } 
  }  
  return vowel.toString();  
} 

    
    //TODO Complete the run method
    public void run() throws IOException
    {
      String txt = ""; 
       Scanner sc = new Scanner(System.in);
      
       for(int i = 0; i < sc.length; i++)
       {
        System.out.println("Enter your line text");
        txt.append(shortHand);
        String line = sc.nextLine();
        if (line.equals("*")) break;
        String shortHandLine = shorthand(line);
        System.out.println(String.format("Short hand form", line, shortHandLine));
       }
    }
}

There are other loops as well. Not only the for loop. And check the API. How on earth did you came up with this: sc.length . You cannot just call whatever methods you imagine. That is why you have the API. To see which methods each class has.

Scanner sc = new Scanner(System.in);
boolean cont = true;

while (cont) {
   System.out.println("Enter value:");
   String s = sc.nextLine();

   if ("*".equals(s)) {
     cont = false;
  } else {
     // do stuff with the 's'
  }

}

There are other loops as well. Not only the for loop. And check the API. How on earth did you came up with this: sc.length . You cannot just call whatever methods you imagine. That is why you have the API. To see which methods each class has.

Scanner sc = new Scanner(System.in);
boolean cont = true;

while (cont) {
   System.out.println("Enter value:");
   String s = sc.nextLine();

   if ("*".equals(s)) {
     cont = false;
  } else {
     // do stuff with the 's'
  }

}

Thank you for correcting me in the last method. My trouble is that how can I use the shorthand method to print the text on screen without the vowels. I have been at it for days now can't figure it out

The shorthand method returns a String. So call it with argument the line given and print what it returns:

String result = shorthand(s);
System.out.println(result);

Hi thank you very much for your help it worked. Just another thing, if you can point me in the right direction if I want to print each string on a separate line in a text file. Am I on the right lines with this code.


PrintWriter pw = new PrintWriter("outfile.txt");
pw.println(result);

the txt file outfile.txt is in the same folder as the code.

really appreciate your help.

Hi thank you very much for your help it worked. Just another thing, if you can point me in the right direction if I want to print each string on a separate line in a text file. Am I on the right lines with this code.


PrintWriter pw = new PrintWriter("outfile.txt");
pw.println(result);

the txt file outfile.txt is in the same folder as the code.

really appreciate your help.

Create a new class with a main method. In that method put the above code and test it. Once you get it to work, use it in your program, or post the code and ask questions.

This question has already been answered. Start a new discussion instead.