java - Occurrences of each letter -


this program have write error,

exception in thread "main" java.lang.arrayindexoutofboundsexception: 50 

write complete program using 2 arrays, upper , lower keep upper , lower alphabet respectively. ask user enter string example:

this test jupiter. see jupiter!!! may dr. d.

your program should parse string , keep track of number of alphabet. both arrays indexed 0 25. logical way use upper[0] count number of ‘a’, , upper[1] count number of ‘b’ , on. likewise lower array.

output should like:

a: 0 a:2  b: 0 b:0 . . . z:0 z:0 

code

import java.awt.*; import javax.swing.*; import java.io.*; import java.util.*;  public class letter {   public static void main(string[] args) {      // results     char[] chars = userenters();      system.out.println();     system.out.println("occurrences of each letter are:");     printarray(countlow(chars), countup(chars));   }    public static char[] userenters() {      string inputx = joptionpane.showinputdialog("enter line of text:  ");     char[] chars = inputx.tochararray();      return chars;   }    public static int[] countlow(char[] input) {     int[] counts = new int[26];      (int = 0; < input.length; i++) {       counts[input[i] - 'a']++;     }     return counts;   }    public static int[] countup(char[] input2) {     int[] countsup = new int[26];     (int = 0; < input2.length; i++) {       countsup[input2[i] - 'a']++;     }     return countsup;   }    public static void printarray(int[] counts, int[] countsup) {     (int = 0; < counts.length; i++) {        system.out.print(counts[i] + " " + (char) ('a' + i) + " ");       system.out.print(countsup[i] + " " + (char) ('a' + i) + "\n");     }   } } 

i hope don't mind did refactor code bit.

please have @ alterantive solution problem , read comments @ bottom of answer.

   import java.util.hashmap; import java.util.map; import java.util.map.entry;  import javax.swing.joptionpane;   public class lettercounter {      //hash maps don't allow duplication.      //the letter key , repetitions value(your goal!)     private map<character, integer> resultsmap = new hashmap<character, integer>();       public static void main(string[] args) {          lettercounter lettercounter = new lettercounter();         lettercounter.fillmap();         lettercounter.showmapcontents();             }      private void showmapcontents() {         (entry<character, integer> entry : resultsmap.entryset())         {             system.out.println("'" + entry.getkey() + "' - " + entry.getvalue() + " times");         }            }      private void fillmap() {         char[] userinputasarray = getuserinputasletterarray();         (int currentletter = 0; currentletter < userinputasarray.length; currentletter++) {             int count = getoccurences(userinputasarray[currentletter],userinputasarray);             resultsmap.put(userinputasarray[currentletter], count);         }     }      private int getoccurences(int letter, char[] userinputasarray) {         int counter = 0;         (int currentindex = 0; currentindex < userinputasarray.length; currentindex++) {             if(userinputasarray[currentindex] == letter)                 counter++;         }         return counter;     }      public char[] getuserinputasletterarray() {         string userinput = joptionpane.showinputdialog("enter line of text:  ");         char[] chars = userinput.tochararray();         return chars;     } } 
  • whenever want exercise need manipulate data, should pick best data structure job. in case, think hash map interesting because avoids duplicates , big part of job you. find cheat sheet in link: http://www.janeve.me/articles/which-java-collection-to-use
  • i noticed used lot static , not object oriented thing do. alternative, when want on run quick examples one, can initialize class inside itself.

i hope useful.


Comments