簡體   English   中英

計算字符串Java中的字符數

[英]Count Chars in a String Java

嗨,我的代碼有問題,我必須計算表達式中使用的變量,當兩個或多個變量相同時,應將其計為1。例如,a + ab =使用的變量總數:2。問題是當我輸入a + a =使用的變量總數時:2.這是我的代碼:

public void simplify(String strexp){
    int ex =strexp.length();

    for (int a=0;a<=ex-1;a++){
        if(a==0)
        {
        if (Character.isLetter(strexp.charAt(a)))
        {
        b++;
       }


        }
        else{
        for(int c=0;c<=a-1;c++){
              if (Character.isLetter(strexp.charAt(c))==Character.isLetter(strexp.charAt(a)))
              {

                System.out.println("equal");
                break;
              }
              else if (Character.isLetter(strexp.charAt(c))!=Character.isLetter(strexp.charAt(a)))
              {
             //if(c==a-1)
              //{
               b++;
               System.out.println("nomatch");
           //  }
               }
        }
        }
        }
   JOptionPane.showMessageDialog(null, b);
    }

將字符串轉換為char數組,將其添加到鍵為char及其值的變量計數的映射中。

import java.util.*;

    class Main

{
    public static void main (String[] args) 
    {
        String yourString = "UNDERSTAND";

        Map<Character, Integer> count = new TreeMap<Character, Integer>();
        for (char c : yourString.toCharArray()) {
            if (count.containsKey(c)) {
                count.put(c, (int) count.get(c) + 1);
            } else {
                count.put(c, 1);
            }
        }

        System.out.println(count);

    }
}

您需要跟蹤看到的變量。 一種方法是使用Set<char> 每當您看到一個字符時,將其添加到集合中。 唯一運算符的數量將是集合中字符的數量。

使用列表計算變量總數

public static int simplify(String strexp) {
    int length = strexp.length();

    List<Character> usedVariables = new ArrayList<Character>();
    for (int i = 0; i < length; i++) {
        char c = strexp.charAt(i);
        if (Character.isLetter(c) && !usedVariables.contains(c)) {
            usedVariables.add(c);
        }
    }

    return usedVariables.size();
}

public static void main(String[] args) {
    System.out.println(simplify("x + b = d"));
    System.out.println(simplify("x + x = a"));
    System.out.println(simplify("x + x = x / 2"));
}

打印出來

3
2
1

在如此低的水平上很難遵循。 為什么不使用正則表達式來查找所有匹配項,然后構建一組唯一的匹配項呢?

標題具有誤導性。 該任務更像是詞匯標記計數。 比簡單的角色高出一步。

一線如何?

String complexExpression = "a + a*2 (2*a + b - 34)/2 + 3*c";

System.out.println(Arrays.toString(complexExpression.split("[+*()-/\\d\\s&&[^a-z]]+")));

System.out.println(new HashSet<String>(Arrays.asList(complexExpression.split("[+*()-/\\d\\s&&[^a-z]]+"))).size());

上面的代碼為我打印了以下內容:

[a, a, a, b, c]
3

當然,我不建議以相同的模式多次執行split操作。

這里有一些東西:

public class DistinctCharCount {
    public static void main(String[] args) {
        String s = "a+ab+abc+abcd";

        HashSet<Character> charSet = new HashSet<Character>();        
        for(int i=0;i<s.length();i++) {
            charSet.add(s.charAt(i));
        }

        System.out.println("Distinct Char Count : " + charSet.size());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM