Results 1 to 5 of 5
  1. #1
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default How to create a Histogram

    Hi, I'm new here and am having some issues using java to create a program. I'm trying to create a program that will have a JTextField that will accept user input. Then when the user presses enter, it needs to create a histogram of all 26 letters, and show how many times each letter appears in the phrase. Any help is appreciated.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    That could be done on a method that accepts the whole phrase and a letter to be compared with,

    Have a loop on that phrase, compare each character,
    As each comparison satisfies, increment the value that will then be returned as the number of times that letter exists
    freedom exists in the world of ideas

  3. #3
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default

    Yeah, I'm kinda retarded and am not really understanding how to do it.

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    PHP Code:
    public class test{
    
        public static void main(String args[]){
             new test();
        }
    
        public test(){
             String temp = "the quick browned fox jumps over the lazy dog";
             String temp1 = "abcdefghijklmnopqrstuvwxyz";
             for(char c:temp1.toCharArray())
                  System.out.println("Character "+c+" appeared "+getHowMany(temp,c)+" times");
        }
    
        private int getHowMany(final String phrase,final char c){
    	int times=0;
            for(char cc : phrase.toCharArray()){
                 if(cc==c)
                     times++;
            }return times;
        }
    }
    Try to test the sample code, fix those bugs if any....
    Last edited by sukatoa; 05-30-2008 at 10:46 AM.
    freedom exists in the world of ideas

  5. #5
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default

    Thank you for your help, it is working very well. My only issue now is I'm trying to get it to make a histogram out of the input values. I can get it to make one bar, but no letters before the last letter in the code works. Like if I do this, it only will count and show the letter "c" bar. Any help is appreciated.

    public void test(){


    String temp1 = "abcdefghijklmnopqrstuvwxyz";
    for(char c:temp1.toCharArray()) {

    switch(c)
    {
    case 'A':
    case 'a':

    height = 10*(getHowMany(input,c));


    case 'B':
    case 'b':

    height = 10*(getHowMany(input,c));

    case 'C':
    case 'c':

    height = 10*(getHowMany(input,c));

    }
    repaint();
    }
    }

Similar Threads

  1. Vector create
    By Warren in forum New To Java
    Replies: 4
    Last Post: 03-02-2010, 02:42 AM
  2. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 04:22 PM
  3. Create a new variable
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:48 AM
  4. how to create different arrays
    By osval in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 11:07 PM
  5. Create XML From XSD
    By Jack in forum XML
    Replies: 1
    Last Post: 07-09-2007, 12:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •