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

Thread: Cumulative sum of entries

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Cumulative sum of entries

    I need a little help.

    Not sure where to go from here.

    public static void main(String[] args)
        {
            Scanner keyboard = new Scanner (System.in);
     
            int value;
            int total;
     
            do
            {
                System.out.print("Enter some numbers: ");
                value = keyboard.nextInt();
     
     
     
     
                System.out.println("\nYou entered numbers " + value + ".\n");
     
     
            }
            while ( value != 0 );
        }

    After each entry, the program reports the cumulative sum of the entries to date. The program terminates when the user enters a zero. If your program received as data, the values 2, 7, 3 and 0, your program should display 2, 9 and 12. Be sure to label your input and your output in an appropriate manner.
    Last edited by CSUTD; November 1st, 2011 at 03:03 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Cumulative sum of entries

    How would you do this by hand, without a computer? If somebody tells you a list of numbers, how would you keep adding them up? Write out instructions in English, that you could give to your dumbest friend, and you'll have an algorithm that should be pretty easy to translate to code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Cumulative sum of entries

    Is there a simpler way to print everything out on two lines?

    Example:
    You entered numbers 2,7, 3, and 0.
    Your totals are 2, 9, and 12.

    {
            Scanner keyboard = new Scanner (System.in);
     
            int value = 0;
            int total = 0;
     
            do
            {
                System.out.print("Enter some numbers: ");
                value = keyboard.nextInt();
     
                total = total + value;
     
                System.out.println("\nYou entered numbers " + value + ".");
                System.out.println("Your total are " + total );
     
            }
            while ( value != 0 );
        }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Cumulative sum of entries

    I'm not sure what you mean by simpler, and I'm not convinced that code does what your example says it does.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Cumulative sum of entries

    This is my output:
    Enter some numbers: 2
     
    You entered numbers 2.
    Your total are 2
    Enter some numbers: 7
     
    You entered numbers 7.
    Your total are 9
    Enter some numbers: 3
     
    You entered numbers 3.
    Your total are 12
    Enter some numbers: 0
     
    You entered numbers 0.
    Your total are 12

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Cumulative sum of entries

    That's what I would expect your output to be. If you want to print them out on the same line, you're going to have to save them to an array or a String and wait until after the loop to print them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Cumulative sum of entries

    Quote Originally Posted by CSUTD View Post
    This is my output:
    Good for you. What do you want us to do about it?
    Improving the world one idiot at a time!

  8. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Cumulative sum of entries

    You may want to have a look in ArrayList, which can be found here:

    ArrayList (Java Platform SE 6)
    Last edited by Tjstretch; November 1st, 2011 at 06:12 PM.

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Cumulative sum of entries

    Quote Originally Posted by KevinWorkman View Post
    That's what I would expect your output to be. If you want to print them out on the same line, you're going to have to save them to an array or a String and wait until after the loop to print them.
    Can't use arrays. Even if we were I wouldn't know how. I am in an intro class. I only know to a certain point.

    Can I ask why you didn't think my code produced what it should.

    Quote Originally Posted by Junky View Post
    Good for you. What do you want us to do about it?
    um, if you would have read the thread you would have known the question. You don't have to answer it or provide help but don't post useless stuff on a thread.

    Quote Originally Posted by Tjstretch View Post
    You may want to have a look in ArrayList, which can be found here:

    ArrayList (Java Platform SE 6)
    How can I be spoon feeding? It is my thread...I am just asking for help, no one has to give me any advice or help at all.

    I don't want it done for me. Just need help sometimes.
    Last edited by CSUTD; November 1st, 2011 at 06:22 PM.

  10. #10
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Cumulative sum of entries

    Yeah I thought you were spoon feeding because you posted a useless amount of code, I edited my post when I looked at the thread owner.

    As a side rant, I really hate it when people say "I can't use that.. I'm just a beginner" because... well that seems counter-intuitive. Is your teacher actually going to give you points off or do you think that there must be some simpler way to do this? If you really can't do arrays, than you can try to append a String like Kevin already said.

    To append Strings you can use += (Compiler will automatically convert it to a StringBuilder, most likely)
    EG:
    String result = "";
    int sum = 3;
    result += sum+" ";
    sum += 2;
    result += sum + " ";
    etc.

Similar Threads

  1. PhoneBook entries with ArrayList problem
    By gpelefty9 in forum Collections and Generics
    Replies: 3
    Last Post: October 30th, 2011, 03:32 AM
  2. [SOLVED] Cumulative array
    By spaggwoo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 23rd, 2011, 09:54 AM
  3. HELP!!! Entry Class to represent entries in a telephone directory
    By Princess D in forum Java Theory & Questions
    Replies: 10
    Last Post: January 22nd, 2010, 05:39 AM