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

Thread: Re: how to display a bar chart using loops

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to display a bar chart using loops

    Quote Originally Posted by tirashad View Post
    hey guys, so im new to Java and yesterday was given an exercise. it said write a program that asks the user to enter sales of five stores and then display a bar chart (which contains rows of asterisks) comparing those sales. i used Scanner class to read input from the keyboard.

    technically speaking, i know how to use a loop to ask for 5 sales, and how to display a pure asterisk bar chart. the problem is that i dont know how to read all the inputs and move on with the chart only after that.

    if i write a loop to read the inputs first then the final number my variable is assigned to is the sales of the fifth store. all sales of four other stores will be replaced and thus lost. if i print out a row of asterisks right after each time i ask for a store's sales then it wont be called a chart. i thought of using five different variables for five stores but it sounds inefficient and probably not the best solution. moreover, this exercise is in the "Loops and Files" chapter so im pretty sure it has something to do with all those loops.

    can anyone please tell me what i should do? i appreciate any help.

    p.s: its exercise 12 on page 269, "Starting out with Java: from Control Structures through Objects" 5th edition by Tony Gaddis.
    I am working on this exercise now and am having some trouble with it. Did you get it figured out? I got my input from the user fine and display the graph afterwards, however the graph shows the total for all five stores on every line. I tried changing my counter variable name in each for loop but that didn't help. Here is my code:

    import java.util.Scanner; // Have to import in order to use the Scanner class.
    // This is my program.
    public class barChart
    {
    public static void main (String[] args)
    {

    int storeNum; //to hold a place for Store counter.
    int sales1;
    int sales2;
    int sales3;
    int sales4;
    int sales5;

    // This creates a scanner object to read user input.
    Scanner keyboard = new Scanner(System.in);

    System.out.print("What is today's sales for store1? "); //Get sale totals from user
    sales1 = keyboard.nextInt(); //Place the input from the user in the store1 variable.

    System.out.print("What is today's sales for store2? "); //Get sale totals from user
    sales2 = keyboard.nextInt(); //Place the input from the user in the store2 variable.

    System.out.print("What is today's sales for store3? "); //Get sale totals from user
    sales3 = keyboard.nextInt(); //Place the input from the user in the store3 variable.

    System.out.print("What is today's sales for store4? "); //Get sale totals from user
    sales4 = keyboard.nextInt(); //Place the input from the user in the store4 variable.

    System.out.print("What is today's sales for store5? "); //Get sale totals from user
    sales5 = keyboard.nextInt(); //Place the input from the user in the store5 variable.

    System.out.println();

    // Create a header for the table.
    System.out.println("Sales Bar Chart");
    System.out.println("---------------------");


    for (storeNum = 1; storeNum<=5; storeNum++)
    {
    //System.out.print("What is today's sales for store " + storeNum + "?: "); //Get sale totals from user
    //sales = keyboard.nextInt(); //Place the input from the user in the store1 variable.

    //Display Store number
    System.out.print("Store " + storeNum);


    for (int count = 0; count<sales1; count+=100)
    {
    //Display sales in hundreds
    System.out.print("*");
    }


    for (int count1 = 0; count1<sales2; count1+=100)
    {
    //Display sales in hundreds
    System.out.print("*");
    }


    for (int count2 = 0; count2<sales3; count2+=100)
    {
    //Display sales in hundreds
    System.out.print("*");
    }


    for (int count3 = 0; count3<sales4; count3+=100)
    {
    //Display sales in hundreds
    System.out.print("*");
    }


    for (int count4 = 0; count4<sales5; count4+=100)
    {
    //Display sales in hundreds
    System.out.print("*");
    }
    System.out.println();
    }


    }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: how to display a bar chart using loops

    Hi dwheeler,

    It looks like the loops "for" of all the counters (counter1, counter2,...) will execute for every store (store1, store2,....)
    For store1 only counter1 must be used to print the *s, for store2 counter2, and so on, to have correct values, need to change the use of these loops.


    BR

    --- Update ---

    Also looks like using count++ gives better result than count+=100, because there is already condition count<sales1 in the loop.
    To correct the code, need find a way to use only one counter for one corresponding store, I am beginner, maybe some more experienced people will confirm or correct.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to display a bar chart using loops

    I don't really know what I need to change it to. I have a different count variable for each store number. I need it to look like this:
    store 1 *
    store 2 **
    store 3 ***
    store 4 ****
    store 5 ******
    But instead it ends up with all the totals added together and looks like this:

    Store 1 *********
    Store 2 *********
    Store 3 *********
    Store 4 *********
    Store 5 *********

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: how to display a bar chart using loops

    Yes, I think because in the main loop, for storeNum = 1, all the loops for the counters will execute one after the other (count, count1,...,count4) and gives 5 stars, then the program will come back to the main loop and increment the storeNum to 2, and do then also execute all the 5 count, and so on, I think this is what is happening.
    But still not find a way how to correct it just by changing the use of the loop.

  5. The Following User Says Thank You to Hazimo13 For This Useful Post:

    dwheeler (September 20th, 2013)

  6. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to display a bar chart using loops

    Thanks Hazimo13,
    It's not due for a couple weeks, I'm actually quite far ahead in my homework so I have time to work with it and try and figure it out.

  7. #6
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: how to display a bar chart using loops

    You are on the right track with nested loops. I would recommend taking a look at array's next to get your desired result in a compact and dynamic manner.
    // It will turn this
    String name1;
    String name2;
    etc....
    String name100;
     
    // Into this
    String[] name = new String[100];
     
    // To access specific stores you would simply do something such as
    name[x] // Where x is it would be replaced with a number 0 - starting position of an array to 99 (Highest position of this array specified when creating the array)

    Array's are not limited to Strings but it is just a small example to show you the basics of it. If you have any issues learning array's just let us know we will be glad to help you out.

  8. The Following User Says Thank You to Ubiquitous For This Useful Post:

    Hazimo13 (September 21st, 2013)

  9. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: how to display a bar chart using loops

    Thanks Ubiquitous,
    I tried to help dwheeler and I am learning at the same time, I have used the arrays as you suggested, and it made the code lighter, (with only 2 "for" loops in the code) and it's working fine now.

    This way I used only one "for" loop for counter, instead of one loop for every store, I played with the parameters i and j for example inside the arrays.
    (one array for storeNum, and a second new array fore sales), and then in the loops I use the arrays with parameters i and j.
    Thank you both , This is helpful for me to learn and understand better loops and arrays.
    dwheeler feel free to ask if some clarification needed.

    Regards.

Similar Threads

  1. how to display a bar chart using loops
    By tirashad in forum Loops & Control Statements
    Replies: 5
    Last Post: August 11th, 2013, 12:59 AM
  2. To check presence of padlock icon in the location bar /address bar
    By Rexy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2013, 06:44 AM
  3. Replies: 2
    Last Post: April 6th, 2012, 01:58 AM
  4. scatterplot chart
    By anikal in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2011, 11:47 PM
  5. [Swing-Progress Bar] Can't resize progress bar
    By Grabar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 14th, 2010, 12:27 PM