Andrew Tran
Andrew Tran

Reputation: 11

Removing white spaces from string using loops

Hi guys I'm trying to remove white spaces using loops specifically. Heres what I've come up with so far

    import java.util.Scanner;
    public class Q2 {

public static void main(String[] args) {

    String input = "";
    char noSpace = ' ';

    Scanner scan = new Scanner(System.in);
    input = scan.nextLine();
    System.out.println(input);

    for (int i = 0; i < input.length(); i++) { //search from right to left
        for (int j = input.length(); j != -1; j--) { //search from left to right
            if (input.charAt(i) == noSpace) { //if there is a space move position of i and j
                i++;
                j--;
            }




    }
    System.out.println(input);

I am still quite new to java, any suggestions would be great thanks!

Upvotes: 0

Views: 6573

Answers (6)

Paph
Paph

Reputation: 11

import java.util.Scanner;
public class Iterations{
public static void main(String[] args){
    Scanner kb = new Scanner(System.in);
    System.out.print("Enter a sentence: ");
    String s = kb.nextLine();
    String temp = "";
    for (int i = 0; i < s.length(); i++){    //This loops separates the string into each character
        String c = s.substring(i, i+1);              
        if (c.equals(" ")){
            System.out.print(c.trim());      //If the individual character is space then remove it with trim()
        } else {
            temp = temp + c;                 //Adds the string up into single sentence
        }
    }
    System.out.println(temp);               //Print it to have a nice line of string
}

}

I am also new to java and happen to do problems that remove spaces with only some methods and loops. Thats my solution, feel free to try it out.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347194

And a combination of themes...

StringBuilder result = new StringBuilder(64);
String str = "sample test";
for (Character c : str.toCharArray()) {
    if (!Character.isWhitespace(c)) {
        result.append(c);
    }
}

System.out.println(result.toString()); // toString is not required, but I've had to many people assume that StringBuilder is a String
System.out.println(str.replace(" ", ""));
System.out.println("Double  spaced".replace(" ", ""));

Basically, nothing new, just runnable examples of what every body else has spoken about...

Upvotes: 1

Bharat Sinha
Bharat Sinha

Reputation: 14363

You've actually gone too far by keeping two loops you could do it in one only:

public static void main(String[] args) {
    String input = "";
    char space = ' ';

    Scanner scan = new Scanner(System.in);
    input = scan.nextLine();
    System.out.println(input);

    for (int i = 0; i < input.length(); i++) { // search from right to left char by char
        if (input.charAt(i)!= space) { // if the char is not space print it. 
            System.out.print(input.charAt(i));
        }
    }
} 

Upvotes: 0

Crazenezz
Crazenezz

Reputation: 3456

Try this one:

public class RemoveWhiteSpace {

    public static void main(String[] args) {

        String str = "Hello World... Hai...             How are you?     .";
        for(Character c : str.toCharArray()) {
            if(!Character.isWhitespace(c)) // Check if not white space print the char
                System.out.print(c);
        }
    }
}

Upvotes: 4

Sathish
Sathish

Reputation: 4713

public class sample {

public static void main(String[] args) {

    String input = "sample test";
    char noSpace = ' ';
    System.out.println("String original:"+input);

    for (int i = 0; i < input.length(); i++) { //search from right to left
        if (input.charAt(i) != noSpace) { //if there is a space move position of i and j
            System.out.print(input.charAt(i));
        }
     }

    }
   }

Upvotes: 0

ymn
ymn

Reputation: 2153

Why you do not use regular expressions? replaceAll("\\s","") removes all whitespaces. Also you can remove other non visible symbols, such as \tab etc.

Look at docs.oracle.com for more info

Upvotes: 1

Related Questions