我如何计算此变量中有多少个字符,字符串,整数和浮点或双float或double?

发布于 2025-02-04 14:17:51 字数 881 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最偏执的依靠 2025-02-11 14:17:51
 public static void main(String[] args) {
        String str = "I will give you a 20.5 rupees instead of 20";
        String[] array = str.split("\\s", -1);
        int chars = 0;
        int strings = 0;
        int ints = 0;
        int doubles = 0;
        for (int i=0; i<array.length; i++){
            if(array[i].matches("[a-zA-Z]+")){
                if(array[i].length() == 1){
                    chars = chars + 1;
                }
                else{
                    strings = strings + 1;
                }
            }
            if(array[i].matches("[0-9]+")){
                ints = ints + 1;
            }
            if(array[i].matches("[+-]?\\d+\\.?(\\d+)?")){
                doubles = doubles + 1;
            }
        }
        doubles = doubles - ints;
        
        System.out.println("chars: "+ String.valueOf(chars));
        System.out.println("strings: "+  String.valueOf(strings));
        System.out.println("ints: "+ String.valueOf(ints));
        System.out.println("doubles: "+ String.valueOf(doubles));
    }
 public static void main(String[] args) {
        String str = "I will give you a 20.5 rupees instead of 20";
        String[] array = str.split("\\s", -1);
        int chars = 0;
        int strings = 0;
        int ints = 0;
        int doubles = 0;
        for (int i=0; i<array.length; i++){
            if(array[i].matches("[a-zA-Z]+")){
                if(array[i].length() == 1){
                    chars = chars + 1;
                }
                else{
                    strings = strings + 1;
                }
            }
            if(array[i].matches("[0-9]+")){
                ints = ints + 1;
            }
            if(array[i].matches("[+-]?\\d+\\.?(\\d+)?")){
                doubles = doubles + 1;
            }
        }
        doubles = doubles - ints;
        
        System.out.println("chars: "+ String.valueOf(chars));
        System.out.println("strings: "+  String.valueOf(strings));
        System.out.println("ints: "+ String.valueOf(ints));
        System.out.println("doubles: "+ String.valueOf(doubles));
    }
月朦胧 2025-02-11 14:17:51

正如@dangling其他评论中所述,您需要将其转换为各自的数据类型,并且必须维护订单。因为如果您不维护数据类型类型层次结构您将始终获得错误的输出。

这是我的试用,可以有比这更好的方法。

String str = "I will give you a 20.5 rupees instead of 20";
String array[] = str.split("\\s+");
int noofChars=0,noofString=0,noofDouble=0,noOfFloat=0,noofInt =0;
for(String arr : array){

    //Parsing to Integer if succesfull then proced to the next one
    try{
        Integer.parseInt(arr);
        noofInt++;
        continue;
    }catch(Exception e){
        e.getMessage();
    }
    //Parsing to Float if succesfull then proced to the next one
    try{
        Float.parseFloat(arr);
        noOfFloat++;
        continue;
    }catch(Exception e){
        e.getMessage();
    }
    //Parsing to Double if succesfull then proced to the next one
    try{
        Double.parseDouble(arr);
        noofDouble++;
        continue;
    }catch(Exception e){
        e.getMessage();
    }
    // If we are here that means its either character or string .
    // easiest way to check  if length == 1 then its a character
    if(arr.length()==1){
        noofChars++;
        continue;                
    }else{
        noofString++;
    }         
    
    
}

System.out.println("String "+noofString+"\n int "+noofInt+"\n char "+noofChars+"\n double "+noofDouble+"\n float "+noOfFloat);

必须使用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.

String str = "I will give you a 20.5 rupees instead of 20";
String array[] = str.split("\\s+");
int noofChars=0,noofString=0,noofDouble=0,noOfFloat=0,noofInt =0;
for(String arr : array){

    //Parsing to Integer if succesfull then proced to the next one
    try{
        Integer.parseInt(arr);
        noofInt++;
        continue;
    }catch(Exception e){
        e.getMessage();
    }
    //Parsing to Float if succesfull then proced to the next one
    try{
        Float.parseFloat(arr);
        noOfFloat++;
        continue;
    }catch(Exception e){
        e.getMessage();
    }
    //Parsing to Double if succesfull then proced to the next one
    try{
        Double.parseDouble(arr);
        noofDouble++;
        continue;
    }catch(Exception e){
        e.getMessage();
    }
    // If we are here that means its either character or string .
    // easiest way to check  if length == 1 then its a character
    if(arr.length()==1){
        noofChars++;
        continue;                
    }else{
        noofString++;
    }         
    
    
}

System.out.println("String "+noofString+"\n int "+noofInt+"\n char "+noofChars+"\n double "+noofDouble+"\n float "+noOfFloat);

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.

黯然 2025-02-11 14:17:51
public static void main(String[] args) {
    String inputStr = "I will give you a 20.5 rupees instead of 20";
    System.out.println("Decimals:");
    Matcher decimalMatches = Pattern.compile("(\\d+\\.\\d+)").matcher(inputStr);
    while (decimalMatches.find()) {
        System.out.print(Double.parseDouble(decimalMatches.group()) + " ");
    }

    System.out.println();
    System.out.println("Integers:");
    Matcher integerMatches = Pattern.compile("(\\d+)").matcher(inputStr);

    while (integerMatches.find()) {
        System.out.print(Integer.parseInt(integerMatches.group()) + " ");
    }

    System.out.println();
    System.out.println("Characters:");
    Matcher charMatches = Pattern.compile("(^\\w\\s|\\s\\w$|\\s\\w\\s)").matcher(inputStr);
    while (charMatches.find()) { 
        System.out.print(charMatches.group(1) + " ");
    }

    System.out.println();
    System.out.println("Words:");
    Matcher wordMatches = Pattern.compile("(\\w{2,})").matcher(inputStr);

    while (wordMatches.find()) {
        String d = wordMatches.group(1);
        try {
            Integer.valueOf(d);
        } catch (NumberFormatException e) {
            System.out.print(d + " ");
        }
    }
}

输出:

Decimals:
20.5 
Integers:
20 5 20 
Characters:
I   a  
Words:
will give you rupees instead of 
public static void main(String[] args) {
    String inputStr = "I will give you a 20.5 rupees instead of 20";
    System.out.println("Decimals:");
    Matcher decimalMatches = Pattern.compile("(\\d+\\.\\d+)").matcher(inputStr);
    while (decimalMatches.find()) {
        System.out.print(Double.parseDouble(decimalMatches.group()) + " ");
    }

    System.out.println();
    System.out.println("Integers:");
    Matcher integerMatches = Pattern.compile("(\\d+)").matcher(inputStr);

    while (integerMatches.find()) {
        System.out.print(Integer.parseInt(integerMatches.group()) + " ");
    }

    System.out.println();
    System.out.println("Characters:");
    Matcher charMatches = Pattern.compile("(^\\w\\s|\\s\\w$|\\s\\w\\s)").matcher(inputStr);
    while (charMatches.find()) { 
        System.out.print(charMatches.group(1) + " ");
    }

    System.out.println();
    System.out.println("Words:");
    Matcher wordMatches = Pattern.compile("(\\w{2,})").matcher(inputStr);

    while (wordMatches.find()) {
        String d = wordMatches.group(1);
        try {
            Integer.valueOf(d);
        } catch (NumberFormatException e) {
            System.out.print(d + " ");
        }
    }
}

Output:

Decimals:
20.5 
Integers:
20 5 20 
Characters:
I   a  
Words:
will give you rupees instead of 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文