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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:09:15+00:00 2026-05-31T00:09:15+00:00

I am having a lot of trouble with this code. The code compiled and

  • 0

I am having a lot of trouble with this code. The code compiled and ran as it was suppose to before I tried to put in the code to catch an exception. Then I could no longer get it to compile. It is suppose to catch an exception and produce an error message if the user inputs a negative number.

import java.awt.event.ActionEvent; //Next group of lines import various Java classes
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.text.*;
import java.io.IOException;

public class SquareRoot extends JFrame
{
    public static void main(String[] args)  {
        //Creates Window
        JFrame frame = new JFrame();
        frame.setSize(450, 300);
        frame.setTitle("Find the Square Root");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel Numberlbl = new JLabel("Enter a number:");
        final JTextField NumberField = new JTextField(10);
        NumberField.setText("");

        JLabel Answerlbl = new JLabel("Square Root of your number is:");
        final JTextField AnswerField = new JTextField(10);
        AnswerField.setText("");

        JLabel ButtonLabel = new JLabel("Calculate Square Root");
        JButton button = new JButton("Find Square Root");

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3,2));
        panel.add(Numberlbl);
        panel.add(NumberField);
        panel.add(ButtonLabel);
        panel.add(button);
        panel.add(Answerlbl);
        panel.add(AnswerField);
        frame.add(panel);

        class CalculateListener implements ActionListener {

            public void actionPerformed(ActionEvent event) {

                double NumberX = Double.parseDouble(NumberField.getText());
                try
                {
                    if(NumberX >=0);
                }
                catch(IOException e)
                {
                    System.out.println("Number can not be negative.");
                }
                double Answer = Math.sqrt(NumberX);
                AnswerField.setText(String.valueOf(Answer));

            }
        }

        ActionListener listener = new CalculateListener();
        button.addActionListener(listener);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.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-31T00:09:17+00:00Added an answer on May 31, 2026 at 12:09 am

    You do not need try-catch to check negative, below should be enough. But you may need try-catch when you do formatting.

    double noX = 0;
    try {
       noX = Double.parseDouble(NumberField.getText());
    } catch(NumberFormatException e) {
       System.out.println("Not a valid number");
    }
    if(noX < 0) {
       System.out.println("Number can not be negative.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, i'm having a lot of trouble with this little piece of code. An
I am having a lot of trouble with this piece of code (I am
Hey guys I am having a lot of trouble trying to understand this and
I am having a lot of trouble with this basic recursion problem in java;
I'm having a lot of trouble implementing this deque using a circular array; in
I am having a lot of trouble with 'base types' in the Code Only
Having a lot of trouble getting texture maps to work in openGL ES (iphone).
I am having a lot of trouble finding good information on how to call
I'm having a lot of trouble getting my priority queue to recognize which parameter
I'm having a lot of trouble trying to define a mock for a rails

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.