Recent Posts
SeC Gaming
the Lounge
New Lounge Topic
New Gaming Topic
We've moved to Discord

You are not connected. Please login or register

Java Advice Needed

5 posters

Go down  Message [Page 1 of 1]

1Java Advice Needed Empty Java Advice Needed 2012-01-12, 00:16

Bla125

Bla125

Since I haven't seen Khult on for a while I'm just gonna make this topic Smile

I made a program to remove vowels out of a phrase someone types. It works perfectly fine but the way I did it was a bit clunky. So what could I do to reduce the wall of code and make it... better. Here's the code, hope someone can help!

Code:
import java.util.*;

public class Tester{

    public static void main(String[] args){
       
        Scanner Input = new Scanner(System.in);
       
        System.out.print("Enter a phrase >> ");
        String Phrase = Input.nextLine();
       
        Phrase = Phrase.replaceAll("a", "");
        Phrase = Phrase.replaceAll("A", "");
       
        Phrase = Phrase.replaceAll("e", "");
        Phrase = Phrase.replaceAll("E", "");
       
        Phrase = Phrase.replaceAll("i", "");
        Phrase = Phrase.replaceAll("I", "");
       
        Phrase = Phrase.replaceAll("o", "");
        Phrase = Phrase.replaceAll("O", "");
       
        Phrase = Phrase.replaceAll("u", "");
        Phrase = Phrase.replaceAll("U", "");
       
        System.out.println(Phrase);
    }
}

2Java Advice Needed Empty Re: Java Advice Needed 2012-01-12, 09:01

Khult

Khult

You have the right idea Bla, it's just that you don't need a separate line of code for each vowel. Get rid of those lines of code and replace it with this:

Phrase = Phrase.replaceAll("(?i)[aeiou]", "");

That one line of code is all you need to remove all the vowels in a phrase. In case you're wondering, (?i) is a pattern modifier that makes the pattern case insensitive.

3Java Advice Needed Empty Re: Java Advice Needed 2012-01-12, 10:18

infadel117

infadel117

//no comment

4Java Advice Needed Empty Re: Java Advice Needed 2012-01-12, 13:42

Chewy

Chewy

I was gonna say put all the letters in an array and loop through it, but looks like Khult knows a much better and simpler way.

And Infadel I like how you used comment syntax to say you have no comment.

5Java Advice Needed Empty Re: Java Advice Needed 2012-01-12, 14:49

Duan

Duan

Infadel117 wrote://no comment

6Java Advice Needed Empty Re: Java Advice Needed 2012-01-13, 00:45

Bla125

Bla125

Thanks Khult!

Sponsored content



Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum