I posted with a program problem yesterday and couldn't get anywhere with all my errors. So I started over with a new program this morning and now have an infinate loop and a couple of questions about what is going on. My program is suppose to ask for total sales for the week, then find commission on those sales, add base salary of $200 then display a table with all salaries listed with "*" keeping tally of all inputs.
ex
200-299***
300-399*
and so on.

Here are my questions and my problem...When I run my program I input 1 sales figure and the program goes into a never ending loop. Why? ---and where are all the tallies "*" beside my references coming from and why?
(here is the output I receive)
100-199: ***
200-299: ****
300-399: *****
400-499: ******
500-599: *******
600-699: ********
700-799: *********
800-899: **********
00-99: **

Here is my code :

import java.util.Scanner;//program uses scanner

public class Sales
{


    public static void main(String[] args)
    {
        Scanner input = new Scanner ( System.in);//scanner uses user input
        int dollars;//used for sales
        int WeeklySalary;//used for commission and weekly pay
        int commission;//used for finding commission
        int Sales[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        System.out.println( "Enter total sales in dollar amount--" +
                "enter a negative number to quit:");//prompt
        //for salesman sales figures
        dollars = input.nextInt();//accepts sales as dollars

        while  (dollars > 0)
        {
        
        commission = (int) (dollars * .09);//finds commission
        WeeklySalary = commission + 200;//finds weekly pay
        

        //for each array element, output a bar of the chart
        for ( int counter = 0; counter < Sales.length; counter++ )
        {
            ////output bar label ( "200-299:"....""900-999: ", 1000+: " )

            if ( counter == 100 )
                System.out.printf( "%5d: ", 1000);
            else
                System.out.printf( "%02d-%02d: ", counter * 100, counter *
                        100 + 99 );

            //print bar of astrisks
            for( int stars = 0; stars < Sales[ counter ]; stars++ )
                System.out.print ( "*" );

            System.out.println();//start a new line of output
        }//end outer for
        }//end while
    }//end main
}//end class BarChart

Recommended Answers

All 3 Replies

>When I run my program I input 1 sales figure and the program goes into a never ending loop. Why?
Because you never change this condition

while  (dollars > 0)
        {

>and where are all the tallies "*" beside my references coming from and why
They are coming from your loop on your fixed "Sales" array. Trace the value of "counter" through those loops and compare them to the output you are getting.

I changed my while to an if , it stopped the infinite loop but now it only goes through one input. It is suppose to continue to accept inputs until a negative number is the input. And I still don't understand why all the tallies. I guess I don't understand how to "trace" my values through my loop. I still working on it but if you could give me some hints I would appreciate it greatly.

Your while loop is fine as long as you re-prompt for the next value at the end of your loop code. In pseudo code, you want something like this

prompt "continue?"
while ("yes"){
 // do stuff

  prompt "continue?"
}

As for as "tracing", just start with the initial value of "counter" in your loop (which is zero) and walk it through your code mentally (or on paper if you prefer). That's all tracing is. You don't have to run through it very far to understand why you are getting the output that you are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.