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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:37:33+00:00 2026-05-30T09:37:33+00:00

I have edited this question but at the bottom of the program I have

  • 0

I have edited this question but at the bottom of the program I have tried to code a switch statement to validate some code. I’m trying to ask the user to enter Y/N to continue. If they enter Y the program should restart. If they enter N the program should end. If they enter Z they should get an error message and be prompted again with the continue y/n?
Right now if I typed a random letter I get sent back to the data entry section and I’m not sure why.

Also I noticed my calculations are off. I’m assuming it has something to do with the fact I’m not using the bigdecimal class. Currently if I typed 5.6 for the percent it automatically assumes it’s 560%

I’m new to Java and unfortunately struggling. Someone was kind enough to help me last night without blatantly giving me the answers which I appreciated because it made me hash things out on my own.

Any help is definately appreciated since I’m having a hard time with this class.

import java.util.Scanner;
import java.text.NumberFormat;

public class LoanCalculator
{
    public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max) //validate double
    {
        double d = 0;
        boolean isValid = false;
        while(isValid == false)
        {
            System.out.println(prompt); //print prompt for double
            if (sc.hasNextDouble())
            {
                d = sc.nextDouble();

                if (d <= min) //if input <= minimum double
                {
                    System.out.println("Error! Number must be greater than " + min);
                    System.out.println();//print error grater than min double
                }
                else if (d >= max) // if input >= maximum double value 
                {
                    System.out.println("Error number must be less than " + max);
                    System.out.println();//print error less than max double value
                }
                else
                    isValid = true; //else double is valid
            }
            else
            {
                System.out.println("Error! Invalid decimal value.");
                System.out.println();
                sc.nextLine();
            }
        }

        return d; //return double
    }
    //validate integer
    public static int getIntWithinRange(Scanner sc, String prompt, int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while(isValid == false)
        {
            System.out.println(prompt); //print prompt for integer
            if (sc.hasNextInt())
            {
                i = sc.nextInt();

                if (i <= min) //if input <= minimum integer
                {
                    System.out.println("Error! Number must be greater than " + min);
                    System.out.println();//print error grater than min integer
                }
                else if (i >= max) // if input >= maximum integer value
                {
                    System.out.println("Error number must be less than " + max);
                    System.out.println();//print error less than max integer value
                }
                else
                    isValid = true; //else integer is valid
            }
            else
            {
                System.out.println("Error! Invalid integer value.");
                System.out.println();
                sc.nextLine();
            }
        }

        return i; //return integer
    }

    public static void main(String[] args)
    {
        System.out.println("Welcome to the loan calculator"); //welcome user to loan calculator
        Scanner sc = new Scanner(System.in); // create new scanner
        String choice = "y"; //start choice loop
        while (choice.equalsIgnoreCase("y"))
        {
            System.out.println();
            System.out.println("DATA ENTRY");
            double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0.0, 1000000.0); //get user input for loanAmount
            //get user input for interestRate
            double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 20);
            //get user input for years
            int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);
            int months = years * 12; // calculate years to months
            //calculate monthly payment
            double monthlyPayment = loanAmount * interestRate/ (1 - 1 / Math.pow(1 + interestRate, months));
            //import currency instance
            NumberFormat currency = NumberFormat.getCurrencyInstance();
            // import percent instance
            NumberFormat percent = NumberFormat.getPercentInstance();
            percent.setMinimumFractionDigits(1); //set fraction digits for percent
            System.out.println("RESULST"); //print results
            //print loanAmount
            System.out.println("Loan Amount: " + currency.format(loanAmount));
            //print interestRate
            System.out.println("Yearly interest rate: " + percent.format(interestRate));
            System.out.println("Number of years: " + years); //print years
            //print monthlyPayment
            System.out.println("Monthly payment: " + currency.format(monthlyPayment));

            boolean quit = false;
            do {
                System.out.println();
                System.out.println("Continue? (y/n): "); //prompt user to continue
                String userinput1 = sc.next();

                char choice1 = userinput1.toLowerCase().charAt(0); 
                switch (choice1) {
                    case 'y':
                        break;
                    case 'n':
                        // case n, do something here
                        quit = true;
                        break;
                    case ' ':
                        System.out.println("Error! This entry is required. Try again.");
                        break;
                    default:
                        System.out.println("Error! Entry must be 'y or 'n''");
                        break;
                }
            } while (!quit);
        }
    }
}
  • 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-30T09:37:34+00:00Added an answer on May 30, 2026 at 9:37 am

    Use a switch like this:

    boolean quit = false;
    do{ // change your while by this
    
        // your own stuff here
    
    
        // then after all your program stuff
        boolean choiceIsOK = false;
        do{
        String userinput = sc.next();
        char choice = userinput.toLowerCase().charAt(0);
        switch(choice){
        case 'y':
            // case y, do nothing, you could even remove that case.
            choiceIsOK = true;
            break;
        case 'n':
            // case n, do something here
            choiceIsOK = false;
            quit = true;
            break;
        default:
            // error or warning
            System.out.println("Type Y or N to respectively continue or quit");
            break;
        }
        }while(!choiceIsOK);
    }while (!quit);
    

    In your code, it will look like this:

    public static void main(String[] args)
    {
    System.out.println("Welcome to the loan calculator"); //welcome user to loan calculator
    Scanner sc = new Scanner(System.in); // create new scanner
    //String choice = "y"; //start choice loop [no need for that line anymore]
    boolean quit = false; 
    do{
            System.out.println();
            System.out.println("DATA ENTRY");
            double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 
                    0.0, 1000000.0); //get user input for loanAmount
            //get user input for interestRate
            double interestRate = getDoubleWithinRange(sc, 
                    "Enter yearly interest rate: ", 0, 20); 
            //get user input for years
            int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100); 
            int months = years * 12; // calculate years to months
            //calculate monthly payment
            double monthlyPayment = loanAmount * interestRate/
                    (1 - 1/Math.pow(1 + interestRate, months)); 
            //import currency instance
            NumberFormat currency = NumberFormat.getCurrencyInstance(); 
            // import percent instance
            NumberFormat percent = NumberFormat.getPercentInstance(); 
            percent.setMinimumFractionDigits(1); //set fraction digits for percent 
            System.out.println("RESULST"); //print results 
            //print loanAmount
            System.out.println("Loan Amount: " + currency.format(loanAmount)); 
            //print interestRate
            System.out.println("Yearly interest rate: " + percent.format(interestRate)); 
            System.out.println("Number of years: " + years); //print years
             //print monthlyPayment
            System.out.println("Monthly payment: " + currency.format(monthlyPayment)); 
    
    
            // then after all your program stuff
            boolean choiceIsOK = false;
                do{
    
                    System.out.println();
                    System.out.println("Continue? (y/n): "); //prompt user to continue 
                    String userinput1 = sc.next();
                        char choice1 = userinput1.toLowerCase().charAt(0);
                        switch(choice1){
                        case 'y':
                            // case y, do nothing, you could even remove that case.
                            choiceIsOK = true;
                            break;
                        case 'n':
                            // case n, do something here
                            choiceIsOK = true; // I fixed this
                            quit = true;
                            break;
                        default:
                            // error or warning
                            System.out.println("Type Y or N to respectively continue or quit");
                            break;
                        }
                    }while(!choiceIsOK);
    
        }while(!quit);
    
        }           
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have this code inside a header (edited): template <int i> class A {};
I have edited and simplified this question a lot. If I have this method
I have edited this question: At root, I have a working folder F (root/F),
edited for clarity I feel like this question already has an answer, but I
I have seen this question already but it does not explain it so that
I have edited this OS 3.2 question based on advice that it could infringe
--edited for clarity (hopefully) I have an XML file that looks something like this:
I have edited the question as what I said before was two pieces of
Sorry for this question. Basically I have a page where I have it automatically

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.