I’m just wondering how do I convert for( char c: str.toCharArray()) to a simplified loop. I tried
for(int i = 0; i <= str.length(); i++)
{
char[] c = str.toCharArray();
However it doesn’t seem to be working. Its a program to count the occurence of letters in a String. Btw this is not homework. Im just trying to practice to get back into the swing of things. Any guidance would be much appreciated. Full code.
String str = "this is my new string";
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for(int i = 0; i <= str.length(); i++)
{
char[] c = str.toCharArray();
if(!map.containsKey(c))
map.put(c,1);
else
{
int value = map.get(c);
map.put(c, ++value);
}
}
System.out.println(map);
Try the String#charAt() method: