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 8665529
In Process

The Archive Base Latest Questions

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

private static int posNum() { Scanner scan = new Scanner(System.in); int input = 0;

  • 0
  private static int posNum() {
            Scanner scan = new Scanner(System.in);
            int input = 0;
            boolean error;

            if (scan.hasNextInt()) {
                input = scan.nextInt();
                error = input <= 0;
            } else {
    28          scan.next();
                error = true;
            }
            while (error) {
                System.out.print("Invalid input. Please reenter: ");
                if (scan.hasNextInt()) {
                    input = scan.nextInt();
                    error = input <= 0;
                } else {
                    scan.next();
                    error = true;
                }
            }
            scan.close();
            return input;

        }

So the second time I call this method its returning the following error.

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at q2.CylinderStats.posNum(CylinderStats.java:28)
    at q2.CylinderStats.main(CylinderStats.java:62)

The first call is rad = posNum(); which runs fine and then secondis height = posNum(); which doesn’t allow a value to be entered before it goes to the error.

Thanks

  • 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:30:06+00:00Added an answer on June 12, 2026 at 5:30 pm

    while calling next you should check if scanner has one.

     if(scan.hasNext())
     scan.next();
    

    According to java doc of Scanner#next

    NoSuchElementException if no more tokens are available

    You can change your method like below

    private static int posNum(Scanner scan) {
        int input = 0;
        boolean error = false;
        if (scan.hasNext()) {
            if (scan.hasNextInt()) {
                input = scan.nextInt();
                error = input <= 0;
            } else {
                scan.next();
                error = true;
            }
        }
        while (error) {
            System.out.print("Invalid input. Please reenter: ");
            if (scan.hasNextInt()) {
                input = scan.nextInt();
                error = input <= 0;
            } else {
                if (scan.hasNext())
                    scan.next();
                error = true;
            }
        }
        return input;
    }
    

    And then call it like below

        Scanner scan = new Scanner(System.in);
        int i = posNum(scan);
        System.out.println(i);
        int j = posNum(scan);
        System.out.println(j);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class StaticTest { private static StaticTest stObj=new StaticTest(); private static int VAR1=10; private
public class StoreMessage extends Thread implements Serializable{ private static long start_nanotime=System.nanoTime(); private static int
import java.util.Scanner; public class Test { private static int decimalNum = 0; private static
public class AthleteManager { private static Icon anIcon = new ImageIcon(); private static int
public partial class _Default : System.Web.UI.Page { private static Stopwatch timer = new Stopwatch();
public class counting { private static int counter = 0; public void boolean counterCheck(){
public class SelfCallingTest { private static int counter; public void SelfCallingMethod(int counter) { Console.WriteLine(The
Is there a way to improve this: private static int CountNewlines(string s) { int
I have this small class looking like this: private static int field1 = -
With this code: public partial class Form1 : Form { private static readonly int

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.