Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java program that does simple string manipulation is not working correctly

Tags:

java

I wrote this program for school and it almost works, but there is one problem. The goal of the program is to take an inputted string and create a new string out of each word in the input beginning with a vowel.

Example:

input: It is a hot and humid day.
output: Itisaand.

Here is the driver:

public class Driver {

public static void main(String[] args) {

    Scanner console = new Scanner(System.in);
    System.out.print("Input: ");
    String input = console.nextLine();
    Class strings = new Class(input);
    int beg=0;
    for(int j=0;j<input.length();j++)
    {
        if(strings.isVowel(j)&&(j==0||input.charAt(j-1)==' '))
            beg=j;
        else if(strings.endWord(j)&&(beg==0||input.charAt(beg-1)==' '))
        {
            strings.findWord(beg, j);       
        }
    }
    System.out.print("Output: ");
    strings.printAnswer();
}

}

And here is the class:

public class Class {

    String input="",answer="";
    public Class(String input1)
    {
        input = input1;
    }


    public boolean isVowel(int loc)
    {
        return (input.charAt(loc)=='U'||input.charAt(loc)=='O'||input.charAt(loc)=='I'||input.charAt(loc)=='E'||input.charAt(loc)=='A'||input.charAt(loc)=='a'||input.charAt(loc)=='e'||input.charAt(loc)=='i'||input.charAt(loc)=='o'||input.charAt(loc)=='u');
    }
    public boolean endWord(int loc)
    {
        return (input.charAt(loc)==' '||input.charAt(loc)=='.'||input.charAt(loc)=='?'||input.charAt(loc)=='!');
    }
    public void findWord(int beg,int end)
    {
        answer = answer+(input.substring(beg,end));
    }
    public void printAnswer()
    {
        System.out.println(answer+".");
    }
}

With this code, i get the output:
Itisaa hotandand humidand humid summerand humid summer day.

By removing this piece of code:

&& (j == 0 || input.charAt(j-1) == ' ')

I get the proper output, but it doesn't work if an inputted word has more than one vowel in it.

For example:

input: Apples and bananas.
output: and.

Can someone please explain:
a) why the code is printing out words beginning with consonants as it is and
b) how I could fix it.

Also, the methods in the class I've written can't be changed.

like image 757
Anthony Graffigna Avatar asked May 26 '26 02:05

Anthony Graffigna


2 Answers

Here's a better algorithm:

  1. split the input into an array of words
  2. iterate over each word
  3. if the word begins with a vowel, append it to the output

The easiest way to split the input would be to use String.split().

Here's a simple implementation:

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    String input = console.nextLine();
    String[] words = input.split(" ");
    StringBuilder output = new StringBuilder();
    for (String s : words) {
        if (startsWithVowel(s)) {
            output.append(s);
        }
        else {
            output.append(getPunc(s));
        }
    }
    System.out.println(output.toString());
}

public static boolean startsWithVowel(String s) {
     char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
     char firstChar = s.toLowerCase().charAt(0);
     for (char v : vowels) {
         if (v == firstChar) {
             return true;
         }
     }
     return false;
 }

public static String getPunc(String s) {
    if (s.matches(".*[.,:;!?]$")) {
        int len = s.length();
        return s.substring(len - 1, len);
    }
    return "";
}
like image 186
jahroy Avatar answered May 27 '26 15:05

jahroy


The problem with your code was: It was counting the same word multiple times, due to it finding vowels and starting the word search process over again.

Heres how I went about solving the problem, while still keeping your code looking relatively the same: All I changed was your loop

for(int i=0;i<input.length();i++)
    {
        if(strings.isVowel(i) &&(i==0 || strings.endWord(i-1))){ 
            beg = i;
            for(int j = i; j < input.length();j++) //look for end of word
            {
                if(strings.endWord(j)) //word has ended
                {
                    i = j; //start from end of last word
                    strings.findWord(beg, j);         
                    break; //word done, end word search
                }
            }
        }
    }

As mentioned above, there are better ways to go about this, and there are some pretty glaring flaws in the setup, but you wanted an answer, so here you go

like image 45
EyeOfTheHawks Avatar answered May 27 '26 15:05

EyeOfTheHawks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!