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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:47:32+00:00 2026-06-15T08:47:32+00:00

UPDATE: Thanks for the advice about keeping codes SSCCE. Like I said, this is

  • 0

UPDATE: Thanks for the advice about keeping codes SSCCE. Like I said, this is my first time posting here. I’ll definitely take the time next time to make sure the code is sufficiently trimmed down before I post here next time.


So I’m writing a program for my computer science class, and having a strange problem that I can’t figure out. I tried to make my program robust by using try/catch statements to stop invalid data type input before it could foul up and end the entire program, but it doesn’t seem to be working. When an invalid data type is entered, the program ends without displaying any of Java’s standard error messages in the window. I have no idea why this is, but I assume I’m using the try/catch statements incorrectly. Here are the two classes for my program:

/* The core class for the finance calculations to be done in the FinanceApp class. */

public class FinanceCore{
  // Instance variables
  int numYears;
  double principal;
  double interestRate;
  double balance;

  public FinanceCore(){
    numYears = 0;
    principal = 0;
    interestRate = 0;
    balance = 0;
  }

  // Mutator methods that return boolean values depending on whether the input was valid or not
  public boolean setYears(int y){
    if(y >= 0){
      numYears = y;
      return true;
    }
    else return false;
  }

  public boolean setPrincipal(double p){
    if(p >= 0){
      principal = p;
      balance = principal;
      return true;
    }
    else return false;
  }

  public boolean setInterestRate(double ir){
    if(ir >= 0 && ir <= 1){
      interestRate = ir;
      return true;
    }
    else return false;
  }

  // Two accessors
  public int getYears(){
    return numYears;
  }

  public double getPrincipal(){
    return principal;
  }

  // This method calculates and returns the balance at the end of each year
  public double plusYear(){
    balance = balance*(1+interestRate);
    return balance;
  }
}

/* This program recieves three pieces of data (interest rate, principal amount, number of years) and generates an output
* table that shows how much money will be in a fund with the given parameters at the end of every year. */

import java.util.Scanner;

public class FinanceApp{

  public static void main(String[]args){
    // First, we will declare our global variables, and set them to default values
    Scanner reader = new Scanner(System.in);
    FinanceCore account = new FinanceCore();
    int menuItem = 0;

    // Now, we'll greet the user (because we're friendly like that)
    System.out.println("Welcome! Please select a menu option below.");

    while(true){
      /* Now, our first user interface: a menu system that displays four options to the user arranged in
       * columns for aesthetic effect. This is accomplished using the printf method.
       */

      System.out.printf("%n%-20s%-20s%n%-20s%-20s%n%-20s%n",
        "Set Principal[1]","Set Interest Rate[2]","Set Timespan[3]","Calculate[4]","Quit[5]");

      System.out.print(": ");

      // Now we get the user input until it is valid, and catch and errors in input type
      try {
        menuItem = reader.nextInt(); 
      }
      catch(Exception e){
        reader.nextLine(); // Clear the input stream to avoid an infinite loop
        System.out.println("Please a valid number 1-5.");
      }

      // The code for setting the principal amount
      if(menuItem == 1){
      while(true){
        System.out.print("Please enter the principal investment amount: ");

        try{
          if(account.setPrincipal(reader.nextDouble()));
          break;
        }
        catch(Exception e){
          reader.nextLine(); // Clear the input stream to avoid an infinite loop
          System.out.println("Please enter a valid dollar amount.");
        }
      }
    }
    // The code for setting the interest rate
    else if(menuItem == 2){
      while(true){
        System.out.print("Please enter the quarterly interest rate: ");
        try{
          if(account.setInterestRate(reader.nextDouble()));
          break;
         }
         catch(Exception e){
           reader.nextLine(); // Clear the input stream to avoid an infinite loop
           System.out.println("Please enter a valid decimal number between 0 and 1 (inclusive).");
         }
       }
     }

     // The code for setting the number of years
     else if(menuItem == 3){
       while(true){
         System.out.print("Please enter the number of years the account will exist: ");
         try{
           if(account.setYears(reader.nextInt()));
           break;
         }
         catch(Exception e){
           reader.nextLine(); // Clear the input stream to avoid an infinite loop
           System.out.println("Please enter a valid integer value.");
         }
       }
     }

     // This part actually executes the calculation
     else if(menuItem == 4){
       System.out.printf("%-10s%-10s%n%-10d%-10.2f%n","YEAR","BALANCE",0,account.getPrincipal());
       int count = 1;
       for(int c = account.getYears(); c > 0; c--){
         System.out.printf("%-10d%-10.2f%n",count,account.plusYear());
         count++;
       }
     }

     // If the user enters any other number, the program quits
     else
       break;
     }
   }
 }

Note that there is one persistent problem with this program that I can’t seem to fix. For some reason, every time
the user enters an invalid data type at the menu selection prompt, the program ends (although without any errors
being thrown).

  • 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-15T08:47:33+00:00Added an answer on June 15, 2026 at 8:47 am

    When you use a try-catch block, you’re essentially telling the compiler that you’re going to take care of displaying the error messages–no built-in messages will show. Instead, whatever error prompts you include in the catch statements will show.

    When I run your program, I see your error messages, but no built-in Java error messages; this is how it should be. Really, the errors are being thrown–but you’re catching them, so Java doesn’t display the default message on the console.

    Regarding your comment at the end of the program:

    Look at what’s happening if the user enters an incorrect data type at the menu prompt; menuItem is still zero. Thus, all the if statements evaluate to false. Thus, the else statement runs, which terminates the program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
UPDATE: I've been playing around with this more, and it seems like tmux's clear-history
First of all, I hope I'd get some advice about my practice because based
Thanks for taking the time to read my question. I am looking for advice
Update Thanks to Marc's help the AlphaPagedList class is now available on CodePlex if
Update: Thanks guys, I didn't realize it was very close to zero but not
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find
Update: I reported this as a bug to Apple and they fixed it! All
Update : This is no longer an issue from C# 6, which has introduced
I'm about to deploy to production a fairly complex site and for the first

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.