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

Thread: Complete beginner (college student) - code not working!!

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Complete beginner (college student) - code not working!!


    Hi, I am new to Java and part of my college assignment is to complete a series of exercises creating java programs and test them in the command window.
    I have missed some lessons due to illness/being away and now i'm struggling with a few and I can't figure out what i'm doing wrong although I know it must be something relatively simple if I knew how...

    My code is supposed to ask for an input 10 times, then calculate and output the total of all commission then output the average. At first I had commission++ below the input which was adding 1 plus the input each time which was wrong and I can't figure out whether i am missing a variable or if my maths is just totally wrong (not skilled in either area).

    Please help....
    import java.util.Scanner;
     
    class commission
     
    {
       public static void main (String [] args)
       {  
    	Scanner input = new Scanner(System.in);	    
     
    	int commission, totcom, staff, comav;
     
    	staff = totcom = comav = 0; // set the number to 0
    	for (staff = 1; staff <=10; staff++)
     
    		{
    		System.out.println("Please input the amount of commission");
    		commission = input.nextInt();
    		//commission+;
     
    		totcom = (commission * staff );
    		comav = (commission / staff);
    		}
     
    	System.out.println("================================");
    	System.out.println("The amount of commission overall is " + totcom);
    	System.out.println("================================");
    	System.out.println("The average amount of commission is " + comav);
     
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Complete beginner (college student) - code not working!!

    Please copy and paste here the contents of the command prompt window where you execute the code. It should show the input to the program and what the program prints out.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    Also Please edit your post and wrap your code with
    [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
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Complete beginner (college student) - code not working!!

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\latham Family>e:

    E:\>cd java/5.4 commission

    E:\Java\5.4 Commission>javac commission.java

    E:\Java\5.4 Commission>java commission
    Please input the amount of commission
    5
    Please input the amount of commission
    6
    Please input the amount of commission
    4
    Please input the amount of commission
    3
    Please input the amount of commission
    4
    Please input the amount of commission
    5
    Please input the amount of commission
    6
    Please input the amount of commission
    7
    Please input the amount of commission
    8
    Please input the amount of commission
    8
    ============================== ==
    The amount of commission overall is 80
    ============================== ==
    The average amount of commission is 0

    E:\Java\5.4 Commission>[COLOR="Silver"]

    --- Update ---
    Taken out duplicate copy

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Complete beginner (college student) - code not working!!

    You haven't explained what the problem with the output is.
    If the problem is the 0, its due to integer arithmetic. for example: 6/8= 0
    To get decimal places make one of the operands a double: 6.0/8 = 0.75

    Otherwise please explain what the problem is. Show what you expect the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Complete beginner (college student) - code not working!!

    The total amount of numbers inputted there should come to 56, however it's displaying 80 at the output as I am assuming it's multiplying 10 (the times the loops goes round) by the last value inputted (8) when I want it to calculate all of the inputted numbers as a total.

    I'm not sure if i've missed the fact I need to 'store' each of the numbers somewhere to enable me to calculate it or if my maths is totally wrong... I think maybe both.

    --- Update ---

    I haven't dealt with the average output yet, I figure if I can resolve the calculation problem then I will be able to average the number

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Complete beginner (college student) - code not working!!

    When you use a calculator to add up a group of numbers, you keep a running total and don't clear the total after each number is added. The same with a program that is to total some values. You add each new number to the total instead of assigning the new number to the total. Assigning clears out the previous value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Complete beginner (college student) - code not working!!

    What is happening is that the commission variable is being replaced by each new user entry.

    When it comes to totalling the average commission, it uses the most recent entry (commission) instead of the sum of entries (totcom).

    --

    Try also to use Java conventions when it comes to naming classes. It should always have a capital letter.

    When using a scanner, it's also advisable to close it after use.
    input.close();

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

    18107 (January 19th, 2013)

Similar Threads

  1. Replies: 4
    Last Post: December 4th, 2012, 06:11 PM
  2. Beginner Java Student Having Some Trouble
    By csprung in forum Loops & Control Statements
    Replies: 1
    Last Post: April 25th, 2012, 08:14 AM
  3. Replies: 10
    Last Post: October 26th, 2011, 02:22 PM
  4. Complete but not working properly
    By Noob101 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 03:17 PM
  5. How to complete code
    By Shay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2010, 01:59 PM