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

Thread: Loop is not working in calcualtion program

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop is not working in calcualtion program

    I have made a calculation program, but having a problem with the loop at the end. The rest of the program works, just then end part is needing some attention. I am trying to get it to increase in 5,000.00 increments to 50% of the total. If inputed value is 1,000.00 i want it to print out in 5,000.00 increments to 1,500.00. The program just outputs the initial value and then stops when it comes to the loop. Any direction would be greatly appreciated. Been banging my head on this for the past 4 days.


    //This program calculates annual sales
    import java.util.Scanner; // Program assigned to use Scanner for input

    public class AnnualSales
    {
    //Main method begins execution of Java application
    public static void main(String[] args)
    {
    //create scanner to obtain input from the command window
    Scanner input=new Scanner(System.in);

    double annualSales; //Annual sales made during the year
    double annualTotal = 0; //The total annual compensation
    double salary; //Annual Salary of sales representative
    double commRate; //Commission rate
    double salesTarget;
    double potSales; //Potential sales
    double counter;

    salary = 60000; //set rate of 60,000.00 per sales rep
    commRate=.10; //set commission rate of 10 percent
    salesTarget=100000;


    System.out.println("The current sales target is $100,000.00");
    System.out.print("Enter the amount of sales:$"); //input query of sales
    annualSales = input.nextInt(); //input value stored



    if (annualSales >= 80000)
    {
    if (annualSales < 100000)
    {
    annualTotal = annualSales * commRate + salary;
    System.out.println("You earned 10% sales incentive!");
    System.out.println("Total compensation is :$" + annualTotal);
    }

    else
    {
    annualTotal = annualSales * (commRate * 2.5) + salary;
    System.out.println("You earned a 10% sales incentive!");
    System.out.println("You also earned the extra incentive!");
    System.out.println("Total compensation is :$" + annualTotal);
    }
    }
    else
    {
    //output of claculation
    System.out.println("No sales incentive was earned.");
    System.out.println("The total compensation is:$" + salary);
    }

    potSales = annualSales * .50 + annualSales;

    System.out.println("This is your potential sales in 5k increments.");
    System.out.println("Total Sales Total Compensation");
    System.out.println("----------------------------------");

    for (counter = annualSales; counter <= potSales; counter++);
    // the loop and then print the commission with the projected sales.
    System.out.println(counter + "\t\t" + counter );

    } //end main method
    } //end class AnnualSales


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Loop is not working in calcualtion program

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop is not working in calcualtion program

    //This program calculates annual sales 
    import java.util.Scanner;   // Program assigened to use Scanner for input
     
    public class AnnualSales
    {
        //Main method begins execution of Java application
        public static void main(String[] args)
        {
            //create scanner to obtain input from the command window
            Scanner input=new Scanner(System.in);
     
            double annualSales;    //Annual sales made during the year
            double annualTotal = 0;    //The total annual compensation 
            double salary;         //Annual Salary of sales representative
            double commRate;       //Commission rate
            double salesTarget;
            double potSales;           //Potential sales
            double counter;
     
            salary = 60000;      //set rate of 60,000.00 per sales rep
            commRate=.10;       //set commission rate of 10 percent
            salesTarget=100000;
     
     
            System.out.println("The current sales target is $100,000.00");
            System.out.print("Enter the amount of sales:$"); //input query of sales
            annualSales = input.nextInt();                  //input value stored
     
     
     
            if (annualSales >= 80000)
            {
                if (annualSales < 100000)
                {
                    annualTotal = annualSales * commRate + salary;
                    System.out.println("You earned 10% sales incentive!");
                    System.out.println("Total compensation is :$" + annualTotal);
                }
     
                else
                {
                    annualTotal = annualSales * (commRate * 2.5) + salary;
                    System.out.println("You earned a 10% sales incentive!");
                    System.out.println("You also earned the extra incentive!");
                    System.out.println("Total compensation is :$" + annualTotal);
                }
            }
            else
            {
                //output of claculation
                System.out.println("No sales incentive was earned.");
                System.out.println("The total compensation is:$" + salary);
            }
     
            potSales = annualSales * .50 + annualSales;
     
            System.out.println("This is your potential sales in 5k increments.");
            System.out.println("Total Sales     Total Compensation");
            System.out.println("----------------------------------");
     
            for (counter = annualSales; counter <= potSales; counter+=500)
            {
    	        System.out.println (  counter + "\t\t" + annualTotal);
            }
        } //end main method
    } //end class AnnualSales

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Loop is not working in calcualtion program

    First, you better put your codes in code tags so it would be easy for us to read

    -There is a semi colon at the end of your loop.
    And please put braces in your loop even though it has only one statement.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Loop is not working in calcualtion program

    Can you post the program's output that shows what is wrong?
    Add some comments describing what is wrong and show what the correct output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop is not working in calcualtion program

    Do I not have the braces in the correct spot? The compiler did not give any errors. Still new to all this Java thing, so forgive me . If the semicolon is not there it errors out before i can run it.[COLOR="Silver"]

    --- Update ---
    The total compensation is:$60000.0
    This is your potential sales in 5k increments.
    Total Sales     Total Compensation
    ----------------------------------
    50000.0		0.0
    55000.0		0.0
    60000.0		0.0
    65000.0		0.0
    70000.0		0.0
    75000.0		0.0


    I have it incrementing now, but the 2nd column is not changing, I think i have a calculation wrong somewhere

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Loop is not working in calcualtion program

    Where is the contents of the variable that is printing as 0.0 given any value?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Loop is not working in calcualtion program

    your annualTotal has initial value of 0. And it will only overwrite when the input is greater than or equal to 80000 if (annualSales >= 80000) otherwise it will still be 0.

    Try to input a value of greater than 80000. Your annualTotal would have a value (but never change since it is overwriting takes only once).

  9. #9
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop is not working in calcualtion program

    I noticed that also about the initial value of it being 0. That's what was causing it to give me a column of zeros. It flagged me to initialize it and it changed it on it's own. Now when i run the program it comes out with this
    The current sales target is $100,000.00
    Enter the amount of sales:$5000
    No sales incentive was earned.
    The total compensation is:$60000.0
    This is your potential sales in 5k increments.
    Total Sales     Total Compensation
    ----------------------------------
    5000.0		65000.0

    now it stops incrementing....weird

    --- Update ---

    I changed using a different variable this time and removed the zero...I also went over the 80,000 mark..now i get this

    The current sales target is $100,000.00
    Enter the amount of sales:$85000
    You earned 10% sales incentive!
    Total compensation is :$68500.0
    This is your potential sales in 5k increments.
    Total Sales     Total Compensation
    ----------------------------------
    85000.0		145000.0
    90000.0		145000.0
    95000.0		145000.0
    100000.0		145000.0
    105000.0		145000.0
    110000.0		145000.0
    115000.0		145000.0
    120000.0		145000.0
    125000.0		145000.0
    BUILD SUCCESSFUL (total time: 4 seconds)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Loop is not working in calcualtion program

    .now i get this
    Is that what you want for output? If not please explain and give an example.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Loop is not working in calcualtion program

    I noticed that also about the initial value of it being 0. That's what was causing it to give me a column of zeros.
    Actually the reason of it being zero is because your input is less than 80000. And you never overwrite it inside the loop.
    Try to input a value of greater than 80000. it will change even though it has initial value of zero. Then you can think what would be the next step after that.

  12. #12
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop is not working in calcualtion program

    Sorry hard to explain.
    the first column should output the initial sales amount and then increase in 5,000.00 increments till it reaches 50% of its initial input.

    The 2nd column should show total sales from the first column + the base salary (60,000.00)
    (example if i enter in 85,000 then it should show in 2nd column 68,500. (60,000 + 8,500)

    then of course everything increments from there

    --- Update ---

    I just noticed something else.. I need the variable "annualTotal" to carry over into the loop code from the calculations above. It is not carrying over and I think that is where my main problem is. I changed to a different variable total, but what i need is the annualTotal...Any reason why it would not carry over into the loop?
            potSales = annualSales * .50 + annualSales;
     
     
            System.out.println("This is your potential sales in 5k increments.");
            System.out.println("Total Sales     Total Compensation");
            System.out.println("----------------------------------");
     
            for (counter = annualSales; counter <= potSales; counter+=5000)
            {
    	        System.out.println (counter + "\t\t" + annualTotal );
            }
     
        } //end main method
    } //end class AnnualSales

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Loop is not working in calcualtion program

    why it would not carry over into the loop?
    What do you mean by "carry over"? The variable: annualTotal is given a value just before the loop. It will keep that value UNTIL it is assigned a different value. Since there isn't any code inside the loop that changes its value, the value will remain the same. If you want it to have different values, the code needs to assign those values to it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. while loop is not working right
    By Kattracks32 in forum Loops & Control Statements
    Replies: 7
    Last Post: February 20th, 2013, 04:26 AM
  2. Just can't see why this loop is not working.
    By Umbrafalx in forum Loops & Control Statements
    Replies: 6
    Last Post: September 12th, 2012, 09:29 AM
  3. How is this nested for loop working!
    By samadniz in forum Member Introductions
    Replies: 2
    Last Post: September 3rd, 2012, 09:59 AM
  4. Loop not working
    By Lurie1 in forum Loops & Control Statements
    Replies: 12
    Last Post: March 7th, 2012, 05:27 AM
  5. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 09:05 AM