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

  • Home
  • SEARCH
  • 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 8951149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:35:43+00:00 2026-06-15T13:35:43+00:00

So this is my code for a crash proof scanner class: import java.util.*; public

  • 0

So this is my code for a crash proof scanner class:

import java.util.*;
public class BPScanner {
    Scanner kb = new Scanner(System.in);
    public int nextInt() {
        while (true) {
            try {
                String input = kb.nextLine();
                int i = Integer.parseInt(input);
                return i;
        }
            catch (NumberFormatException e1) {}
            catch (NoSuchElementException e2) {}
            System.out.print("\nPlease input an integer: ");
            kb.close();
            kb = new Scanner(System.in);
        }
    }    
}

I am calling this class from another class:

public void scnr() {
    while (true){
        System.out.print("Type a num (for test), (0 to break)");
        int n = bpkb.nextInt();
        if (n == 0) break;
        System.out.println(n);
    }
}

When I run it it returns an infinite loop that keep saying:

Please input an integer: 
Please input an integer: 
Please input an integer: 
Please input an integer: 

Any ideas how to fix it?
Thank you very much in advance.

  • 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-15T13:35:45+00:00Added an answer on June 15, 2026 at 1:35 pm

    Just remove the following codes from nextInt function:

    kb.close();
    kb = new Scanner(System.in);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this code crash? CustomDatabaseHelper.java: public class CustomDatabaseHelper { SQLiteDatabase db; private static
Why does this code crash the Scala 2.8.1 compiler? val a = new Array[{
why does this code crash? is using strcat illegal on character pointers? #include <stdio.h>
The Problem I'm receiving crash reports from users that look like this: Code Type:
This code causes my app to crash on an iPhone 4 and on the
I am getting this nasty crash and error. The crash occurs when this code
Following code will compile but crash at run time: int main() { try {
Why does this code crash the program when I run it ostream& operator<<(ostream& cout,
When trying to reallocate memory I crash when using this code: //value of i
When I run this code to load a jpeg file I get a crash

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.