java面试题 读取a.txt文件,算出里面有多少个字母和多少个大写字母 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 大写字母和小写字母的ACsii码不一样,从这一点入手,不是很难。 可以用bufferreader来读取 每次比较Accii码或者就直接比较A-Z a-z public static void main(String[] args) { BufferedReader r = null; int m = 0,n=0; try { r = new BufferedReader(new FileReader(new File("a.txt"))); String s = null; StringBuffer ss= new StringBuffer(); while((s = r.readLine())!=null) ss.append(s); for(int i = 0; i<ss.length(); i++){ char c = ss.charAt(i); if(c>='a'&& c<'z') m++; else if(c>='A'&& c<='Z') n++; } System.out.println("小写:"+m+",大写:"+n); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { r.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private final static void counting(String fileName) { try { InputStreamReader reader = new InputStreamReader(new FileInputStream(fileName)); char[] buffer = new char[1024]; int letters = 0; int cLetters = 0; while (reader.read(buffer, 0, 1024) != -1) { for (char c : buffer) { if ((c >= 65 && c <= 91) || (c >= 97 && c <= 123)) { letters++; if (c >= 97 && c <= 123) { cLetters++; } } } } System.out.println("所有字母:" + letters); System.out.println("大写字母:" + cLetters); } catch (FileNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } Map中Entry内部类 从1到100中,如果其中有一个数丢了,如何找出这个数,有几种方法?集合框架怎么做呢? 希望前辈帮忙解答个初级问题. 帮忙找个错误。急! jdk1.5下用javaw.ext可以运行, 用java.exe运行就提示连不上mysql? 一个很简单的问题,我就是搞不定(散分80) 初学JAVA 请问哪有jbuilder 8 下载? applet 输出html 请问,JAVA的函数传递参数的时候,能传递引用吗??? 输入数学表达式得结果 java如何实现对局域网的监视程序
public static void main(String[] args) {
BufferedReader r = null;
int m = 0,n=0;
try {
r = new BufferedReader(new FileReader(new File("a.txt")));
String s = null;
StringBuffer ss= new StringBuffer();
while((s = r.readLine())!=null)
ss.append(s);
for(int i = 0; i<ss.length(); i++){
char c = ss.charAt(i);
if(c>='a'&& c<'z')
m++;
else if(c>='A'&& c<='Z')
n++;
}
System.out.println("小写:"+m+",大写:"+n);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
r.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(fileName));
char[] buffer = new char[1024];
int letters = 0;
int cLetters = 0;
while (reader.read(buffer, 0, 1024) != -1) {
for (char c : buffer) {
if ((c >= 65 && c <= 91)
|| (c >= 97 && c <= 123)) {
letters++;
if (c >= 97 && c <= 123) {
cLetters++;
}
}
}
}
System.out.println("所有字母:" + letters);
System.out.println("大写字母:" + cLetters);
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}