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: Writing a while loop to read positive integers from the user until the user enters the EOF character.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing a while loop to read positive integers from the user until the user enters the EOF character.

    import java.util.Scanner;
    public class Project_5
    {
      public static void main (String[] args)
      {
        Scanner input= new Scanner (System.in);
     
        int inputNum, sum = 0;
        int numValid = 0, numInvalid = 0;
        double average;
     
     
        while (true){
        System.out.println ("Enter your values, pressing enter in between each one.  When finished press ctrl+d");
             if (input.hasNext());
        inputNum = input.nextInt();
     
        if (inputNum > 0){
          numValid = input.nextInt();}
     
        else if (inputNum < 0){
          numInvalid = input.nextInt();
          System.out.println ("The number " + inputNum + " is invalid.");}
     
     
          System.out.println ("There were " + numValid + " valid numbers entered." +
                              "\nThe sum of the valid numbers was --- and the average was ---." +
                              "\n There were " + numInvalid + " invalid numbers entered.");

    So I'm attempting to have this program take the users input of integers, pressing enter in between each one. If they enter a negative number it lets them know that its invalid. At the end of the program it takes all of the valid integers entered and must add them and average them. I've put the final println in there as a placeholder. I'm very lost with my output. If the user types in "6" then presses enter and types in "3" , the output is:

    There were 3 valid numbers entered.
    The sum of the valid numbers was --- and the average was ---.
    There were 0 invalid numbers entered.

    It then continues on allowing the user to enter more values. I've tried editing my brackets but that hasn't given me any results.
    Here is the example output for correct code"

    Enter a positive value (EOF to quit): 4
    Enter a positive value (EOF to quit): 7
    Enter a positive value (EOF to quit): 8
    Enter a positive value (EOF to quit): 2
    Enter a positive value (EOF to quit): -1
    The number "-1" is invalid.

    Enter a positive value (EOF to quit): 8
    Enter a positive value (EOF to quit): 0
    Enter a positive value (EOF to quit): -4
    The number "-4" is invalid.

    Enter a positive value (EOF to quit): CTRL-D

    There were 6 valid numbers entered.
    The sum of the valid numbers was 29 and the average was 4.83.
    There were 2 invalid numbers.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,073
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Writing a while loop to read positive integers from the user until the user enters the EOF character.

    You have a test case that fails (6 enter, 3 enter). I recommend you trace your code with your biologic computer to investigate - and hopefully discover - why that test case fails.

Similar Threads

  1. How to replace the vowels with a user input Character
    By gdoggson in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 4th, 2013, 04:53 AM
  2. Trying to validate a character user input in an array ?
    By lizzy2 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2013, 03:04 PM
  3. Replies: 1
    Last Post: May 16th, 2012, 05:15 PM
  4. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  5. How to Read user input from the console with InputStreamReader in Java?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:48 AM

Tags for this Thread