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

Thread: Java Help...

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Help...

    I need to create a method which totals a series of positive integers which the user enters from the keyboard. The user will type in each value followed by an <Enter> keypress. After the last value the user will type a negative value: this is the terminal sentinel.

    So far, the code which I have in the class Looping:
     public static void main(String[] args){
              int count =0;
              int sum= 1;
              do
              { 
                  System.out.println(+ count + sum);      
                  count++;
                  sum += count;
                }while(count <=4);
            }
        }

    import java.util.Scanner;
     
     
    /**
     * InputReader reads typed text input from the standard text terminal. 
     
     */
    public class InputReader
    {
        private Scanner reader;
     
        /**
         * Create a new InputReader that reads text from the text terminal.
         */
        public InputReader()
        {
            reader = new Scanner(System.in);
        }
     
        /**
         * Accesses a String typed in text terminal
         * 
         * @returns String value input
         */
        public String getString()
        {
            String input = reader.nextLine();
            return input;
        }
     
        /**
         * Accesses a int typed on a single line in text terminal
         * 
         * @returns int value input
         */
        public int getInt()
        {
            int input = reader.nextInt();
            reader.nextLine();
            return input;
        }
     
    }
    Last edited by xs4rdx; February 18th, 2010 at 06:20 AM.


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Java Help...

    You're really almost there, aren't you? I don't understand what the problem is. You have all the code written... now you just have to put it together.

    ReadAndSum()
    1. sum := 0
    2. input := 0
    3. while input >= 0
    4.    do Display("The nth partial sum is ", sum) ! this is optional
    5.    sum := sum + input
    6.    input := ReadInteger()
    7. Display("The sum is ", sum)

    The translation from pseudocode is pretty straightforward... you already know how to Display(...) and ReadInteger().

    One thing: you need to be careful about how you're printing out the count and sum. Try this...

    System.out.println("(count, sum) =  (" + count + ", " + sum+ ")");
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

  3. The Following User Says Thank You to CT&SC For This Useful Post:

    xs4rdx (February 21st, 2010)