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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:15:39+00:00 2026-05-16T03:15:39+00:00

I have a calculation application which I need to validate the fields to check

  • 0

I have a calculation application which I need to validate the fields to check if the values entered are numeric numbers and not alphanumeric. I have some ideas about the codes.
Please guide me if I have done anything wrong or seem noob as this is my first time trying out Swing.

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
    String text1 = jTextField1.getText();            // TODO add your handling code here:
}

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
    String text2 = jTextField2.getText();        // TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    if (text1 and text2 != <numeric number>){
        JOptionPane.showConfirmDialog(null, "Please enter numbers only", "naughty", JOptionPane.CANCEL_OPTION);
    }
    // First we define float variables.
    float num1, num2, result;
    // We have to parse the text to a type float.
    num1 = Float.parseFloat(jTextField1.getText());
    num2 = Float.parseFloat(jTextField2.getText());
    // Now we can perform the addition.
    result = num1+num2;
    // We will now pass the value of result to jTextField3.
    // At the same time, we are going to
    // change the value of result from a float to a string.
    jTextField3.setText(String.valueOf(result));
    // TODO add your handling code here:
}

Please do help. By the way why does my NetBeans keep informing me that it does not recognize the “JOptionPane” Command?

  • 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-16T03:15:40+00:00Added an answer on May 16, 2026 at 3:15 am

    Float.parseFloat() will throw a NumberFormatException if the String isn’t numeric and cannot be parsed into a Float. You can add a try-catch block to check for this condition:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        float num1, num2, result;
    
        try {
            num1 = Float.parseFloat(jTextField1.getText());
            num2 = Float.parseFloat(jTextField2.getText());
            result = num1+num2;
            jTextField3.setText(String.valueOf(result));
    
        } catch (NumberFormatException e) {
            JOptionPane.showConfirmDialog(null, "Please enter numbers only", "naughty", JOptionPane.CANCEL_OPTION);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller that runs some calculation on data, and then I need
I have a calculator application in java swing, which is working properly with mouse
I have a SQL data calculation which is used as part of where clause
I have a stored procedure which update a table based on such calculation and
I have some idea that it is due to some complex calculation, but i
I have a VB6 application (please don't laugh) which does a lot of drawing
I'm trying to solve a classic problem - I have a multi-threaded application which
I've inherited a Delphi application which I need to convert to a C# application
I have an application that stores data in database. I need search functionality to
I have a web application running on Tomcat. There are several calculations that need

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.