我如何计算此变量中有多少个字符,字符串,整数和浮点或双float或double?
String str = "I will give you a 20.5 rupees instead of 20";
我如何计算此变量中有多少个字符,字符串,整数和浮点或双float或double?
像这样的事情:
char chars = 2; // "I" and "a"
String strings = 6; // "will", "give", "you", "rupees","instead" and "of"
int ints = 1; // 20
double doubles = 1; // 20.5
我想成为这样的输出:
char 2字符串6 int 1 float 1
我练习在上面工作:
int s,f,d,c,i;
String [] splited = str.split("\\s+");
for(int i=0;i<splited.length;i++)
{
String typee=splited[i].getClass.getName();
if(typee==string){
s=s+1;}
else if(typee=integer){
i=i+1;
}
else if(typee=float){
f=f+1;
}
else if(typee=char){
c=c+1;
}
else if(typee=double){
d=d+1;
}
}
System.out.println("String "+s+"\n int "+i+"\n float "+f+"\n char "+c+"\n double "+d);
String str = "I will give you a 20.5 rupees instead of 20";
How can I count how many char, string, integer, and float or double are in this variable?
Something like this:
char chars = 2; // "I" and "a"
String strings = 6; // "will", "give", "you", "rupees","instead" and "of"
int ints = 1; // 20
double doubles = 1; // 20.5
I want to the output to be something like this :
char 2 string 6 int 1 float 1
I practice working on it :
int s,f,d,c,i;
String [] splited = str.split("\\s+");
for(int i=0;i<splited.length;i++)
{
String typee=splited[i].getClass.getName();
if(typee==string){
s=s+1;}
else if(typee=integer){
i=i+1;
}
else if(typee=float){
f=f+1;
}
else if(typee=char){
c=c+1;
}
else if(typee=double){
d=d+1;
}
}
System.out.println("String "+s+"\n int "+i+"\n float "+f+"\n char "+c+"\n double "+d);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如@dangling其他评论中所述,您需要将其转换为各自的数据类型,并且必须维护订单。因为如果您不维护数据类型类型层次结构您将始终获得错误的输出。
这是我的试用,可以有比这更好的方法。
必须使用
try-catch
来确保如果无法解析程序,则该程序不应突然结束,然后继续检查下一个数据类型。As @dangling else said in the comment you need to convert to respective datatypes and you have to maintain order. Because if you don't maintain the data type hierarchy you will always get the wrong output.
Here is my tryout, there can be a better approach than this.
Had to use
try-catch
to ensure that if it fails to parse the program should not end abruptly and proceed to check the next datatype.输出:
Output: