string - trim.() method does not work(java) -
so i'm trying count characters in string without spaces. somehow trim() method doesn't cut spaces:
class test { hashmap<character, integer> testmap = new hashmap<>(); public void encode(string text) { string code = text.trim(); (int = 0; < code.length(); i++) { character key = code.charat(i); if (testmap.containskey(key)) { testmap.put(key, testmap.get(key) + 1); } else { testmap.put(key, 1); } } system.out.println(testmap); system.out.println(code); } main method:
public class mymain { public static void main(string[] args) { test test = new test(); test.encode("tessstttt faill"); } }
so output is:
{ =1, a=1, s=3, t=1, t=4, e=1, f=1, i=1, l=2} tessstttt faill where mistake. lot ahead!! (i'm not experienced yet, sorry in case of obvious mistake)
trim won't remove whitespace, leading , trailing. remove whitespace, want this:
text.replaceall("\\s","")
Comments
Post a Comment