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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:00:05+00:00 2026-05-20T08:00:05+00:00

I’m a new Java programmer, and I’ve been trying to setup a simple Swing

  • 0

I’m a new Java programmer, and I’ve been trying to setup a simple Swing program that allows a user to input two integers into JTextFields and when an “add” JButton is clicked the two numbers are added together and the sum is output in a JLabel.

This I can handle, however, where I’m running into difficulty is being able to setup the two JTextFields to consider invalid numbers as zero. For instance, if the character(s) “x” or “xxx” were entered this would cause the value of that JTextField to be 0 for the purposes of the addition. So far I have used try/catch and if/else statements without any success. I have also scoured the Internet for hours over the past two days looking for some solutions for this problem with no results.

Any help would be greatly appreciated! The code I have so far is listed below…

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Adder extends JPanel implements ActionListener {

   JButton addBut = new JButton("Add");
   JTextField field1 = new JTextField("Number 1"),
   field2 = new JTextField("Number 2");
   JLabel numSum = new JLabel("=");

       Adder() {
             this.setPreferredSize(new Dimension(299, 43));
             addBut.addActionListener(this);
             setLayout(new FlowLayout());
             add(field1);
             add(field2);
             add(numSum);
             add(addBut);            
       }

        public void actionPerformed(ActionEvent ae) {
           if(ae.getSource().equals(addBut)){

             try {
                double one = Double.parseDouble(field1.getText());
                double two = Double.parseDouble(field2.getText());
                numSum.setText("" + (one + two));
             }           

                catch(Exception ex) {
             }
        } 

                else {
                    field1.setText(" ");
                    field2.setText(" ");
                    numSum.setText("");
             }
        }

                public static void main(String[] args){
                   JFrame jf=new JFrame();
                   jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   Adder x = new Adder();
                   jf.add(x);
                   jf.pack();
                   jf.setVisible(true);
              }
        }
  • 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-20T08:00:06+00:00Added an answer on May 20, 2026 at 8:00 am

    Try

    double one = 0.0;
    double two = 0.0;
     try {
       one = Double.parseDouble(field1.getText());
     }
     catch(Exception ex) {
        field1.setText("0");
     }
    
     try {
        two = Double.parseDouble(field2.getText());
     }
     catch(Exception ex) {
       field2.setText("0");
     }
    
    numSum.setText("" + (one + two));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm making a simple page using Google Maps API 3. My first. One marker

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.