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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:15:28+00:00 2026-06-13T08:15:28+00:00

I trying to understand why the method validateItemquantity eventually crashes with an NumberFormatException error.

  • 0

I trying to understand why the method validateItemquantity eventually crashes with an NumberFormatException error. The logic seem correct, but something is not right. Its suppose to keep asking the user to reenter until it gets an valid input and then return that valid input.

String validateItemquantity(String itemQuantity) {
       try{ 
           Integer.parseInt(itemQuantity);

          }
       catch (NumberFormatException e) {
           itemQuantity = JOptionPane.showInputDialog 
                                   ("Invalid item quantiy, please enter a new Value");
           validateItemquantity(itemQuantity);
          }
       return itemQuantity ; 

Method that calls it:

 private void bnPurchaseActionPerformed(java.awt.event.ActionEvent evt) {

    String itemCode, validItemquantity ; 
    int itemQuantity, itemPrice, itemCost, totalCost ;

    validItemquantity =  validateItemquantity(itemQuantityinput.getText());
    itemQuantity = Integer.parseInt(validItemquantity);
    itemCode = itemCodeinput.getText();
    itemPrice = catalog.searchCatalog(itemCode);
    itemCost = payment.calculateItemcost(itemQuantity,itemPrice);
    totalCost = payment.calculateTotalcost(itemCost);
  • 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-13T08:15:29+00:00Added an answer on June 13, 2026 at 8:15 am

    Have it return the result of the parse or call itself on exception, like this:

    String validateItemQuantity(String itemQuantity) {
       try { 
           return Integer.parseInt(itemQuantity); // returns if no exception
       } catch (NumberFormatException e) {
           return validateItemQuantity(JOptionPane.showInputDialog 
                                   ("Invalid item quantiy, please enter a new Value"));
       }
    }
    

    This method will only return a valid response; it loops forever until it gets a valid response.

    A few comments have mentioned that the above code may be attacked by the user entering bad data millions of times and blowing the stack. I say “let them”, but if you really want to make it safe, use a while loop:

    String validateItemQuantity(String itemQuantity) {
        while (true) {
            try { 
                return Integer.parseInt(itemQuantity); // returns if no exception
            } catch (NumberFormatException e) {
               itemQuantity = JOptionPane.showInputDialog(
                   "Invalid item quantiy, please enter a new Value"));
            }
        }
    }
    

    It adds a while loop, so the code complexity increases slightly, but it’s only couple of extra lines of code and it is more efficient and safe.

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

Sidebar

Related Questions

Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
I'm trying to understand the equals() method better. All examples I've seen do something
I am trying to understand the meaning of that something before the method name
I'm trying to understand how getListCellRendererComponent method works but I don't get it. I
I am trying to understand how ThreadPool.QueueWorkItem method works. But couldn't understand how it
I'm trying to understand a method using the disassembly feature of Reflector. As anyone
Trying to understand what's the correct way of implementing OpenID authentication with Spring Security.
Trying to understand something. I created a d:\svn\repository on my server. I committed folders
Howdy, I am trying to understand this method signature: public <K> Map<K, String> getMulti(Serializer<K>
I'm quite lost in trying to understand why a method doesn't get invoked. I

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.