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 3 of 3

Thread: Display histogram using values from Array, use asterisks in histogram not numerals

  1. #1
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Display histogram using values from Array, use asterisks in histogram not numerals

    Currently I'm trying to create a histogram that displays asterisks instead of the numeral. My project is two classes, one for data, and one for display. I feel like I have 90% of this project done except for I can't for the life of me figure out how to print *** instead of 3. Any tips and feedback on my code is greatly appreciated. I really try hard to learn this material and unfortunately for me, it's slow going. I have had success in these forums reading other people's explanations, sometimes people explain things in a manner that is different from what I previously thought and it's a great help. I'm hoping for that again. Here comes some shaky code...My assignment was to create a program that has two classes, uses an ArrayList, uses a loop to process raw data to fill in an Array with the counts, and then use those count values to display a histogram. So, my project is a tool rental center. The ToolRentalDisplay will utilize the Array, and hopefully, the histogram. The ToolRentalData Class has the ArrayList of String values which I chose to make the names of different tools. There is no user input so I made the list of 10 tools (the size of my array) and made the Plate Compactor the most rented tool. Hopefully someone, anyone, can understand what I'm trying to do and help me out. If I didn't, or you have questions, please ask. Thanks everyone.
    import java.util.ArrayList;
    import java.util.Arrays;
     
    public class ToolRentalDisplay
    {
       private ArrayList<String> rentalData;
       private int[] rentalCounts;
       /**
        * Constructor for objects of class ToolRentalDisplay
        */
       public ToolRentalDisplay()
       {
            ToolRentalData data = new ToolRentalData();
            rentalData = data.getData();
            analyzeData();
       }
     
        /**
         * Review the tools that were rented in a given shift    //the reason this documentation looks like this is because through all my testing I've been 
         * Record the counts of each tool individually              //doing it was easier to be able to see it right there & I can copy/paste.      
         * rentalCounts[0]: Plate Compactor
         * rentalCounts[1]: Carpet Cleaner
         * rentalCounts[2]: Lawn Mower
         * rentalCounts[3]: Roto-Hammer
         * rentalCounts[4]: Carpet Cleaner
         * rentalCounts[5]: Pressure Washer
         * rentalCounts[6]: Air Coil Siding Nailer
         * rentalCounts[7]: Plate Compactor
         * rentalCounts[8]: Plate Compactor
         * rentalCounts[9]: Hammer Drill   
         */
       private void analyzeData()
       { 
           int[] rentalCounts = new int[10];
     
     
           for (String rental : rentalData) {
                if (rental.equals("Plate Compactor")) {
                    rentalCounts[0]++;
                }
                else if (rental.equals("Carpet Cleaner")) {
                    rentalCounts[1]++;
                }
                else if (rental.equals("Lawn Mower")) {
                    rentalCounts[2]++;
                }
                else if (rental.equals("Roto-Hammer")) {
                    rentalCounts[3]++;
                }
                else if (rental.equals("Air Coil Siding Nailer")) {
                    rentalCounts[4]++;
                }
                else if (rental.equals("Pressure Washer")) {
                    rentalCounts[5]++;
                }
                else if (rental.equals("Air Coil Siding Nailer")) {
                    rentalCounts[6]++;
                }
                else if (rental.equals("Plate Compactor")) {
                    rentalCounts[7]++;
                }
                else if (rental.equals("Plate Compactor")) {
                    rentalCounts[8]++;
                }
                else if (rental.equals("Hammer Drill")) {
                    rentalCounts[9]++;
                }
           }
       }
     
       /**
        * Display the analyzed data as a histogram using asterisks, not numerals                                                    
        */                                                                                                               
       public void displayData()                                                                                                                                                                                           
       {    
           System.out.println("Number of times each tool was rented during that shift:");
           System.out.println("Plate Compactor:   " +      rentalCounts[0]);
           System.out.println("Carpet Cleaner:  "    +      rentalCounts[1]);
           System.out.println("Pressure Washer:    " +    rentalCounts[2]);
           System.out.println("Lawn Mower: "         +     rentalCounts[3]);
           System.out.println("Air Coil Siding Nailer:  " + rentalCounts[4]);
           System.out.println("Hammer Drill:    "    +      rentalCounts[5]);
       } 
    }

  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: Display histogram using values from Array, use asterisks in histogram not numerals

    out how to print *** instead of 3
    Build a String of *s and then print it:
    See this link from Google: https://stackoverflow.com/questions/...racter-n-times

    Or have a loop that iterates 3 times and print an * each time

    Note: The list of if/else if statements should have a final else statement that prints an error message for rental if none of the previous statements were true.

    --- Update ---

    Also posted here: https://coderanch.com/t/744919/java/...m-values-Array
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Display histogram using values from Array, use asterisks in histogram not numerals

    Hey, thanks for the stackoverflow link, I've looked at that site quite a bit, and honestly, I don't really understand what I'm reading. I tried using

    for (int j = 0; j < rentalCounts[i] - 1; j++) {
    System.out.println(" * ");
    }

    and it didn't work. I read somewhere that if I move whatever I wanted printed with the asterisk above the loop it should work. In the example they show, it's not exactly like mine, but I guess it worked. Mine didn't, so I was wondering if I was naming something wrong, or if I needed to declare a variable or something. What I currently have displays the amount, just not the asterisk, so I feel like I'm close.

    --- Update ---

    Ok, I just went back and tried to make a couple corrections on my project. I added a final else statement to my if/else chain and then tried adding a for loop to my displayData method. When I tried to run the program I got an error that says java.lang.NullPointerException: Null. I took the for loop out and now I still get the same error message. I don't get it. In my BlueJ editor it's highlighting the System.out.println("Plate Compactor: " + rentalCounts[0]).
    Last edited by misterCrypto; August 18th, 2021 at 12:34 PM.

Similar Threads

  1. Histogram equalization Help Please!
    By dazzle1218 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2013, 04:08 PM
  2. Array Histogram
    By Thor in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 4th, 2012, 08:06 AM
  3. [SOLVED] Printing A Histogram...
    By Nuggets in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 18th, 2012, 02:11 PM
  4. [SOLVED] BufferReader, histogram
    By Nhedro in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 22nd, 2012, 09:39 AM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM