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

Thread: Beginner Question--Sum of numbers program; Validation

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

    Default Beginner Question--Sum of numbers program; Validation

    Hi,

    I'm just starting to learn Java, and I'm trying to write a program to sum integers from 1 to the entered number. The integer entered has to be greater than 0, and I need to use a while statement to validate the input. I also need to use a scanner class for keyboard input.

    I have the program able to recognize a negative or zero number, but I cannot figure out how to both sum the numbers and watch the input to make sure it's not negative. Can someone point me in the right direction? I've been working on this thing for more than 6 hours already..

    This is my code so far:

    import java.util.Scanner;
     
    public class SumOfNumbers
    {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a positive nonzero number: ");
            int x = Integer.parseInt(input.nextLine());
            int y = 0;
            int sum = 0;
     
            while (y <= x) {
                sum = sum + y;
                y++;
     
            System.out.println("Sum of all the integers from 1 through " + x + " is: " + sum);
            }
     
            if (y > x)
                System.out.println("Invalid. Please enter a positive nonzero number.");
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,469
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: Beginner Question--Sum of numbers program; Validation

    Something like (change to fit your variables):
    while ( numberEntered < 0 )
    {
        System.out.print( "Enter a non-negative integer!"   );
        numberEntered = Integer.parseInt(input.nextLine());
        System.out.println();
    }

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Beginner Question--Sum of numbers program; Validation

    I think you are misunderstanding the requirements. You need to validate user input before you can perform the sum. So you will need 2 loops, one for each task.
    Improving the world one idiot at a time!

Similar Threads

  1. Input Validation - Take no Symbols or Numbers
    By ProgrammablePeter in forum Java Theory & Questions
    Replies: 4
    Last Post: December 29th, 2013, 03:01 AM
  2. stuck trying to count sum of even and odd numbers for reading loop.
    By gio in forum Loops & Control Statements
    Replies: 2
    Last Post: February 21st, 2013, 10:23 AM
  3. Quick Recursion question involving sum
    By ManInTheMiddle in forum Algorithms & Recursion
    Replies: 6
    Last Post: December 1st, 2012, 07:19 AM
  4. Question about for loop to print the sum of series of fractions
    By _K_ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 07:08 AM
  5. sum of arrays and printing even and odd numbers in the array
    By senecawolf in forum Collections and Generics
    Replies: 3
    Last Post: November 8th, 2011, 02:07 PM