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

Thread: Issue with while loops...beginner question

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Issue with while loops...beginner question

    This problem is inputting 10 numbers, and printing th My solution is working except it runs forever when I input anything other than an int. Can someon tell me whatI've done wrong?



    package com.company;
     
    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
     
            int count = 1;
            int sum = 0;
            while (count<=10) {
                System.out.println("Enter number #"+count);
                boolean hasNextInt = scanner.hasNextInt();
     
                if (hasNextInt){
                    int number = scanner.nextInt();
                    count++;
                    sum +=number;
     
                }
     
                else {
                    System.out.println("Invalid Input");
                    continue;
                }
                scanner.nextLine();
     
     
            }System.out.println("the sum of these numbers is "+sum);
            scanner.close();
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Issue with while loops...beginner question

    it runs forever
    Look at the condition that will end the loop. When is the value of count changed?

    If the Scanner has an invalid value (say hasNext... is false), it will keep that value until some next method reads and clears it.
    Be sure the code reads and rejects any invalid value before trying to read and process the next user input.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issue with while loops...beginner question

    Quote Originally Posted by Norm View Post
    Look at the condition that will end the loop. When is the value of count changed?

    If the Scanner has an invalid value (say hasNext... is false), it will keep that value until some next method reads and clears it.
    Be sure the code reads and rejects any invalid value before trying to read and process the next user input.
    well I thought about it and I think removing the "continue;" fixes the issue

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Issue with while loops...beginner question

    Yes, that should work. Except for the case that the user has entered more than one number on a line before hitting Enter. But the current code already has that problem so there wouldn't be any change for the user.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Very Beginner Questions. Loops. 2nd post
    By davidanthony129 in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2014, 07:13 PM
  2. Beginner question about for loops...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 03:57 PM
  3. ArrayIndexOutOfBoundsException issue (Beginner?)
    By cziiki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 15th, 2012, 07:12 AM
  4. please help with if-else statement, loops and beginner stuff
    By javabeg123 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2011, 08:38 PM
  5. Issue with beginner program
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:55 AM