发个计算字符中空格,字母,数字个数的java类

bobo364 2009-12-01 11:25:52
今天天不错,昨天有人发的面试题中又有这样的问题,所以发个计算字符中空格,字母,数字个数的java类,水平次,如有不足,望指出,顺便撒20分

public class StringCaculate
{
private int i,j,k,l;
public StringCaculate
{
i=0;j=0;k=0;l=0;
}
public void caculate(String o)
{
StringBuffer aBuffer=new StringBuffer(o.lengh());
char aCharacter;
for(int m=0;m<o.lengh();m++)
{
aCharacter=o.charAt(i);
if(Character.isLetter(aCharacter))
i++;
else if(Character.isDigit(aCharacter))
j++;
else
l++;
}
System.out.println("有字母"+i+"个");
System.out.println("有数字"+j+"个");
System.out.println("有空格"+l+"个");
}
}



public class test
{
public static void main(String[] args)
{
String a="this is a string";
StringCaculate.caculate(a);
}
}



...全文
214 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
poikill 2009-12-01
  • 打赏
  • 举报
回复
学习学习
bobo364 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 shine333 的回复:]
大哥,不知道你有没有测试过
Java codeStringCaculate sc=new StringCaculate();
sc.caculate("abc");
sc.caculate("abc");
sc.caculate("abc");
i,j,k没必要做成成员变量
[/Quote]

公司里没java,无法测试
bobo364 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kf156 的回复:]
LZ貌似没考虑到字符串除了可以有空格、字母、数字外,还能有符号或其他文字
[/Quote]

仅限于判断这3者
shine333 2009-12-01
  • 打赏
  • 举报
回复
大哥,不知道你有没有测试过
StringCaculate sc = new StringCaculate();
sc.caculate("abc");
sc.caculate("abc");
sc.caculate("abc");

i,j,k没必要做成成员变量
kf156 2009-12-01
  • 打赏
  • 举报
回复
LZ貌似没考虑到字符串除了可以有空格、字母、数字外,还能有符号或其他文字
shine333 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 bobo364 的回复:]
Java code/*
* To change this template, choose Tools | Templates
* and open the template in the editor.*/package exp1;/**
*
*@author 364*/publicclass StringCaculate
{privateint i,j,k,l;public StringCaculate()
{
i=0;j=0;k=0;l=0;
}publicvoid caculate(String o)
{
StringBuffer aBuffer=new StringBuffer(o.length());char aCharacter;for(int m=0;m<o.length();m++)
{
aCharacter=o.charAt(m);if(Character.isLetter(aCharacter))
i++;elseif(Character.isDigit(aCharacter))
j++;else
l++;
}
System.out.println("有字母"+i+"个");
System.out.println("有数字"+j+"个");
System.out.println("有空格"+l+"个");
}
}/*
* To change this template, choose Tools | Templates
* and open the template in the editor.*/package exp1;/**
*
*@author bobo364*/class test
{publicstaticvoid main(String[] args)
{
String a="this is a string";
StringCaculate c=new StringCaculate();
c.caculate(a);
}
}

回家用netbean修改测试通过的版本,顺便关帖
[/Quote]

LZ,还是每次有测试连续调用!
c.caculate(a);
c.caculate(a);
c.caculate(a);
bobo364 2009-12-01
  • 打赏
  • 举报
回复


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package exp1;

/**
*
* @author 364
*/
public class StringCaculate
{
private int i,j,k,l;
public StringCaculate()
{
i=0;j=0;k=0;l=0;
}
public void caculate(String o)
{
StringBuffer aBuffer=new StringBuffer(o.length());
char aCharacter;
for(int m=0;m<o.length();m++)
{
aCharacter=o.charAt(m);
if(Character.isLetter(aCharacter))
i++;
else if(Character.isDigit(aCharacter))
j++;
else
l++;
}
System.out.println("有字母"+i+"个");
System.out.println("有数字"+j+"个");
System.out.println("有空格"+l+"个");
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package exp1;

/**
*
* @author bobo364
*/
class test
{
public static void main(String[] args)
{
String a="this is a string";
StringCaculate c=new StringCaculate();
c.caculate(a);
}
}


回家用netbean修改测试通过的版本,顺便关帖
whut0802 2009-12-01
  • 打赏
  • 举报
回复
jf闪人
  • 打赏
  • 举报
回复
一般这种程序都会加一个条件就是大数据量,从文件中读取等等,按才是关键所在。
lovepay1413 2009-12-01
  • 打赏
  • 举报
回复
全角。。。。半角。。。
shine333 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 applerockhool 的回复:]
对不?
Java codepublicstaticvoid testCount(String str) {char[] array= str.toCharArray();int letter=0, number=0, space=0;for (int i=0; i< array.length; i++) {if (array[i]=='')
space++;elseif (array[i]>='0'&& array[i]<='9')
number++;elseif (array[i]>='a'&& array[i]<='z')
letter++;
}
System.out.println("space="+ space+" number="+ number+" letter="+ letter);

}
[/Quote]
如果你只针对小写Ascii码
applerockhool 2009-12-01
  • 打赏
  • 举报
回复
对不?
public static void testCount(String str) {
char[] array = str.toCharArray();
int letter = 0, number = 0, space = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == ' ')
space++;
else if (array[i] >= '0' && array[i] <= '9')
number++;
else if (array[i] >= 'a' && array[i] <= 'z')
letter++;
}
System.out.println("space=" + space + " number=" + number + " letter="
+ letter);

}
Kevin2305 2009-12-01
  • 打赏
  • 举报
回复
lz肯定没有运行过,length都拼错了
课程目标:学习Java语言字符串相关的知识、字符编码常识和正则表达式的使用,并完成案例前导课程:《Java工程师必学系列课程》前4部课程内容:本课程是《Java工程师必学系列课程》的第5部分,主要讲解Java语言字符串相关知识、字符编码常识和正则表达式的使用。本课程涉及的主要内容可以分为四部分:一、String、StringBuffer和StringBuilder基本常识、基本原理和使用技巧二、字符编码常识三、Java语言正则表达式的详细语法和使用技巧四、实战案例课程说明:在开发Java程序的过程,最常用的莫过于字符串相关的。可以毫不夸张的说,任何一个Java程序,都离不开对字符串保存和处理。很多学员对字符串的理解只是处于比较粗浅的阶段。殊不知,如果对字符串处理的不好,会影响到软件的运行效率。本课程专门讲解字符串相关的知识,将从字符串的存储方式、底层的运行方式等各方面深入讲解其的原理和技巧。此外,对字符串进行更高级的处理,又要用到正则表达式的相关知识。正则表达式广泛应用于各种与字符串处理相关的场合。它是一套独立的语言系统,经过几十年的完善和发展,现在已经非常的强大,并且形成了国际标准。各种高级编程语言,都实现了自己的表达式引擎。本课程详细的讲解了Java语言正则表达式的语法和使用技巧。掌握了正则表达式,对编程水平的提高有非常大的帮助!同时,本课程在最后一部分,安排了非常精彩的、完整的实战案例,通过实战的形式切实帮助学员提高解决具体问题的能力!预期效果:认真学习完本课程,学员可以掌握字符串处理及正则表达式相关的系统知识,并能提高实际的编码水平。环境配置要求:学习本课程需安装JDK1.8或更高版本的JDK,以便程序能正确运行,建议使用IntelliJ IDEA 2019.1.2或更高版本的开发工具。    因有合作协议约束,《穆哥学堂》只提供PDF版本的课件!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧