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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:05:49+00:00 2026-06-17T01:05:49+00:00

So, I was looking for an efficient way, using Java’s standard packages, to read

  • 0

So, I was looking for an efficient way, using Java’s standard packages, to read an input integer… For example, I came across the class “Scanner”, but I found two main difficulties:

  1. if I don’t insert an int, I’m not actually able to solve the exception;
  2. this class works with tokens, but my aim is to load the string in its full length.

This is an example of execution I would like to realize:

Integer: eight
Input error - Invalid value for an int.
Reinsert: 8 secondtoken
Input error - Invalid value for an int.
Reinsert: 8
8 + 7 = 15

And this is the (incorrect) code I tried to implement:

import java.util.Scanner;
import java.util.InputMismatchException;

class ReadInt{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        boolean check;
        int i = 0;
        System.out.print("Integer: ");
        do{
            check = true;
            try{
                i = in.nextInt();
            } catch (InputMismatchException e){
                System.err.println("Input error - Invalid value for an int.");
                System.out.print("Reinsert: ");
                check = false;
            }
        } while (!check);
        System.out.print(i + " + 7 = " + (i+7));
    }
}
  • 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-17T01:05:51+00:00Added an answer on June 17, 2026 at 1:05 am

    Use a BufferedReader. Check NumberFormatException. Otherwise very similar to what you have. Like so …

    import java.io.*;
    
    public class ReadInt{
        public static void main(String[] args) throws Exception {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            boolean check;
            int i = 0;
            System.out.print("Integer: ");
            do{
                check = true;
                try{
                    i = Integer.parseInt(in.readLine());
                } catch (NumberFormatException e){
                    System.err.println("Input error - Invalid value for an int.");
                    System.out.print("Reinsert: ");
                    check = false;
                }
            } while (!check);
            System.out.print(i + " + 7 = " + (i+7));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking for the efficient way to track the logged users when using
I'm looking for an efficient way to get the list of unique commit authors
I have a table which contains many records. I'm looking for an efficient way
Looking for suggestions for an efficient way to maintain basic audit fields for entities
I am looking for a more efficient way to do comparisons between all elements
I am looking for a most efficient way to re-use widgets with different selectors...
I'm looking for an elegant and efficient way to represent and store an arbitrary
I am looking for the most efficient way to update a number of rows
I am looking for a decently efficient way to listen for List changes on
I have a bipartite graph and I'm looking for the most efficient iterative way

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.