Topic: 【原创】JAVA经典实例--处理字符串中的单个字符

  Print this page

1.【原创】JAVA经典实例--处理字符串中的单个字符 Copy to clipboard
Posted by: prettyxx
Posted on: 2003-12-18 11:09

问题:如何处理字符串中的单个字符
解决之道:
采用循环一级String类的charAt()方法
代码:
import java.io.*;

/** CheckSum - remove leading spaces
* @author Ian F. Darwin, ian@darwinsys.com
* @version $Id: CheckSum.java,v 1.2 2000/11/25 17:56:14 ian Exp $
*/

public class CheckSum {
public static void main(String[] av) {
CheckSum c = new CheckSum();
int sum = 0;
if (av.length == 0)
sum = c.process(new BufferedReader(
new InputStreamReader(System.in)));
else for (int i=0; i<av.length; i++)
try {
sum += c.process(new BufferedReader(new FileReader(av[i])));
} catch (FileNotFoundException e) {
System.err.println(e);
}
System.out.println(sum);
}

/** CheckSum one file, given an open BufferedReader.
*/
public int process(BufferedReader is) {
//+
int sum = 0;
try {
String inputLine;

while ((inputLine = is.readLine()) != null) {
int i;
for (i=0; i<inputLine.length(); i++) {
sum += inputLine.charAt(i);
}
}
is.close();
} catch (IOException e) {
System.out.println("IOException: " + e);
} finally {
return sum;
}
//-
}
//-
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923