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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:11:00+00:00 2026-05-30T05:11:00+00:00

I’m trying to code a loan calculator. I seem to be having issues. I

  • 0

I’m trying to code a loan calculator. I seem to be having issues. I am trying to get an input from the user and validate the input. I know I am doing it wrong the problem is I’m scratching my head wondering how to do it right.
I get a red line on the d = getDouble(sc, prompt); and the i = getInt(sc, prompt); which I understand I don’t have that coded correctly. I’m just unsure how to go about fixing it.

I also have to validate the continue statement which I wasn’t to sure the best way to go about that and finally the instructor expects the code to be 80 lines or less which I am right about 80 lines. I guess I’m looking for a better way to do this but being new I’m scratching my head and I’m hoping someone can lend a hand.

As always I really appreciate the help.

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

    public class LoanCalculator
    {   
        public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
        {
          double d = 0.0;
          boolean isValid = false;
          while(isValid == false);
           {
                d = getDouble(sc, prompt);
                if (d <= min)
                   {
                   System.out.println("Error! Number must be greater tha 0.0");
                   }
                   else if (d >= max)
                  {
                     System.out.println("Error number must be less than 1000000.0");
                }
                else 
                    isValid = true;
            }
            return d;       
        }
            public static int getIntWithinRange(Scanner sc, String prompt, int min, int max)
        {
            int i = 0;
            boolean isvalid = false;
            while(isvalid == false)
             {
                i = getInt(sc, prompt);
                if (i <= min)
                       System.out.println("Error! Number must be more than 0");
                else if (i >= max)
                    System.out.println("Error! Number must be less than 100");
                else 
                    isvalid = true;
            }   
        }
          public static void main(String[] args)
         {
            System.out.println("Welcome to the loan calculator");
            Scanner sc = new Scanner(System.in);
            String choice = "y";
            while (choice.equalsIgnoreCase("y"))
            {
                System.out.println("DATA ENTRY");
                double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0.0, 1000000.0);
                double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 20);
                int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);
                int months = years * 12;

                double monthlyPayment = loanAmount * interestRate/
                        (1 - 1/Math.pow(1 + interestRate, months));

                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
                percent.setMaximumFractionDigits(3);
                System.out.println("RESULST");
                System.out.println("Loan Amount" + currency.format(loanAmount));
                System.out.println("Yearly interest rate: " + percent.format(interestRate));
                System.out.println("Number of years: " + years);
                System.out.println("Monthly payment: " + currency.format(monthlyPayment));

                System.out.println();
                System.out.println("Continue? (y/n): ");
                choice =sc.next();
                System.out.println();

            }           
        }
    }
  • 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-30T05:11:01+00:00Added an answer on May 30, 2026 at 5:11 am

    I think getDouble and getInt are string functions so you would have to get a string first then call those methods. However, since you have a scanner, I assume you want to use that with the nextXXX methods:

    Scanner sc = new Scanner (System.in);
    double d = sc.nextDouble();
    

    You can use this complete snippet for educational purposes:

    import java.util.Scanner;
    class Test {
        public static void main (String args[]) {
            Scanner sc = new Scanner (System.in);
    
            System.out.print("Enter your double: ");
            double d = sc.nextDouble();
    
            System.out.print("Enter your integer: ");
            int i = sc.nextInt();
    
            System.out.println("You entered:  " + d + " and " + i);
        }
    }
    

    Transcript:

    Enter your double: 3.14159
    Enter your integer: 42
    You entered:  3.14159 and 42
    

    Basically, the process is:

    • Instantiate a scanner, using the standard input stream.
    • Use print for your prompts.
    • Use the scanner nextXXX methods for getting the input values.

    A little more assistance here, based on your comments.

    In your main function, you have:

    double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0.0, 1000000.0)
    

    and that function has the prototype:

    public static double getDoubleWithinRange(
        Scanner sc, String prompt, double min, double max)
    

    That means those variables in the prototype will be set to the values from the call. So, to prompt for the information, you could use something like (and this is to replace the d = getDouble(sc, prompt); line):

    System.out.print(prompt);
    double d = sc.nextDouble();
    

    And there you have it, you’ve prompted the user and input the double from them. The first line prints out the prompt, the second uses the scanner to get the input from the user.

    As an aside, your checks for the minimum and maximum are good but your error messages have hard-coded values of 0 and 100K. I would suggest that you use the parameters to tailor these messages, such as changing:

    System.out.println("Error! Number must be greater tha 0.0");
    

    into:

    System.out.println("Error! Number must be greater than " + min);
    

    That way, if min or max change in future , your users won’t get confused 🙂


    I’ll leave it up to you to do a similar thing for the integer input. It is your homework, after all 🙂

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.