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

Thread: My java program won't add up the numbers?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My java program won't add up the numbers?

    Hi,

    I don't know why my java program won't add up the numbers but here is the code:

    import java.util.Scanner;
    /* Design, code and test a program to enable a user to type in four savers' balances and the percentage bonus (which is the same for everyone). The program should then output the new balances for each saver. */
     
    class saverBonus
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    		double newBalance, bonus, fourSavers, sum, percentageBonus;
     
    		sum = 0;
     
    		System.out.print("Please input the first savers' balance: ");
    		fourSavers = input.nextDouble();
     
    		System.out.print("Please input the second savers' balance: ");
    		fourSavers = input.nextDouble();
     
    		System.out.print("Please input the third savers' balance: ");
    		fourSavers = input.nextDouble();
     
    		System.out.print("Please input the fourth savers' balance: ");
    		fourSavers = input.nextDouble();
     
    		sum = fourSavers + fourSavers;
     
    		System.out.print(fourSavers + sum);
    	}
    }

    What can I do to alter the code to make it add up the numbers together?

    Thanks


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    308
    My Mood
    Bored
    Thanks
    45
    Thanked 86 Times in 72 Posts
    Blog Entries
    4

    Default Re: My java program won't add up the numbers?

    Every time you ask for a new input you are over-writing the last one. You will need separate variables for each input.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: My java program won't add up the numbers?

    Umm.... What will this code do?
    fourSavers+=fourSavers;
    // Assuming that fourSavers is initialized by 0

  4. #4
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: My java program won't add up the numbers?

    Idk about your code but i'm guessing

    fourSavers = fourSavers + input.nextDouble(); would work.. Not sure if this would work but deffiently take ^'s advice.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: My java program won't add up the numbers?

    seems like your expecting to add FOUR numbers from FOUR inputs? right?, then you should have 4 variables to hold EACH of the FOUR inputs, then work aroundwith those 4 and you might get the sum of the FOUR

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: My java program won't add up the numbers?

    Quote Originally Posted by chronoz13 View Post
    seems like your expecting to add FOUR numbers from FOUR inputs? right?, then you should have 4 variables to hold EACH of the FOUR inputs, then work aroundwith those 4 and you might get the sum of the FOUR
    Why not using an array instead of creating four different variables? Or use a single variable and add it up for all four inputs as OP seems to calculate the sum of all inputs. So, there is no need to use four different variables or any array.

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: My java program won't add up the numbers?

    You are using too many variables, if u don't need them anymore u can just sum them to fourSavers, and just print.
    Also u are summing sum and fourSavers in system.out.print();
    i've tried it to see what was the solution and here it is:

    import java.util.Scanner;
     
    public class saverBonus{
    		public static void main(String[] args)
    		{
    			Scanner input = new Scanner(System.in);
    			double fourSavers = 0.00;
     
    			sum = 0;
     
    			System.out.print("Please input the first savers' balance: ");
    			fourSavers += input.nextDouble();
     
    			System.out.print("Please input the second savers' balance: ");
    			fourSavers += input.nextDouble();
     
    			System.out.print("Please input the third savers' balance: ");
    			fourSavers += input.nextDouble();
     
    			System.out.print("Please input the fourth savers' balance: ");
    			fourSavers += input.nextDouble();
     
    			System.out.print(fourSavers);
    		}
    }

    This if u don't need the values anywhere else, u only use one variable and just add the input to it

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: My java program won't add up the numbers?

    Quote Originally Posted by Nhedro View Post
    i've tried it to see what was the solution and here it is:
    ...

    Nhedro, please read the forum rules and the following article:
    The Problem with Spoon-feeding

Similar Threads

  1. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  2. Need help with an Octal Numbers program
    By anrowl01 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 23rd, 2011, 03:32 AM
  3. Complex Numbers in Java
    By JavaNovice03355 in forum Member Introductions
    Replies: 1
    Last Post: May 18th, 2011, 07:30 PM
  4. using Eclipse and every program gives the errorr with floating numbers
    By Neera in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2010, 11:11 AM
  5. [SOLVED] Problem with a tutorial program(Adding the answer of two squared numbers together)
    By Melawe in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 7th, 2010, 09:03 AM