0

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); } } } 
3
  • 1
    Fix your indentation please Commented Sep 18, 2014 at 21:39
  • 1
    Is there a reason you're using StringBuilder and not just String? Commented Sep 18, 2014 at 21:41
  • 1
    I'm guessing because it is close to being a mutable String. Commented Sep 18, 2014 at 21:41

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Character.toLowerCase(input.charAt(i)) is a char and not accepted by the replace method. Try input.substring(i, i + 1).toLowerCase() instead.
I believe that both beginning and ending index should be i (the same). replace() method specifies that both indexes are inclusive.
The method replace(int, int, String) in the type StringBuilder is not applicable for the arguments (int, int, char) :/
@PM77-1 no it doesn't. It says the end is exclusive.
2

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.

Comments

0

Using StringUtils.swapCase:

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

Comments

-1

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 

6 Comments

Why downvote? this is working, please run the code and see if its failing and then downvote.
@user3764862, thanks for checking. If you believe its working please upvote. :)
I don't have enough reputation to vote but I really appreciate your help :)
It is the wrong output. Use one of the answers that was upvoted to get the correct output.
@user3487063, you are correct. The mistake was in Volune's solution. Instead of converting lower to upper, he tried to convert lower to lower in his last else if statement. My apologies for comparing the wrong output to yours.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.