Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Java Spacing and Astericks

$
0
0
Hi There,


I have an assignment due which I've been trying to finish, the problem is I am about 99% complete I just need that extra push to complete that last 1%. The description is as follows:
Write a program that will allow the user to input their name from the keyboard. Replace all the vowels in their name with asterisks (“*”). Print out the modified name centered horizontally on the monitor. If the name has an even number of characters insert an additional space between the first and last name.

The output should look like this:
Enter your name: Gary Langner
G*ry L*ngn*r
Enter your name: Alan Alda
Al*n Ald*
Enter your name: Mickey Mouse
M*ck*y M**s*

My code looks like this:

import java.util.*;
import java.io.*;

public class vowel
{
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 = "";
String vowels = word.replaceAll("[aeiou]", "*");

if (length % 2 == 0)
{
space = "  ";
}
else
{
space = " ";
}

System.out.println(vowels);
}
} 


I can't figure out how to do the double space if the word is even.

Any help would be fantastic!

Viewing all articles
Browse latest Browse all 51036

Trending Articles