2014-02-16 56 views
0

我是Java新手。 我在信息从一个文本文件,其中一种看起来像这样写着:将空白空间更改为java

asdasd EE50 Psaefsdf 
asrt L38 sdfsdf 
lookhere  sdfsdfs 
sdfsdf 3PO dsfsdf 
more... 

我想知道是否可以删除该空的空间,其中数字通常会和它改为“ x“或什么的。如何做到这一点?

Something like this?: 

if(b.equals(" ")){ 
change to x or something? } 

相关的代码:

File filename = new File("a.file"); 
     Scanner Sc = new Scanner(filename); 
     while (Sc.hasNextLine()) {    
       String a = Sc.next(); 
       String b = Sc.next(); 
       String c = Sc.next(); 

之后他们添加到对象,所有。

+0

如果有'在列之间,也许你可以通过线,分割线读取和寻找空的空间\ t's? – alex

回答

0
String line ="....", newLine =""; 
for(int i=0; i<line.Length; i++){ 
    if(!(line.charAt(i).equals(' ') || line.charAt(i).equals('\t'))) 
     newLine+= line.charAt(i); 
    else 
     newLine+= "x"; 
} 

或者更好的使用StringBuilder为eficiancy