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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:20:20+00:00 2026-06-13T05:20:20+00:00

I have to validate my user input for an integer value and prompt the

  • 0

I have to validate my user input for an integer value and prompt the user to reenter if an invalid value is entered. Thus far I have put together this method below. i believe the logic is correct but I need to get the user to reenter input again after the error message.

void validateItemquantity() {
    boolean error = true;
    while (error) {
        try{ 
            Integer.parseInt(itemQuantityinput.getText());
            error = false; 
        }
        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Item quantity must be a positive number, please reenter");
            error = true; 
        }
    }
}

Part of code from jframe method:

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

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

        validateItemquantity();
        itemCode = itemCodeinput.getText();
        itemQuantity = Integer.parseInt(itemQuantityinput.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-13T05:20:22+00:00Added an answer on June 13, 2026 at 5:20 am

    I think the loop should be in the calling method.

    boolean validateItemquantity() {
        try{ 
            Integer.parseInt(itemQuantityinput.getText());
            return true; 
        }
        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Item quantity must be a positive number, please reenter");
            error = false; 
        }
    }
    

    And the calling part should be something like that

    while(!validateItemquantity())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to validate user input data and ensure a string value is convertible
I have to validate user-input from stdin that is not going to be entered
I have loop designed to validate the user input on a question, it was
In my code, I have two text boxes that validate input as the user
I have a Datagridview, on which I need to validate the user input on
I have form who validate user input, I use PHP, Javascript and AJAX. I
How can I validate the user input by using scanf. Right now I have
I need to validate some user input that is encoded in UTF-8. Many have
I have an Ext.grid.Panel and i want to validate user input: there is a
i have a regular expression on ASP.net textbox control that validate for user input

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.