简体   繁体   中英

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

I am attempting to create a simple program that counts the amount of each instance of letters AZ in a single string. Example:
input: "abc dca"
output:
there are/is 2 instance(s) of the letter a
there are/is 1 instance(s) of the letter b
there are/is 2 instance(s) of the letter c
there are/is 1 instance(s) of the letter 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");

}


} 

This works completely fine with a short string like the one shown in the example, but when I attempt it with a much longer String I don't have much luck
On Jgrasp compiler I get an error that looks something like this:

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

On VS Code, the String extends beyond the boundaries and thus doesn't really fit on the screen.
So that is my predicament basically that I wish to run this code with a much larger string

A hashmap would be simpler approach. By converting the String to a character array and tallying.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM