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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:30:15+00:00 2026-05-25T12:30:15+00:00

I am looking at the example in How to Program in Java, 7e .

  • 0

I am looking at the example in How to Program in Java, 7e.
User inputs the data manually into object of class AccountRecord record

AccountRecord record = new AccountRecord();
Scanner input = new Scanner( System.in );
while ( input.hasNext() ) // loop until end-of-file indicator
      {
         try // output values to file
         {
            // retrieve data to be output
            record.setAccount( input.nextInt() ); // read account number
            record.setFirstName( input.next() ); // read first name
            record.setLastName( input.next() ); // read last name
            record.setBalance( input.nextDouble() ); // read balance

         .............................................................

         catch ( NoSuchElementException elementException )
         {
            System.err.println( "Invalid input. Please try again." );
            input.nextLine(); // discard input so user can try again
         } // end catch
      }

I have hard time figuring out how catch ( NoSuchElementException elementException ) works. According to Java Documentation, NoSuchElementException is

Thrown by the nextElement method of an Enumeration to indicate that
there are no more elements in the enumeration.

So, why it would also throw an exception in case type mismatch between expected and what is actually entered, such as for record.setAccount(input.nextInt()), user inputs some text string?

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-05-25T12:30:16+00:00Added an answer on May 25, 2026 at 12:30 pm

    For type mismatch problems, you should catch InputMismatchException. Since it inherits from NoSuchElementException, you will catch it by catching a NoSuchElementException (so the code as it is will catch it and work as expected). To me, that’s a strange inheritance relationship, though…. Certainly does not represent an is-a relationship.

    If you really want to differentiate both cases, catch an InputMismatchException before a NoSuchElementException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example 1 /** *Program Name: Cis36L0411.java *Discussion: Class -- Data Members ONLY * Method
Looking for an example of storing session data in a database, using the Zend
I'm looking to write a short program (maybe a Hello World) in Java bytecode.
I looking for example of program, that modifies a string inside its exe. I
I am looking for a way to tell in a Java program if a
I am looking for example where things in python would be easier to program
I'm looking to write a java program that has the following requirements: An ability
I'm looking for example code for setting up a connection to a bluetooth device
I'm looking for example of how I would solve the scenario below: Imagine my
I'm trying to learn Rails better by looking at example applications, and while looking

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.