Note that there are some explanatory texts on larger screens.

plurals
  1. POJava program that does simple string manipulations is not working
    text
    copied!<p>I have to do a java lab that will take an inputted sentence and create a new string with only the words beginning with vowels. </p> <p>Example: </p> <blockquote> <p>input: It is a hot and humid summer day.</p> <p>output: Itisaand. </p> </blockquote> <p>EDIT: The output I get is anananananan.</p> <p>Here's my code (with comments). The methods are pretty simple. Im just not sure why this isn't working. </p> <pre><code>import java.util.*; 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); // class has constructor for input int beg = 0, end = 0; for (int j = 0; j &lt; input.length(); j++) { if (strings.isVowel(j) &amp;&amp; (input.charAt(j - 1) == ' ' || j == 0)) beg = j; // isVowel finds if there is a vowel at the location else if (strings.endWord(j)) end = j - 1; // endWord finds if there is a space or punctuation at the location if (beg == end - 2) strings.findWord(beg, end); // findWord adds a substring of input from beg to end to the answer } System.out.print("\nOutput: "); strings.printAnswer(); // printAnswer prints the answer } } </code></pre> <p>EDIT: Here is the code for my class.</p> <pre><code>public class Class { String input = "", answer = ""; public Class(String input1) { input = input1; } public boolean isVowel(int loc) { return (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 + "."); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload