R Hamilton
R Hamilton

Reputation: 263

Java: how to convert each char in a string to '*' except for spaces

I have been set a task to convert each char in a string to * except for any spaces in that string (the string is user input), the one caveat is that I must use a for loop. My code is below... the current problem that I am having is that when I try to use the char in an if statement condition I am getting a "Type mismatch: cannot convert from char to Boolean" error. All help appreciated...

public static void main(String[] args) {
    //declare vars
    Scanner input = new Scanner(System.in);
    String name = "";
    int length = 0;

    //get name string
    System.out.println("Enter your name");
    name = input.nextLine();

    //get length of name string
    length = name.length();

    //convert name string to array of characters
    char[] nameChars = name.toCharArray();

    //iterate over array of chars replacing each 
    // char with * and space with space
    for (int index=0;index==length;index++){
        //if the char is a space then do nothing
        if (nameChars[index] = ' ') {
        //else convert to *
        } else {
            nameChars[index] = '*';
        }
    }

    //convert array back to string and output
    String newName = new String(nameChars);
    System.out.println(newName);

    //close resources
    input.close();

}

Upvotes: 0

Views: 392

Answers (8)

leopcode
leopcode

Reputation: 74

String text = "It is a string";
System.out.println(text.replaceAll("[a-zA-Z]", "*"));

Output for the code will be : ** ** * ******.

Note that it's easier to use a StringBuilder when you want to modify String. You need to remember that Strings are immutable.

Upvotes: -1

user3437460
user3437460

Reputation: 17454

Java: how to convert each char in a string to '*' except for spaces

You can just do it in one line without any loops..

String str = pw.replaceAll("[^\\s]", "*");

TEST:

String str1 = "ASDAS sad!@# !@#^% ?".replaceAll("[^\\s]", "*");
System.out.println(str1);

OUTPUT:

***** ****** ***** *

Note: For char[], you can use String.valueOf(yourArray).replaceAll().

Upvotes: 0

Salavat Yalalov
Salavat Yalalov

Reputation: 114

Why do you want to invent a bicycle?
It's better to use the power of Java and exactly replaceAll method of String.class:

String input = "123 wer 456 zxcv!";
String result = input.replaceAll("[^ ]", "*");
System.out.println(result);

Upvotes: 0

Mehdi Bouzidi
Mehdi Bouzidi

Reputation: 1985

If you want to use your code, then your condition is wrong, you're affecting the space char to the current char if the string nameChars[index] = ' ' you must change it to nameChars[index] == ' ', note the double equal signe ==.

However, I suggest you use this simple condition, cause you'll not do anything when the current char is a space char, then you don't have to check if it's a space char:

 if (nameChars[index] != ' ') {
             nameChars[index] = '*';
            } 

Upvotes: 0

MGPJ
MGPJ

Reputation: 1072

Try the below approach

if (nameChars[index] == ' ') {
    //else convert to *
    } else {
        nameChars[index] = '*';
    }

Upvotes: 0

Bohemian
Bohemian

Reputation: 425098

Instead of writing all that code, try this:

String newName = name.replaceAll("\\S", "*");

The regex \S matches "any non whitespace character".

Upvotes: 2

Miraj50
Miraj50

Reputation: 4407

Use == in place of = for the if condition checking as == is the relational operator while = is the assignment operator.

Change index==length to index < length otherwise your loop will never run! Initially index = 0 while length is some positive value and index!=length then. So the loop will never run.

Upvotes: 0

Atimene Nazim
Atimene Nazim

Reputation: 419

You have to use == instead of = like below:

if (yourChar == ' ') {
//your code ...
}

Upvotes: 0

Related Questions