How to remove all white spaces in java [duplicate]

2019-01-21 10:07发布

This question already has an answer here:

I have a programming assignment and part of it requires me to make code that reads a line from the user and removes all the white space within that line. the line can consist of one word or more.

What I was trying to do with this program is have it analyze each character until it finds a space and then save that substring as the first token. then loop again until it reaches no more tokens or the end of the line.

I keep on getting this when I try to compile it:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index   out of range: 1
    at java.lang.String.charAt(String.java:694)
    at trim.main(trim.java:23)

Here is the code

import java.util.Scanner ;
import java.lang.Character;
import java.lang.String ;
public class trim
{
        public static void main (String[]args)
        {

        String a  ;
        String b  ;
        String c ;
        char aChar ;
        int i = 0 ;

        Scanner scan = new Scanner(System.in);

        a = scan.nextLine();
        a =a.trim() ;


         for ( ; i >= 0 ; i++ )
         {
           aChar = a.charAt(i) ;
           if (aChar != 32)
           {
            a = a.substring(0,i+1);
           }
           else
           {
            b = a.substring(i,160) ;
            b= b.trim();
            c = c + a ;
            c = c.trim() ;
            a = b ;
            i = 0 ;
           }
           if (b.equals(null))
           {
            i = -1 ;
           }
         }
        }
}

easier ways to do this is appreciated, but I still want to get this program working.

and I can't use sentinels in the input.


Thanks everybody for all the help,

I will use the simpler method , and will read the javadoc.

14条回答
做自己的国王
2楼-- · 2019-01-21 10:29

You can use a regular expression to delete white spaces , try that snippet:

Scanner scan = new Scanner(System.in);
    System.out.println(scan.nextLine().replaceAll(" ", ""));
查看更多
戒情不戒烟
3楼-- · 2019-01-21 10:30

Replace all the spaces in the String with empty character.

String lineWithoutSpaces = line.replaceAll("\\s+","");
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-21 10:37
boolean flag = true;
while(flag) {
    s = s.replaceAll(" ", "");
    if (!s.contains(" "))
        flag = false;
}
return s;
查看更多
Summer. ? 凉城
5楼-- · 2019-01-21 10:38
String a="string with                multi spaces ";
//or this 
String b= a.replaceAll("\\s+"," ");

String c= a.replace("    "," ").replace("   "," ").replace("  "," ").replace("   "," ").replace("  "," ");

//it work fine with any spaces

*don't forget space in sting b

查看更多
该账号已被封号
6楼-- · 2019-01-21 10:40
String a="string with                multi spaces ";

String b= a.replace("    "," ").replace("   "," ").replace("  "," ").replace("   "," ").replace("  "," ");

//it work fine with any spaces

查看更多
别忘想泡老子
7楼-- · 2019-01-21 10:45
trim.java:30: cannot find symbol
symbol  : method substr(int,int)
location: class java.lang.String
b = a.substr(i,160) ;

There is no method like substr in String class.

use String.substring() method.

查看更多
登录 后发表回答