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

Thread: While Loop help

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default While Loop help

    I'm trying to write a code using while loop that prompts user to enter any 5 integer numbers. I want the output to display those 5 numbers I entered and as well as the sum of the numbers entered.

    My question is: Am I using the while loop correctly or there is any simplified method doing it??

    My code:
     
    import java.util.Scanner;
     
    public class readinput {
     
    public static void main(String[] args){
     
           int count = 0;
            Scanner input = new Scanner(System.in);
     
                    System.out.println("Enter 1st number");
                int number1 = input.nextInt();
                count=count+1;
                    System.out.println("Enter 2nd number");
                int number2 = input.nextInt();
                count=count+1;
                    System.out.println("Enter 3rd number");
                int number3 = input.nextInt();
                count=count+1;
                    System.out.println("Enter 4th number");
                int number4 = input.nextInt();
                count=count+1;
                    System.out.println("Enter 5th number");
                int number5= input.nextInt();
                count=count+1;
     
            while (count==5) {       
     
            System.out.println("number1: "+number1);
            System.out.println("number2: "+number2);
            System.out.println("number3: "+number3);
            System.out.println("number4: "+number4);
            System.out.println("number5: "+number5);
            System.out.println("Sum of all the numbers: "+(number1+number2+number3+number4+number5)); break;    }  
        }
    }

    Example my Input are these following number: 2, 6, 1, 3, 7
    Output:
    number1: 2
    number2: 6
    number3: 1
    number4: 3
    number5: 7
    Sum of all the numbers: 19


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: While Loop help

    Welcome! Excellent first post!

    I take it you don't know arrays yet? Using an array to store the user's input would dress up the loop and program a lot. Let us know if you can use arrays.

    Your use of the while() loop is correct but completely unnecessary. Does the assignment require the use of a while() loop?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    skypi205 (November 2nd, 2013)

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: While Loop help

    Thanks for your reply, GregBrannon.

    I have not learn about the arrays yet and I have to implement using the logic while() loop.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: While Loop help

    You can use a while() loop to collect 5 numbers and calculate and print the sum using one variable. But if you want to store those 5 numbers in separate variables for later retrieval without the use of an array, then a while loop is completely unnecessary.

    What does the assignment require? This may be a time you shouldn't add requirements to impress the instructor.

  6. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: While Loop help

    It is just an exercise and the question is stated like this: Write a program that prompts user to enter 5 integer numbers and displays the numbers entered and calculate the sum of the number entered. You can assume that the user will always key in numbers and not texts. Implement the logic using while loop.

    the output should go like this:
    number1: 2
    number2: 6
    number3: 1
    number4: 3
    number5: 7
    Sum of all the numbers: 19

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

    Default Re: While Loop help

    while user has not entered five numbers {
        prompt for input
        add input to sum
        increment counter
    }
    display result
    You only need three variables: current input, sum, counter.
    Improving the world one idiot at a time!

  8. #7
    Junior Member JApache's Avatar
    Join Date
    Nov 2013
    Posts
    5
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: While Loop help

    Hi Skypi205,
    Just a pointer to something that i noticed in your code
    while (count==5) {

    You have used an equality operator inbetween your parenthesis , in the while loop
    There is nothing wrong with the way you have done it , i would like to point out though that
    the ASCII value of '5' is being evaluated against the actual string of 'COUNT'
    since the String has 5 characters C,O,U,N,T this will evaluate to true in the while loop
    Usually the assignment operator is used for this '=' to evaluate the value of the variable COUNT

  9. #8
    Forum VIP
    Join Date
    Jun 2011
    Posts
    232
    My Mood
    Stressed
    Thanks
    36
    Thanked 66 Times in 54 Posts
    Blog Entries
    4

    Default Re: While Loop help

    Quote Originally Posted by JApache View Post
    Hi Skypi205,
    Just a pointer to something that i noticed in your code
    while (count==5) {

    You have used an equality operator inbetween your parenthesis , in the while loop
    There is nothing wrong with the way you have done it , i would like to point out though that
    the ASCII value of '5' is being evaluated against the actual string of 'COUNT'
    since the String has 5 characters C,O,U,N,T this will evaluate to true in the while loop
    Usually the assignment operator is used for this '=' to evaluate the value of the variable COUNT
    That is incorrect. Skipi205 is correctly testing the integer variable 'count' against the number 5 with the equality operator "==". You don't use the assignment operator "=" to test for equality.

  10. #9
    Junior Member JApache's Avatar
    Join Date
    Nov 2013
    Posts
    5
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: While Loop help

    Quote Originally Posted by ChristopherLowe View Post
    That is incorrect. Skipi205 is correctly testing the integer variable 'count' against the number 5 with the equality operator "==". You don't use the assignment operator "=" to test for equality.
    Christopher , as i mentioned in my post to Skipi205 "There is nothing wrong with the way you have done it ", either way the expression evaluates to true, i was just pointing out that there is more then one way of doing the same thing and the differences .But i get what you are saying == tests for Equality = is an assignment operator

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

    Default Re: While Loop help

    Your first post was full of incorrect information.
    Quote Originally Posted by JApache View Post
    i would like to point out though that the ASCII value of '5' is being evaluated against the actual string of 'COUNT'
    NO! Count is the name of the variable and its type is an int. There is no String involved.
    since the String has 5 characters C,O,U,N,T this will evaluate to true in the while loop
    NO! Conditional statements do not compare the length (number of chars) of a String to an int. Unless you specifically call the length method.
    Usually the assignment operator is used for this '=' to evaluate the value of the variable COUNT
    NO! The assignment operator is never used to compare values.
    Improving the world one idiot at a time!

  12. #11
    Junior Member JApache's Avatar
    Join Date
    Nov 2013
    Posts
    5
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: While Loop help

    Quote Originally Posted by Junky View Post
    Your first post was full of incorrect information.

    NO! Count is the name of the variable and its type is an int. There is no String involved.

    NO! Conditional statements do not compare the length (number of chars) of a String to an int. Unless you specifically call the length method.

    NO! The assignment operator is never used to compare values.
    Humble Apologies you are correct i misread the code ,

Similar Threads

  1. help with when the for loop is met and i want to run the while loop again
    By m49er704 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 22nd, 2013, 09:03 AM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 10:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM