Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Array Histogram

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array Histogram

    Hello, I'm having trouble coding a program which reads in strings (search terms) from a file, orders them, and then creates a histogram showing the occurrence of each string length.

    For example, if the search terms were: frog, cat, dog; the program would print the following output:

    Search Terms
    -------------
    frog
    cat
    dog

    Term Length ... Count
    ------------ ... ------
    3 .................... 2
    4 .................... 1

    I have the program reading in terms and sorting them, but I can't get the histogram to display the number of occurrences for each term.

    I get output like this:
    3 .......... 1
    3 .......... 1
    4 .......... 1

    I know it's a logic error, but what should I be doing?

    package nicepackage;
     
    import java.io.*;
    import java.util.Scanner;
     
    public class Search {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception {
    		System.out.println("\nSearch Terms");
    		System.out.print("------------");
     
    		FileReader fr1 = new FileReader("C:\\users\\T\\inSearchterms.txt");
    		Scanner inFr1 = new Scanner (fr1);
     
    		int n = inFr1.nextInt()+1;
    		String[] a = new String[n];
    		for (int i=0; i<n; i++)
    		{
    			a[i]= inFr1.nextLine();
    			//System.out.println(a[i]);
    		}
    		selectionSort(a);
    		System.out.println("\nTerm Length	Count");
    		System.out.println("-----------	-----");
     
    		int[] histogram = new int[n];
    		int[] ct = new int[n];
     
    		for (int b=1; b <= a.length-1; b++)
    		{
    			for (int d = 30; d >= 0; d--)
    			{
    				if (d == a[b].length())
    				{
    					histogram[b] = a[b].length();
    					ct[b]++;
    				}
    			}
    			System.out.println(histogram[b] + "		" + ct[b]);
    		}
    	}
     
    	public static void selectionSort (String [] a)
    	{
    		for (int i=a.length-1; i >= 1; i--)
    		{
    			String currentMax = a[0];
    			int currentMaxIndex = 0;
    			for (int j=1; j <= i; j++)
    			{
    				if (currentMax.compareToIgnoreCase(a[j])<0)
    				{
    					currentMax = a[j];
    					currentMaxIndex = j;
    				}
    			}
    			if (currentMaxIndex != i)
    			{
    				a[currentMaxIndex] = a[i];
    				a[i] = currentMax;
    			}
    			//System.out.println("/"+a[i]);
    		}
     
    	}
    }

    What am I doing wrong? Any help is greatly appreciated, thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Histogram

    Do you know how to use a Map? It would be useful: key=length of String, value=count

    Otherwise use an array. What is the max String length? Have an array with a slot for each length and increment the slots as per String length.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Histogram

    Unfortunately, I don't know how to use a Map.

    I need the program to determine the max String length, can I do that using the process you described? If I'm understanding correctly, I think that's what I'm trying to do. I set up two int arrays, one called histogram and one called ct, each with as many slots as there are terms in the file (or at least that's what I intended to do).

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Histogram

    Read all the strings and remember the length of the longest one, create the array then read them again and count the lengths.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Histogram

    I know that the longest String in the file I'm using is 26 characters, but I can't just create the array specific to the Strings in this file because I need it to work with any file.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Histogram

    One way would be to make two passes. The first to find the longest, the second to count.
    Another way would be to use parallel arrays, have one array contain the length, and the other the count.


    Using the new collection classes would make this easier.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Histogram

    Thanks for your help so far.

    I think I'm trying to use parallel arrays. Am I initializing them correctly in my code? I believe that's where I'm getting errors.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Histogram

    java.lang.ArrayIndexOutOfBoundsException: 16
    The index is past the end of the array. Remember array indexes go from 0 to the length-1
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    14
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default h

    Can somebody take another look at this, please? I'm losing my mind trying to figure this out.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Histogram

    Please post the program's output and explain what is wrong with it and show what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Printing A Histogram...
    By Nuggets in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 18th, 2012, 02:11 PM
  2. [SOLVED] BufferReader, histogram
    By Nhedro in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 22nd, 2012, 09:39 AM
  3. Histogram Java Program
    By djl1990 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 24th, 2011, 06:47 AM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM