-2

I have been presented with a problem for my Java class and I have no idea how to go about it. My professor barely covered loops. I have no idea how to solve this.

Here is the project objective:

Reads a string and prints out all vowels contained in that string.

Vowels are A E I O U a e i o u.

Input: the value of s, a string

Output: a string containing all the vowels in s, in the order in which they appear in s

public class GetVowels
{
    public static void main(String[] args)
    {
        String r = "";

        Scanner in = new Scanner(System.in);
        String s = in.nextLine();

        System.out.println(r);
    }
}
Baz
  • 36,440
  • 11
  • 68
  • 94
Rimshot
  • 11
  • 2
  • 7
  • 5
    study up on loops: [java for loop](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html), [java while loop](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html). – pb2q Oct 05 '12 at 14:42
  • 1
    Well, have you tried reading documentation and tutorials about loops? http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html and http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html for example. – Jon Skeet Oct 05 '12 at 14:42
  • 5
    It's a good brain-exercise.. do it on paper first! We sometimes jump on the keybard too quik. The PC won't replace our noggin – Caffeinated Oct 05 '12 at 14:44
  • 1
    @Adel more programmers in the workplace need to follow that guidance too. – Cuga Oct 05 '12 at 15:06

2 Answers2

5

Some ideas:

  • Store a list of all the vowels you want to remove
  • Convert everything to one case (toUpperCase() or toLowerCase())
  • Loop through each character, test if they're equal to the vowels being sought
  • If they match, record the one that matched

For some guidance on loops, particularly on iterating over a String:

https://stackoverflow.com/a/2451660/101095

The easiest way to for-each every char in a String is to use toCharArray():

for (char ch: "xyz".toCharArray()) {
}
Community
  • 1
  • 1
Cuga
  • 17,668
  • 31
  • 111
  • 166
  • 1
    I guess the op is having trouble using loops. Or in other words: He doesn't know anything about loops ... – Fildor Oct 05 '12 at 14:46
  • @Fildor - And he says "My professor barely covered loops" . Let's talk to his professor about this... Loops are so vital though. OP need to figure it ouit – Caffeinated Oct 05 '12 at 14:47
  • :) agreed. At least he could have given a hint, where to look. – Fildor Oct 05 '12 at 14:48
  • 1
    I could just write an exact solution, but that'd cut out his chance to learn... which is what the real goal is here. His professor might also get very curious if he has a perfect solution but no clue how to write a loop. – Cuga Oct 05 '12 at 14:49
  • 1
    Instead of converting all to lower or upper case you can try String.equalsIgnoreCase(), maybe it's a bit more elegant. – NickDK Oct 05 '12 at 14:58
  • I'm no looking for a quick fix. I'm coming from no programming background what so ever! The teacher I have barley speaks english. He gets frustrated incredibly easy when you sit down and ask him a question. He is of no help and I thought I could at least get some explanations here. I'm learning Java because I WANT to learn java, not that I have to. Assume all you want, im just asking a question. – Rimshot Oct 05 '12 at 15:00
0

What you might want is a "for" loop : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

You'll need the length of your input : String.length() for the upper bound of the loop. Remember that Java Strings' index start at 0!

Inside the loop you then examine character by character.

Baz
  • 36,440
  • 11
  • 68
  • 94
Fildor
  • 14,510
  • 4
  • 35
  • 67