The Character Class in Java -


here short program counts letters of given word entered user. i'm trying figure out following lines in program:

counts[s.charat(i) - 'a']++; // don't understand - 'a' doing

system.out.println((char)('a' + i) // don't 'a' + does.

    import java.util.scanner;      public class listing9_3 {          public static void main(string[] args) {             //create scanner             scanner input = new scanner (system.in);             system.out.println("enter word find out occurences of each letter: ");             string s = input.nextline();              //invoke count letters method count each letter             int[] counts = countletters(s.tolowercase());              //display results             for(int = 0; i< counts.length; i++){                 if(counts[i] != 0)                     system.out.println((char)('a' + i) + " appears " +                  counts[i] + ((counts[i] == 1)? " time" : " times"));     ***//i don't understand 'a' + doing             }         }         public static int[] countletters(string s) {             int[] counts = new int [26]; // 26 letters in alphabet              for(int = 0; < s.length(); i++){                 if(character.isletter(s.charat(i)))                     counts[s.charat(i) - 'a']++;      ***// don't understand - 'a' doin             }             return counts;         }      } 

characters kind of integer in java; integer number associated character on unicode chart. thus, 'a' integer 97; 'b' 98, , on in sequence through 'z'. s.charat(i) returns character; assuming lower-case letter in english alphabet, subtracting 'a' gives result 0 'a', 1 'b', 2 'c', , on.

you can see first 4096 characters of unicode chart @ http://en.wikibooks.org/wiki/unicode/character_reference/0000-0fff (and there references other pages of chart well). you'll see 'a' there u+0061 (which hex, = 97 decimal).


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -