Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Question Help with Horizontal Asterisk Bar Chart with X's

    I've been working on this all day. and specifically JUST this problem. I'm writting a program that reuires me to make a bar out of asterisks and that every 10th asterisk should be changed to an "X".
    I tried many different solutions either from the book, internet and own logic. nothing seems to be working 100%. the closest I have gotten is this Code
    Java Code:
    import javax.swing.JOptionPane;
    
    public class BarChartProgram
    {
    	public static void main(String []args)
    	{
    	
    	int Store1Sales;
    	int Store2Sales;
    	int Store3Sales;
    
    	String Store1 = JOptionPane.showInputDialog("Enter today's sales for store 1:");
    	Store1Sales = Integer.parseInt(Store1); 
    	while (Store1Sales<=-1)
    	{
    	String invalid1 = JOptionPane.showInputDialog ("Invalid input - enter a value of zero or greater for Store 1: ");
    	Store1Sales = Integer.parseInt(invalid1);
    	}
    	
    	String Store2 = JOptionPane.showInputDialog("Enter today's sales for store 2:");
    	Store2Sales = Integer.parseInt(Store2); 
    	while (Store2Sales<=-1)
    	{
    	String invalid2 = JOptionPane.showInputDialog ("Invalid input - enter a value of zero or greater for Store 2: ");
    	Store2Sales = Integer.parseInt(invalid2);
    	}
    
    	String Store3 = JOptionPane.showInputDialog("Enter today's sales for store 3:");
    	Store3Sales = Integer.parseInt(Store3); 
    	while (Store3Sales<=-1)
    	{
    	String invalid3 = JOptionPane.showInputDialog ("Invalid input - enter a value of zero or greater for Store 3: ");
    	Store3Sales = Integer.parseInt(invalid3);
    	}
    	
    	System.out.print("Store 1: ");
    	for(int k=0, l=2; k<Store1Sales-(Store1Sales/10); k++,l++){
    	System.out.print("*");
    	if(l==10){
    	System.out.print("X");
    	l=1;
    	}
    	}
    	System.out.print("\n");
    	
    	System.out.print("Store 2: ");
    	for(int k=0, l=2; k<Store2Sales-(Store2Sales/10); k++,l++){
    	System.out.print("*");
    	if(l==10){
    	System.out.print("X");
    	l=1;
    	}
    	}
    	
    	System.out.print("\n");
    	
    	System.out.print("Store 3: ");
    	for(int k=0,l=2; k<Store3Sales-(Store3Sales/10); k++, l++){
    	System.out.print("*");
    	if(l==10){
    	System.out.print("X");
    	l=1;
    	}
    	}
    	
    	System.out.print("\n");
    	}
    }
    The problem I am having here is that all values that end in "9" are displayed in output as 1 number higher. (IE: 9 becomes 10, 19 becomes 20, etc..) but 10 and 20 do not display an improper amount of asterisks or X's and all other numbers loop properly.

    I am limited to loops at this time so advanced methods or statements will not help, (i'd just get marked down for being ambitious). I would greatly appreciate the help you can give me.
    Last edited by pbrockway2; 10-21-2012 at 04:46 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help with Horizontal Asterisk Bar Chart with X's

    Hi comptechgivinsky, welcome to the forums! I've added "code" tags to your post. Put [code] at the start of a section of code and [/code] at the end: that way the forum software will preserve the formatting and the code will be readable.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help with Horizontal Asterisk Bar Chart with X's

    Basically you're printing logic is wrong. If you are printing for 9, say, you loop executes 9 times. On the ninth (last) time the value of l is 10 and you get the spurious "X".

    I think you are making it overly complex by printing both a * and a X during the same path of the loop. Go around the loop as many times as there are sales and each time you go around print just one symbol (use an if statement to decide which, much as you're doing at the moment.)

    You can decide whether it's time to print X instead of * by using a second loop counter as you are doing. (I would have initialised and reinitialised it to zero and checked whether it had become equal to 9). Changing the value of a loop variable within a loop is fraught with danger (as you've found!) and a more straight forward approach is to use a single loop variable. That variable can be used to tell whether it's time to stop, but it can also be used to tell if you're at a multiple of ten.

    The % (remainder) operator is designed for just this purpose. It returns the remainder after integer division. So 16/3 is 1, 15/3 is 0, etc. A number is a multiple of ten when it has zero remainder, ie when (k+1)%10==0.

    ---

    Follow standard Java coding conventions and begin variables with a lower case letter. Eg, store1Sales.

  4. #4
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Help with Horizontal Asterisk Bar Chart with X's

    Okay, so after I posted this I wound up getting some help from a source i didn't think could help. after reviewing a bunch of logic.. we came up with a loop that seems to hold true in every aspect.. now that I have received the same help here, my only questions is if this would be considered a good program structure.
    Java Code:
    System.out.print("Store 1: ");
    	for(int k=0, l=1; k<store1Sales); k++,l++){
    		if(l==10){
    	        System.out.print("X");
    	        l=1;
    	        }
                    else {System.out.print(“*”)
                    }
    	}
    	System.out.print("\n");
    this code was replicated for store 2 and 3 and replaced with appropriate values.

Similar Threads

  1. jfreechart plotting line chart on candlestick chart
    By amlwwalker in forum New To Java
    Replies: 0
    Last Post: 10-07-2012, 02:18 AM
  2. Help with horizontal output
    By IdrinkJava in forum New To Java
    Replies: 6
    Last Post: 01-28-2011, 06:00 PM
  3. Print Horizontal Bars
    By xstrandedx7688 in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 05:52 AM
  4. Replies: 0
    Last Post: 07-02-2008, 09:06 PM
  5. Demo bar chart and pie chart
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:22 AM

Tags for this Thread

Posting Permissions

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