java实现 删除输入的多余空格

/*
* lfsfxy9 @ 09.12.25
* 主要用于去除字符输入过程中多输入的空格。
* 此代码为Java作业
*/
import java.io.*;
public class fileinout {

public static void main(String[] orgs){
char c;
int c1;
int a=0;//空格计数用 。
File filePath=new File("temp");
if(!filePath.exists()) filePath.mkdirs();
File f1=new File(filePath,"d1.txt");
try{
FileOutputStream fout=new FileOutputStream(f1);
System.out.println("请输入字符串,以#作为结束符 .");
while ((c=(char)System.in.read())!='#'){
fout.write(c);
}
fout.close();
System.out.println("打印从文件中读入的数据 ");
FileInputStream fin=new FileInputStream(f1);
c1=fin.read();
System.out.print((char)c1);
while ((c1=fin.read())!=-1){
if((char)c1==' '){
a=a+1; //空格计数.
}
else{
a=0;
}
if(a>=2){
//
}
else{
System.out.print((char)c1);
}
}
System.out.print("\n");
fin.close();
}
catch (FileNotFoundException e){System.err.println(e);}
catch (IOException e){System.err.print(e);}
}
}

虽说功能实现了,可是从浅层看却没涉及到面向对象的东西,总体来说,其实实现这个功能可用方法很多,比如单独做一个检测类,来删除多余的空格。我想得比较省事儿,算是异曲同工之妙吧(但面向对象的好多优势没有发挥出来。。 你知道是哪些吗?~),只是在字符输出的同时进行一下检测、删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值