Welcome to Dream.In.Code
Become a Java Expert!

Join 149,613 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,848 people online right now. Registration is fast and FREE... Join Now!




Java Removing Vowels

 
Reply to this topicStart new topic

Java Removing Vowels

HeroOfEras
18 Sep, 2007 - 03:39 PM
Post #1

New D.I.C Head
*

Joined: 18 Sep, 2007
Posts: 1


My Contributions
Hey, well I did this code to remove vowels from words and sentences, but I was told it had to be simpler can anybody help me figure out a much simpler way to write this code?

CODE

import java.util.*;

public class NoVowels {

    private static final String VOWEL = "[aeiou]";
    private static final String VE = "";
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        s.useDelimiter(System.getProperty("line.separator"));
        String sentence;
        String letter="";
        int number;
        String word="";
        System.out.println("Enter a sentence ");
        sentence = s.next();
        StringBuffer temp = new StringBuffer(sentence);
        number = temp.length();
        for(int index =0; index < number; index++) {
               if(sentence.matches(VOWEL));
                word = sentence.replaceAll(VOWEL, VE);
        }

        System.out.println(word);
    }
}




Thanks

This post has been edited by HeroOfEras: 18 Sep, 2007 - 03:41 PM
User is offlineProfile CardPM
+Quote Post

alpha02
RE: Java Removing Vowels
18 Sep, 2007 - 05:21 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
Hi and welcome to DIC! Removing vowels from a String can be done the following way:

CODE
String sentence = "Hello world";
sentence = sentence.replace("a", "");
sentence = sentence.replace("e", "");
sentence = sentence.replace("i", "");
sentence = sentence.replace("o", "");
sentence = sentence.replace("u", "");
System.out.println(sentence); //Hll wrld


The replace(String a, String B ) method of the String class replaces all occurences of argument 1 by the second argument. This way, you can remove a certain character or word from the initial String. This object is immutable, meaning it does not change its state and you must assign the returned value to the initial String, like:

CODE
String a = a.replace("a", ""); //a will have its "a" characters removed.


If you need more help then just reply.

Hope this helps.

This post has been edited by alpha02: 18 Sep, 2007 - 05:27 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:25AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month