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);
}
}
}
-
1Fix your indentation pleasekhelwood– khelwood2014年09月18日 21:39:27 +00:00Commented Sep 18, 2014 at 21:39
-
1Is there a reason you're using StringBuilder and not just String?Roddy of the Frozen Peas– Roddy of the Frozen Peas2014年09月18日 21:41:27 +00:00Commented Sep 18, 2014 at 21:41
-
1I'm guessing because it is close to being a mutable String.Hovercraft Full Of Eels– Hovercraft Full Of Eels2014年09月18日 21:41:50 +00:00Commented Sep 18, 2014 at 21:41
4 Answers 4
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.
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 (the same). replace() method specifies that both indexes are inclusive.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
Using StringUtils.swapCase:
String res = StringUtils.swapCase(original).replaceAll("\\d","*");
Comments
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