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: 2 questions about making bar chart using asteriks

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2 questions about making bar chart using asteriks

    import java.util.Scanner; //import scanner statement
     
    public class BarChartProgram
    {
    	public static void main(String [] args)
    	{
    		Scanner keyboard = new Scanner (System.in); //scanner object
    		int store1Sales;                            //number of store1 sales
          int store2Sales;                            //number of store2 sales
          int store3Sales;                            //number of store 3 sales
          int cols1;                                  //Asterisks line 1      
          int cols2;                                  //Asterisks line 2 
          int cols3;                                  //Asterisks line 3
     
          //Prompt store1 sales number
          System.out.print("Enter today's sales for store 1: ");  
          store1Sales = keyboard.nextInt();
          while (store1Sales < 0) //while statement if prompt number is less than 0
          {  
             System.out.print("Invalid input - enter a value of zero or greater: ");
             store1Sales = keyboard.nextInt();
          }
     
          //Prompt store2 sales number
          System.out.print("Enter today's sales for store 2: ");
          store2Sales = keyboard.nextInt();
          while (store2Sales < 0) //while statement if prompt number is less than 0
          {  
             System.out.print("Invalid input - enter a value of zero or greater: ");
             store2Sales = keyboard.nextInt();
          }
     
          //Prompt store3 sales number
          System.out.print("Enter today's sales for store 3: ");
          store3Sales = keyboard.nextInt();
          while (store3Sales < 0) //while statement if prompt number is less than 0
          {  
             System.out.print("Invalid input - enter a value of zero or greater: ");
             store3Sales = keyboard.nextInt();
          }
          System.out.println("");
          System.out.println("SALES BAR CHART\n");
     
     
          //Print out store1 sales number in asteriks
          System.out.print("Store 1: ");
          for (cols1 = 1; cols1 <= store1Sales; ++cols1)
          {
             System.out.print("*");
          }
          System.out.println("");
     
     
          //Print out store2 sales number in asteriks
          System.out.print("Store 2: ");
          for (cols2 = 1; cols2 <= store2Sales; ++cols2)
          {
             System.out.print("*");
          }     
          System.out.println("");
     
     
          //Print out store3 sales number in asteriks
          System.out.print("Store 3: ");
          for (cols3 = 1; cols3 <= store3Sales; ++cols3)
          {
             System.out.print("*");
          }
       }
    }

    Edit: I figured out 1st question.


    2nd question
    How do I make it so replace every tenth asteriks with "X"?
    for example, if i enter 15 for store1 sales, it shows like

    Store1: *********X*****

    I've been on this for a while but just can't figure it out.

    this is example of print out result

    Enter today's sales for store 1: 9
    Enter today's sales for store 2: -2
    Invalid input - enter a value of zero or greater: -1
    Invalid input - enter a value of zero or greater: 23
    Enter today's sales for store 3: 16

    SALES BAR CHART

    Store 1: *********
    Store 2: *********x*********x***
    Store 3: *********x******

    and currently mine would print it out like

    SALES BAR CHART

    Store 1: *********
    Store 2: ***********************
    Store 3: ****************


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

    Default Re: 2 questions about making bar chart using asteriks

    Use an if statement with the remainder (or modulus) operator in your for loop.

    pseudocode:

    if ( cols % 10 == 0 )
    print ( "X" )
    else
    print ( "*" )

    You may have to do some special magic for cols = 0, but you can figure that out.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 questions about making bar chart using asteriks

    oh.. that worked. thx a lot.

Similar Threads

  1. Re: how to display a bar chart using loops
    By dwheeler in forum Loops & Control Statements
    Replies: 6
    Last Post: September 21st, 2013, 05:28 PM
  2. 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
  3. 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
  4. Pls I need help on these questions that are making me go crazy
    By Topiloe in forum Java Theory & Questions
    Replies: 3
    Last Post: September 27th, 2012, 02:27 AM
  5. Replies: 2
    Last Post: April 6th, 2012, 01:58 AM