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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:17:57+00:00 2026-06-03T01:17:57+00:00

I am working on writing a file reader, and the idea is to have

  • 0

I am working on writing a file reader, and the idea is to have the user enter a number that represents the line number from the text file. The variable that holds this number is of type int. However, when the user enters a String instead, Java throws the InputMismatchException exception, and what I want is to have a loop in the catch clause, where I will be looping until the user enters a valid value, i.e. an int. The skeleton looks like this:

 public void _____ throws IOException {
    try {
    // Prompting user for line number
    // Getting number from keyboard
    // Do something with number
    } catch (InputMismatchException e) {
       // I want to loop until the user enters a valid input
       // When the above step is achieved, I am invoking another method here
    }  
}

My question is, what are some possible techniques that could do the validation?
Thank you.

  • 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-03T01:17:59+00:00Added an answer on June 3, 2026 at 1:17 am

    Avoid using exceptions for flow control. Catch the exception, but only print a message. Also, do need for loops within loops.

    It’s as simple as this:

    public void _____ throws IOException {
        int number = -1;
        while (number == -1) {
            try {
                // Prompt user for line number
                // Getting number from keyboard, which could throw an exception
                number = <get from input>;
            } catch (InputMismatchException e) {
                 System.out.println("That is not a number!");
            }  
        }
        // Do something with number
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam working on a project which involves writing a Mork File (Mork is a
I am working on writing the data into Excel sheet in c#. I have
Imagine that I have a C# application that edits text files. The technique employed
I am working on a personal project that uses a custom config file. The
I have an app that is available from the Android Market. Some users have
Working on writing a custom property Handler for our custom file type in windows
I'm writing a Java program that has a working drag and drop GUI for
These day's I'm working into a New's Website ,and the back-end I'm writing from
I'm working on writing a kernel, and I have a few friends working with
I need some help in writing a batch file. I have a path stored

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.