user3764862
user3764862

Reputation: 25

Stringbuilder java

I'm trying to store a string and determine whether it is a uppercase/lowercase letter or number, If the character is a lower case letter, redisplay the character in upper case, if the character is a upper case letter, redisplay the character in lower case if the character is a digit, display it as an asterisk. I could make it with the numbers but if I include .toString().toLowerCase() or .toUpperCase() for the letters the program keeps looping. What should I do?

public class CharacterArray {
    public static void main(String[] args) {
        StringBuilder input = new StringBuilder("800 Test St NY");
        for (int i = 0; i < input.length(); i++) {
            System.out.println(input.charAt(i));

            if(Character.isDigit(input.charAt(i))){
                input.replace(i,i+1,"*");
            }
            else if(Character.isUpperCase(input.charAt(i))) {
                input.replace(i, i+1,input.toString().toLowerCase());
            }
            else if(Character.isLowerCase(input.charAt(i))) {
                input.replace(i, i+1,input.toString().toUpperCase());
            }
            System.out.println(input);
        }
    }
 }

Upvotes: 0

Views: 1679

Answers (4)

Volune
Volune

Reputation: 4339

Since you want to replace only one character, you should use StringBuilder#setCharAt:

if (Character.isDigit(input.charAt(i))) {
    input.setCharAt(i, '*');
} else if (Character.isUpperCase(input.charAt(i))) {
    input.setCharAt(i, Character.toLowerCase(input.charAt(i)));
} else if (Character.isLowerCase(input.charAt(i))) {
    input.setCharAt(i, Character.toUpperCase(input.charAt(i)));
}

This way you know you won't change the length of the string.

Upvotes: 2

user3487063
user3487063

Reputation: 3682

You need to use Character#toUpperCase and Character#toLowerCase

public static void main(String[] args){


            StringBuilder input = new StringBuilder("800 Test St NY");
            for (int i = 0; i < input.length(); i++) {
                System.out.println(input.charAt(i));

                if(Character.isDigit(input.charAt(i))){
                    input.replace(i,i+1,"*");
                }
                else if(Character.isUpperCase(input.charAt(i))) {
                    input.replace(i, i+1,Character.toString(Character.toLowerCase(input.charAt(i))));
                }
                else if(Character.isLowerCase(input.charAt(i))) {
                    input.replace(i, i+1,Character.toString(Character.toUpperCase(input.charAt(i))));
                }
                System.out.println(input);
            }
        }

prints (the output of above code:)

8
*00 Test St NY
0
**0 Test St NY
0
*** Test St NY

*** Test St NY
T
*** test St NY
e
*** tEst St NY
s
*** tESt St NY
t
*** tEST St NY

*** tEST St NY
S
*** tEST st NY
t
*** tEST sT NY

*** tEST sT NY
N
*** tEST sT nY
Y
*** tEST sT ny

Upvotes: -1

khelwood
khelwood

Reputation: 59095

When you do this:

input.replace(i, i+1, input.toString().toLowerCase());

you are replacing one character with the whole of your string (in lower case), making input longer and longer so you'll never get to the end of it.

Instead:

input.replace(i, i+1, input.substring(i,i+1).toLowerCase());

Similarly where you convert to upper case.

Edit: fixed passing wrong argument type to replace.

Volune's answer looks better still. Use that.

Upvotes: 2

Jean Logeart
Jean Logeart

Reputation: 53809

Using StringUtils.swapCase:

String res = StringUtils.swapCase(original).replaceAll("\\d","*");

Upvotes: 0

Related Questions