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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:57:26+00:00 2026-06-05T07:57:26+00:00

I am working on my first Java project implementing a class called HeartRates which

  • 0

I am working on my first Java project implementing a class called “HeartRates” which takes the user’s date of birth and returns their max and target heart rate. Everything in the main test program works except for one thing, I can’t figure out how to stop the rest of the code from printing after the exception is caught.

I am not really sure about the entire portion of code where the exception is caught because it was copied and pasted from what the professor gave us. If anybody can tell me how to terminate the program after an error occurs, or print a custom error message and stop the program from executing further I would appreciate it.

Here is the code:

 import java.util.Scanner;
 import java.util.GregorianCalendar;

 import javax.swing.JOptionPane;

 public class HeartRatesTest {

public static void main(String[] args) {
    HeartRates test= new HeartRates();
    Scanner input = new Scanner( System.in );
    GregorianCalendar gc = new GregorianCalendar();
    gc.setLenient(false);

        JOptionPane.showMessageDialog(null, "Welcome to the Heart Rate Calculator");;
        test.setFirstName(JOptionPane.showInputDialog("Please enter your first name: \n"));
        test.setLastName(JOptionPane.showInputDialog("Please enter your last name: \n"));
        JOptionPane.showMessageDialog(null, "Now enter your date of birth in Month/Day/Year order (hit enter after each): \n");

        try{
            String num1= JOptionPane.showInputDialog("Month: \n");
            int m= Integer.parseInt(num1);
            test.setMonth(m);
                gc.set(GregorianCalendar.MONTH, test.getMonth());
            num1= JOptionPane.showInputDialog("Day: \n");
            m= Integer.parseInt(num1);
            test.setDay(m);
                gc.set(GregorianCalendar.DATE, test.getDay());
            num1= JOptionPane.showInputDialog("Year: \n");
            m= Integer.parseInt(num1);
            test.setYear(m);
                gc.set(GregorianCalendar.YEAR, test.getYear());

                gc.getTime(); // exception thrown here
        }

        catch (Exception e) {
            e.printStackTrace();
            }   



    String message="Information for "+test.getFirstName()+" "+test.getLastName()+": \n\n"+"DOB: "+ test.getMonth()+"/" +test.getDay()+ "/" 
            +test.getYear()+ "\nAge: "+ test.getAge()+"\nMax Heart Rate: "+test.getMaxHR()+" BPM\nTarget Heart Rate(range): "+test.getTargetHRLow()
            +" - "+test.getTargetHRHigh()+" BPM";
    JOptionPane.showMessageDialog(null, message);
}
  • 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-05T07:57:28+00:00Added an answer on June 5, 2026 at 7:57 am

    Not really sure why you want to terminate the application after the exception is caught – wouldn’t it be better to fix whatever went wrong?

    In any case, in your catch block:

    catch(Exception e) {
        e.printStackTrace(); //if you want it.
        //You could always just System.out.println("Exception occurred.");
        //Though the above is rather unspecific.
        System.exit(1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on my first homework project in a web programming class, which is
I have been working on my first Java project with the 'Drools Planner' package,
I'm working on my first real project with Java. I'm beginning to get comfortable
I'm working on my first GWT-based Java project for a school assignment. It's a
I'm working on a project using OpenCV, and there is a function which takes
I'm working on my first C#/WPF project (I'm a Java/Web developer with some Flex/As
I'm working on my first project using Python 2.7. We're coming from a Java
I am working on a Java project using Netbeans for the first time. I
I am working phonegap related project first .xib file i have webview from java
I am working in two java projects. The first supports OSGi components, second does

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.