Java Case Switcher

2019-07-28 22:42发布

问题:

So I'm writing a little return program that switches the case of characters in a string, so HELLo becomes hElLo and hello becomes HeLlO, and aaa becomes AaA. I'm having a bit of trouble though. I'm still new and learning java, so sorry if this breaks rules:

public static String altCase(String text){
      String str = "";
      for (int i = 0; i <= text.length()-1; i++)
      {
        char ch = text.charAt(i);
        boolean lastIsUpperCase = true;
        if(Character.isUpperCase(i-1))
        {
          lastIsUpperCase = true;
        }
        else if(Character.isLowerCase(i-1))
        {
          lastIsUpperCase = false;
        }

        if(lastIsUpperCase)
        {
          str += Character.toLowerCase(ch);
        }   
        else if (!lastIsUpperCase)
        {
          str += Character.toUpperCase(ch);
        }
      }
      return str;
 }

回答1:

You should add the char to your if clause as in:

      String str = "";
      for (int i = 0; i <= text.length()-1; i++)
      {
        char ch = text.charAt(i);

        if(Character.isUpperCase(ch))
        {
          str += Character.toLowerCase(ch);
        }
        else if(Character.isLowerCase(ch))
        {
            str += Character.toUpperCase(ch);
        }


      }
      return str;
 }


回答2:

I would use a StringBuilder to build up the return value, and you could also use a String.toCharArray() and a for-each loop. Finally, you can test if a character is lower case (or upper case) and then swap the case. Something like,

public static String altCase(String text) {
    if (text == null || text.isEmpty()) {
        return text;
    }
    StringBuilder sb = new StringBuilder(text.length());
    for (char ch : text.toCharArray()) {
        if (Character.isUpperCase(ch)) {
            sb.append(Character.toLowerCase(ch));
        } else if (Character.isLowerCase(ch)) {
            sb.append(Character.toUpperCase(ch));
        } else {
            sb.append(ch);
        }
    }
    return sb.toString();
}


回答3:

You could make a char array and then check and swap each element individually. When you are done just pass that char array to a string as argument to make it into a string

char[] word = new char[3];
String str = new String(word); 


回答4:

So I managed to do it.

public static String altCase(String text){
      String str = "";
      str += Character.toUpperCase(text.charAt(0));
      for (int i = 1; i <= text.length()-1; i++)
      {
        char ch = text.charAt(i);
        boolean lastUp = flipFlop(i);
        char temp = switcher(ch, lastUp);
        str+=temp;
      }
      return str;
 }
 public static boolean flipFlop (int i){
      boolean bool = true;
      if(i==1){
        bool = true;
      }
      else if((i%2)==0)
      {
           bool = false;
      }
      else if((i%2)!=0)
      {
           bool = true;
      }
      return bool;
 }
 public static char switcher (char ch, boolean lastUp){
   char temp = ch;
   if(lastUp){
        temp = Character.toLowerCase(ch);
   }
   else if (lastUp==false){
     temp = Character.toUpperCase(ch);
   }
   return temp;
 }

I added a 'flipFlop' method to track the iterations, and the method 'switcher' changes between upper and lower case based on the condition of the last char. (lastUp is true when the last character in the string is uppercase).