簡體   English   中英

Java:如何繞過 Visual Studio 字符串長度限制的字符串長度限制?

[英]Java: How to Bypass String length limit on Visual Studio string length limit?

我正在嘗試創建一個簡單的程序來計算單個字符串中每個字母 AZ 實例的數量。 例子:
輸入:“abc dca”
output:
有/是2個字母a的實例
有/是 1 個字母 b 的實例
有/是2個字母c的實例
有/是 1 個字母 d 的實例

class ilikyo
{
public static boolean checkifvalid(String wrds)
{     int stopper  = 0;
    boolean checked = true;
    for(int i = 0; i < wrds.length(); i++)
          {
          if((int)wrds.charAt(i) != 32 && (int)wrds.charAt(i) < 65)
          {
          System.out.println("error!   " + wrds.charAt(i) + "   is not a valid input");
            stopper++;
            checked = false;
          }
        
          }
            if(stopper == 0)
                   {
                   System.out.println("Input is valid!");
                   }
return checked;
}
public static String converttoLower(String wrdy)
{     
String copy = "";
for(int i= 0; i < wrdy.length(); i++)
{
    if((int)wrdy.charAt(i) >= 97 || (int)wrdy.charAt(i) == 32)
             {
             copy = copy + wrdy.charAt(i);
             }
        else
             {
              int Upper = (int)wrdy.charAt(i) +32;
                copy = copy + (char)Upper;
             }
}
return copy;
}
public static void sortthealph(String wrd)
          {
          int check  = 0;
          int stopper = 0;
          int spaces = 0;
          wrd = converttoLower(wrd);
          String copy = "";
          for(int i = 97; i <= 122; i++)
                   {
                   int counthowmany = 0;
                  for(int j = 0; j < wrd.length(); j++)
                        {
                        
                               if((int)wrd.charAt(j) == i)
                               {
                                     counthowmany++;
                                     check = counthowmany;
                               }
                               if((int)wrd.charAt(j) == 32 && stopper == 0)
                               {
                               spaces++;
                            
                               }
                               
                               
                        }
                      if(counthowmany > 0)
                               {
                               System.out.println("there are/is  " + counthowmany + " instance(s) of the letter " + (char)i);
                               }
                        stopper = 1;
                   }
                   System.out.println(copy);
                   System.out.println(" + " + spaces + " spaces");
         
          }
        
public static void main(String[] args)
{

long starttime = System.nanoTime();
String testing = "abc dca";
sortthealph(testing);
long endtime =System.nanoTime();
long totaltime = endtime - starttime;
System.out.println((double)totaltime/1000000000 + "  seconds elapsed");

}


} 

對於示例中所示的短字符串,這完全可以正常工作,但是當我嘗試使用更長的字符串時,我運氣不佳
在 Jgrasp 編譯器上,我得到一個看起來像這樣的錯誤:

jgraspcoding.java:80: error: constant string too long

在 VS Code 上,字符串超出了邊界,因此並不真正適合屏幕。
所以這基本上是我的困境,我希望用更大的字符串運行這段代碼

hashmap 將是更簡單的方法。 通過將字符串轉換為字符數組並進行計數。

char[] chars = str.toCharArray();
Map <char, Integer> hash = new HashMap<char, Integer>();


for(char ch: chars) {
    Integer cnt = hash.get(ch);
    hash.put(ch, (cnt == null) ? 1 : cnt +1);
}

for (Map.Entry<char, Integer> entry : hash.entrySet()) {
    System.out.println("there are/is " + entry.getValue() + "  instance(s) of the letter " entry.getKey());
} 

WARN: uncompiled code

暫無
暫無

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

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