Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:45:27+00:00 2026-06-12T17:45:27+00:00

I am writing a simple program to calculate the average of a set of

  • 0

I am writing a simple program to calculate the average of a set of numbers. You get the numbers using Scanner, so I am using while loop with .hasNext() as a condition. However the loop is infinite.

Is it possible to break out of it without writing some certain word like “stop” in the input?

public class main {

    public static void main(String[] args){

        Scanner Input = new Scanner(System.in);
        int count = 0;
        int temp;
        int sum = 0;

        while(Input.hasNextInt()){

           count++;           

           temp = Input.nextInt();
           sum += temp;
           System.out.println(temp);
           System.out.println(count);           

        } // End of while

        double average = sum / count;
        System.out.println("The average is: " + average);

    } // End of method main

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:45:28+00:00Added an answer on June 12, 2026 at 5:45 pm

    The break; statement can be sued to… well… break out of an iteration. And by iteration I mean you can get out of a for too, for example.

    You have to define WHEN do you want to break out of the iteration and then do something like this:

    while(Input.hasNextInt(Input)){
       if(condition())
           break;
    
       count++;           
    
       temp = Input.nextInt();
       sum += temp;
       System.out.println(temp);
       System.out.println(count);           
    
     }
    

    Otherwise, you can make an auxiliary method that defines if the iteration should keep on going, like this one:

    private boolean keepIterating(Scanner in) {
        boolean someOtherCondition = //define your value here that must evaluate to false
                                     //when you want to stop looping
        return Input.hasNextInt() && someOtherCondition;
    }
    

    Method that you will have to invoke in your while:

    while(keepIterating()){
    
       count++;           
    
       temp = Input.nextInt();
       sum += temp;
       System.out.println(temp);
       System.out.println(count);           
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I was writing this simple program to calculate the day of any date
I am writing simple parallel program in C++ using OpenMP. I am working on
I'm writing a simple program in C which opens an internet browser using the
I'm writing a simple program that reads and processes file content using a BufferedReader
I'm writing a simple program in C++ to calculate the tax for a purchase
I'm writing a simple program using SDL, but I'm running into a strange problem.
I'm trying to learn C by writing a simple program to output Fibonacci numbers.
I am writing simple client-server program. Client send some messages to server using UDP
I'm writing a simple painting program using Java, and I want some method to
I'm writing a program that calculates input a user gives, simple numbers. 2, 4,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.