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);
 }
 }
 }
Roddy of the Frozen Peas
15.3k11 gold badges63 silver badges111 bronze badges
asked Sep 18, 2014 at 21:38
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.

answered Sep 18, 2014 at 21:44
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.

MarGar
4655 silver badges12 bronze badges
answered Sep 18, 2014 at 21:49

Comments

0

Using StringUtils.swapCase:

String res = StringUtils.swapCase(original).replaceAll("\\d","*");
answered Sep 18, 2014 at 21:46

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
answered Sep 18, 2014 at 21:50

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.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.