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

Thread: how to display a bar chart using loops

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Location
    HCM city, Vietnam
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default how to display a bar chart using loops

    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.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to display a bar chart using loops

    Have you done arrays yet? If so, then you could have an array of sales totals:

    int[] salesTotals = new int[5];

    Without arrays, using a loop to gather and store or use variables becomes more difficult.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Location
    HCM city, Vietnam
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to display a bar chart using loops

    well, sadly i havent learned about arrays. im currently at chapter 4 of a "starting out with Java" book and all i have covered so far are basic things such as conditional statements and loops. my teacher also said that this problem would be solved easily with an array. however its a loop exercise so im supposed to solve it with few things i have learned only.

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to display a bar chart using loops

    Quote Originally Posted by tirashad View Post
    well, sadly i havent learned about arrays. im currently at chapter 4 of a "starting out with Java" book and all i have covered so far are basic things such as conditional statements and loops. my teacher also said that this problem would be solved easily with an array. however its a loop exercise so im supposed to solve it with few things i have learned only.
    Well it's pretty easy to do this task using arrays, but since you don't want to use it you'll have to think some more

  5. #5
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: how to display a bar chart using loops

    Unless your teacher/instructor specifically says "Don't declare 5 variables in a row and use them", then I see nothing wrong with using 5 separate values and assigning them when the user is giving the option to provide input. It's when you come across programs that need much more than 5 variables to store all their data, then declaring one int at a time becomes ineffiecient.

    But it's never a bad thing to start off with great programming habits. Google arrays, it shouldn't take more than 30min to have them down.

  6. The Following User Says Thank You to AlexHail For This Useful Post:

    tirashad (September 23rd, 2013)

  7. #6
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: how to display a bar chart using loops

    This is how I'd do it. I'd declare 5 variables.
    Write on them the amount of the sales.
    Get the highest amount and depending on how big is it, divide it by multiples of 10 or 100.
    Lets say highest sale was $1000, then you'd divide that by 100, and do the same to the others.
    Also remember that the " / " operator doesnt give you the exact amount all the time, gives you a whole number.
    That way you'll have a whole number for each variable related to what they sold.

    The easiest way to go, is to print out the bars with symbols like "#" horizontally.

    Lets say..

    Andrew ########## ($1000)
    Johnny ##### ($500)
    Joseph ####### ($600)
    .
    .
    and so on.

    I'd suggest on using the System.out.println(""). That way you'll keep writting or adding a "#" with loop for the number given to the amount they sold.
    Remeber, you can use System.out.printlm("\n") is the same as having System.out.print("") which will make you write on the next line in the command prompt.

    I hope you find this helpful.

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

    tirashad (September 23rd, 2013)

Similar Threads

  1. 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
  2. Help with pie chart
    By Wise girl in forum Java Theory & Questions
    Replies: 12
    Last Post: June 2nd, 2012, 07:07 PM
  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