Guest

Letter Counting

By: conor on Oct 10th, 2011  |  syntax: Java  |  size: 3.47 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. import javax.swing.JOptionPane;
  2. public class LetterCounting
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 //the inputted text
  7.                 String input = JOptionPane.showInputDialog("Enter your sentence(s).");
  8.                 //convert string to all lowercase
  9.                 input = input.toLowerCase();
  10.                 //integers of the number of each of the 26 letters of the alphabet
  11.                 int a = 0;
  12.                 int b = 0;
  13.                 int c = 0;
  14.                 int d = 0;
  15.                 int e = 0;
  16.                 int f = 0;
  17.                 int g = 0;
  18.                 int h = 0;
  19.                 int i = 0;
  20.                 int j = 0;
  21.                 int k = 0;
  22.                 int l = 0;
  23.                 int m = 0;
  24.                 int n = 0;
  25.                 int o = 0;
  26.                 int p = 0;
  27.                 int q = 0;
  28.                 int r = 0;
  29.                 int s = 0;
  30.                 int t = 0;
  31.                 int u = 0;
  32.                 int v = 0;
  33.                 int w = 0;
  34.                 int x = 0;
  35.                 int y = 0;
  36.                 int z = 0;
  37.                 //integer created for the length of characters in the sentence
  38.                 int length = input.length();
  39.                 //for statement that keeps running for the entire length of the sentence
  40.                 for(int ix =0; ix<length; ix++)
  41.                 {
  42.                         //if statements asking for each letter, and if that letter is present increases the value of the letter integer
  43.                         if(input.charAt(ix)=="a".charAt(0))
  44.                         {
  45.                                 a++;
  46.                         }
  47.                         if(input.charAt(ix)=="b".charAt(0))
  48.                         {
  49.                                 b++;
  50.                         }
  51.                         if(input.charAt(ix)=="c".charAt(0))
  52.                         {
  53.                                 c++;
  54.                         }
  55.                         if(input.charAt(ix)=="d".charAt(0))
  56.                         {
  57.                                 d++;
  58.                         }
  59.                         if(input.charAt(ix)=="e".charAt(0))
  60.                         {
  61.                                 e++;
  62.                         }
  63.                         if(input.charAt(ix)=="f".charAt(0))
  64.                         {
  65.                                 f++;
  66.                         }
  67.                         if(input.charAt(ix)=="g".charAt(0))
  68.                         {
  69.                                 g++;
  70.                         }
  71.                         if(input.charAt(ix)=="h".charAt(0))
  72.                         {
  73.                                 h++;
  74.                         }
  75.                         if(input.charAt(ix)=="i".charAt(0))
  76.                         {
  77.                                 i++;
  78.                         }
  79.                         if(input.charAt(ix)=="j".charAt(0))
  80.                         {
  81.                                 j++;
  82.                         }
  83.                         if(input.charAt(ix)=="k".charAt(0))
  84.                         {
  85.                                 k++;
  86.                         }
  87.                         if(input.charAt(ix)=="l".charAt(0))
  88.                         {
  89.                                 l++;
  90.                         }
  91.                         if(input.charAt(ix)=="m".charAt(0))
  92.                         {
  93.                                 m++;
  94.                         }
  95.                         if(input.charAt(ix)=="n".charAt(0))
  96.                         {
  97.                                 n++;
  98.                         }
  99.                         if(input.charAt(ix)=="o".charAt(0))
  100.                         {
  101.                                 o++;
  102.                         }
  103.                         if(input.charAt(ix)=="p".charAt(0))
  104.                         {
  105.                                 p++;
  106.                         }
  107.                         if(input.charAt(ix)=="q".charAt(0))
  108.                         {
  109.                                 q++;
  110.                         }
  111.                         if(input.charAt(ix)=="r".charAt(0))
  112.                         {
  113.                                 r++;
  114.                         }
  115.                         if(input.charAt(ix)=="s".charAt(0))
  116.                         {
  117.                                 s++;
  118.                         }
  119.                         if(input.charAt(ix)=="t".charAt(0))
  120.                         {
  121.                                 t++;
  122.                         }
  123.                         if(input.charAt(ix)=="u".charAt(0))
  124.                         {
  125.                                 u++;
  126.                         }
  127.                         if(input.charAt(ix)=="v".charAt(0))
  128.                         {
  129.                                 v++;
  130.                         }
  131.                         if(input.charAt(ix)=="w".charAt(0))
  132.                         {
  133.                                 w++;
  134.                         }
  135.                         if(input.charAt(ix)=="x".charAt(0))
  136.                         {
  137.                                 x++;
  138.                         }
  139.                         if(input.charAt(ix)=="y".charAt(0))
  140.                         {
  141.                                 y++;
  142.                         }
  143.                         if(input.charAt(ix)=="z".charAt(0))
  144.                         {
  145.                                 z++;
  146.                         }
  147.                 }
  148.                 //Prints out all the letters
  149.                 System.out.println("a:" + a);
  150.                 System.out.println("b:" + b);
  151.                 System.out.println("c:" + c);
  152.                 System.out.println("d:" + d);
  153.                 System.out.println("e:" + e);
  154.                 System.out.println("f:" + f);
  155.                 System.out.println("g:" + g);
  156.                 System.out.println("h:" + h);
  157.                 System.out.println("i:" + i);
  158.                 System.out.println("j:" + j);
  159.                 System.out.println("k:" + k);
  160.                 System.out.println("l:" + l);
  161.                 System.out.println("m:" + m);
  162.                 System.out.println("n:" + n);
  163.                 System.out.println("o:" + o);
  164.                 System.out.println("p:" + p);
  165.                 System.out.println("q:" + q);
  166.                 System.out.println("r:" + r);
  167.                 System.out.println("s:" + s);
  168.                 System.out.println("t:" + t);
  169.                 System.out.println("u:" + u);
  170.                 System.out.println("v:" + v);
  171.                 System.out.println("w:" + w);
  172.                 System.out.println("x:" + x);
  173.                 System.out.println("y:" + y);
  174.                 System.out.println("z:" + z);
  175.         }
  176. }
  177.  
  178.