简体   繁体   中英

How to ignore spaces in String?

I'm trying do a code where transform a String into a "Dancing string". My code are working but I want ignore spaces " ", how can I do it?

Example:

Scanner input: Hello Guys

Output: Dancing sentence: HeLlO GuYs

I want this output: " HeLlO gUyS"

 import java.util.Scanner;


public class Main {
     String retorno="";
     
    Main(){
    }
String    concatena(String frase){
      String[] split = frase.split("");

    for(int i =0; i<split.length;i++){
         if(i%2==0){
         retorno +=split[i].toUpperCase();
        }
       else 
         retorno +=split[i].toLowerCase();
       }
         return retorno;
    }

    public static void main(String[] args) {
        Main teste = new Main();
        System.out.println("Write the dancing sentence: \n");
       Scanner scanner = new Scanner(System.in);
      teste.concatena(scanner.nextLine());
        System.out.println("Dancing sentence: "+teste.retorno);

    }
}

(i%2==0) doesn't have a way of considering a space on subsequent iterations, so could introduce a boolean and do something like this:

boolean toUpper = true;
for(int i =0; i<split.length;i++){
    if(split[i].equals(" ")) {
        retorno += split[i];
    } else if (toUpper) {
       retorno +=split[i].toUpperCase();
       toUpper = false;
    } else {
       retorno +=split[i].toLowerCase();
       toUpper = true;
   }
}

What you need to do is:

  1. if a character is a space ignore it.
  2. Otherwise alternatively convert characters to uppercase and lowercase.

A possible way to do this is:

 import java.util.Scanner;


public class Main {
     String retorno="";
    Main(){
    }
String    concatena(String frase){
      String[] split = frase.split("");
      Boolean flag = true;
    for(int i =0; i<split.length;i++){
        if(!split[i].equals(" ")){
            if(flag){
         retorno +=split[i].toUpperCase();
         flag = false;
        }
       else{
           retorno +=split[i].toLowerCase();
           flag = true;
       }
         
       }else{
            retorno +=split[i];
        }
    }
         
         return retorno;
    }

    public static void main(String[] args) {
        Main teste = new Main();
        System.out.println("Write the dancing sentence: \n");
       Scanner scanner = new Scanner(System.in);
      teste.concatena(scanner.nextLine());
        System.out.println("Dancing sentence: "+teste.retorno);

    }
}

What done here is basically we have a flag variable initialized to true. if this flag is true, we turn the current character to uppercase, if false we turn it to lowercase.

for each character we first check if its a space.

if(!split[i].equals(" ")){

if it is not, then we can turn it to upper or lower case based on the flag variable and toggle flag value for next character.

if(flag){
         retorno +=split[i].toUpperCase();
         flag = false;
        }
       else{
           retorno +=split[i].toLowerCase();
           flag = true;
       }

if it is a space, we simply append it to the result string without any modification.

retorno +=split[i];

This is just one way of doing it. There are many more you just need to take care of 2 things mentioned above.

Yet another way while sticking with your current theme:

public String concatena(String frase) {
    String[] split = frase.split("");
    int spaces = 0;
    for (int i = 0; i < split.length; i++) {
        String str = split[i].toUpperCase();
        if (str.equals(" ")) { 
            retorno += str;
            spaces++;
            continue; 
        }
        if ((i - spaces) % 2 == 0) {
            retorno += str;
        }
        else {
            retorno += str.toLowerCase();
        }
    }
    return retorno;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM