Blackout621
Blackout621

Reputation: 43

Replacing Vowels With Asterisks

I have an assignment where I must take a user-inputted name and convert vowels to asterisks. I have that portion of the assignment down pact (as you'll see in my code below), but there's another part to it: if the entered string has an even amount of characters, there should be two instead of one spaces between the first and last name. If it's odd, it should only be one. I've attempted to implement this with an if-else statement, but it does not work properly.

Help would be appreciated. Thank you.

    import java.util.*;
    import java.io.*; // necessary for user input

    public class Prog510a
    {
      public static void main(String[] args)
      {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter your name: ");
       String word = input.nextLine();

       int length = word.length();
       String space = "";
       for(int count = 0; count < word.length(); count ++)
       {
           char c = word.charAt(count);
           if(c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O'
           || c == 'u' || c == 'U')
           {
            String front = word.substring(0, count);
            String back = word.substring(count + 1);
            word = front + "*" + back;
           }
       }
        if (length % 2 == 0)
        {
          space = "  ";
        }
        else
        {
          space = " ";
        }

       System.out.println(word);
      }

} 

Upvotes: 0

Views: 6093

Answers (3)

Shadow
Shadow

Reputation: 4016

Well, you might want to first split the full name into the first and last name. This can be done using the split function, which returns a String array:

string[] firstAndLastNames = word.split(" ");

This would of course be done after you've replaced the vowels, but it doesn't have to be.

Then you just have to get the length of the full name, and assign to the space variable appropriately:

if ((firstAndLastNames[0].length() + firstAndLastNames[1].length()) % 2 == 0)
    space = "  ";
else
    space = " ";

Then when you output it, do something like:

System.out.println(firstAndLastNames[0] + space + firstAndLastNames[1]);

I would also reccomend a better way of replacing the vowels with asterisks, because it's quite messy. It might be better to create an array of vowels, and use the equalsIgnoreCase function to replace:

string[] vowels = {"a","e","i","o","u"};
for (int i = 0; i < vowels.length; i++}
    for (int j = 0; j < word.length; j++)
        if (word[j].equalsIgnoreCase(vowel[i]))
            word[j] = "*";

Since you haven't learnt arrays yet, you could just use strings replace function to replace the space with either one space or two depending on the length, something like:

if (word.length() % 2 == 0)
    word = word.replace(" ","  ");

You don't need to check if it's odd, because it will already have 1 space, which is what you want.

Upvotes: 2

striving_coder
striving_coder

Reputation: 798

Here's a shorter version for you:

public class NameVowelsToAsterisks{

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String word = input.nextLine();

        String astWord = word.replaceAll("[AEIOUYaeiouy]", "*");  //changing to asterisks

        String spaceWord = new String();
        if (astWord.length() % 2 == 0) {
            int spacePos = astWord.indexOf(' ');    //finding position of space
            spaceWord = new StringBuilder(astWord).insert(spacePos, " ").toString(); //adding space if needed
        } else spaceWord = astWord;

        System.out.println(spaceWord);  //printing result
}

Upvotes: 2

Tahar Bakir
Tahar Bakir

Reputation: 716

First i think you should use replace instead of using that big ugly loop.

A tutorial : java string replace example

The documentation : java api : in this one look for the methods replace, replaceAll, replaceFirst

Hope this helps

Upvotes: 1

Related Questions