0

I'm working on a final project for java and I need to make a project that gets a bunch of details about a loan. Then I'm supposed to put 5 loans in an array then get the details then display all the details but for some reason in my loop in automatically gives an invalid type error. The error is this:

Please enter the prime interest rate

30

Please enter a loan type.

Choose either Business or Personal. If you don't type it like that you'll get an error.

You've entered an invalid type. Please restart and try again.

CreateLoan.java

package Construction;

import java.util.Scanner;
import Construction.Loan;


//Gotta fix an issue where it automatically gives an invalid loan type.
public class CreateLoan {

    public static void main(String[] args) {
        int x = 0;
        int primeRate;
        String type;
        Scanner input = new Scanner(System.in);
        Loan[] loans = new Loan[5];
        System.out.println("Please enter the prime interest rate");
        primeRate = input.nextInt();
        primeRate = primeRate/100;

        for(x = 0; x < 6; ++x) {
            System.out.println("Please enter a loan type. Choose either Business or Personal. If you don't type it like that you'll get an error.");
            type = input.nextLine();
            if (type.equalsIgnoreCase("Business")) {
                System.out.println("What is the account number on the loan?");
                int ln = input.nextInt();
                System.out.println("What is the last name on the account?");
                String last = input.nextLine();
                System.out.println("What is the loan amount? If you put more then 100k it'll only accept up to 100k");
                int la = input.nextInt();
                System.out.println("What is the term on the account? If you enter something other then 1, 3, or 5 it will default to a short term.");
                int term = input.nextInt();
                loans[x] = new BusinessLoan(ln, last, la, term);
            }
            else if (type.equalsIgnoreCase("Personal")) {
                System.out.println("What is the account number on the loan?");
                int ln = input.nextInt();
                System.out.println("What is the last name on the account?");
                String last = input.nextLine();
                System.out.println("What is the loan amount? If you put more then 100k it'll only accept up to 100k");
                int la = input.nextInt();
                System.out.println("What is the term on the account? If you enter something other then 1, 3, or 5 it will default to a short term.");
                int term = input.nextInt();
                loans[x] = new PersonalLoan(ln, last, la, term);
            } else {
                System.out.println("You've entered an invalid type. Please restart and try again.");
                System.exit(0);
            }
            Loan.displayAll();
        }
    }
}

Loan.java

package Construction;

public class Loan implements LoanConstant{
    public static int loanNumber;
    public static String lastName;
    public static int loanAmount;
    public static int interestRate;
    public static int term;
    public int primeRate;

    public int getLoanNumber() { return loanNumber; }
    public void setLoanNumber(int n) { n = loanNumber; }

    public String getLastName() { return lastName; }
    public void setLastName(String s) { s = lastName; }

    public int getLoanAmount() { return loanAmount; }
    public void setLoanAmount(int n) {
        n = loanAmount; 
        if (loanAmount > MAX_LOAN_AMOUNT)
            loanAmount = MAX_LOAN_AMOUNT;
        }

    public int getTerm() { return term; }
    public void setTerm(int n) { 
        n = term; 
        if (term == 1) {
            term = SHORT_TERM;
        } else if (term == 3) {
            term = MEDIUM_TERM;
        } else if(term == 5) {
            term = LONG_TERM;
        } else
            term = SHORT_TERM;
    }

    public int getInterestRate() { return interestRate; }
    public void setInterestRate(int i) { i = interestRate; }

    public Loan(int ln, String last, int la, int term) {
        setLoanNumber(ln);
        setLastName(last);
        setLoanAmount(la);
        setTerm(term);
    }

    public static void displayAll() {
        System.out.println("The Company's Name is " + COMPANY_NAME);
        System.out.println("The loan number is " + loanNumber);
        System.out.println("The last name on the loan is " + lastName);
        System.out.println("The loan amount is " + loanAmount);
        System.out.println("The interest rate on the loan is " + interestRate);
        System.out.println("The term on the account is " + term);
    }
}

PersonalLoan.java

package Construction;

public class PersonalLoan extends Loan{

    public PersonalLoan(int ln, String last, int la, int term) {
        super(ln, last, la, term);
        interestRate = (int)((primeRate * 0.02) + primeRate);
        setInterestRate(interestRate);
    }
}

BusinessLoan.java

package Construction;

public class BusinessLoan extends Loan{

    public BusinessLoan(int ln, String last, int la, int term) {
        super(ln, last, la, term);
        interestRate = (int)((primeRate * 0.01) + primeRate);
        setInterestRate(interestRate);
    }

}
buckley183
  • 23
  • 4
  • Its not. I need to know how to get those details 5 times in a loop. Thats what my project says but when I do it it automatically goes to the else clause of saying its an invalid type – buckley183 Dec 08 '14 at 05:21
  • when u dont enter any thin in your `type` then is will execute else part,where you write `System.exit(0);` ....please remove that line and try to execute. – Prashant Jajal Dec 08 '14 at 05:21
  • Then it just continues on having everything as 0. After that then I get that skipping next line thing but as it stands that first loop through bugs and causes everything to be 0 because I cant enter a type. – buckley183 Dec 08 '14 at 05:24
  • Your `nextInt()` leaves `end of line` character(s) in the keyboard buffer. Your `nextLine()` picks them up. Read the question I pointed to. – PM 77-1 Dec 08 '14 at 05:24
  • I got it to work. You were right. It was the skipping thing. Sorry about that >. – buckley183 Dec 08 '14 at 05:28
  • use BufferedReader or use next() method of Scanner, then parse the String to Integer, You won't get this issue. – Arun Xavier Dec 08 '14 at 05:28

2 Answers2

1

update your line to:

    type = input.next();
Prashant Jajal
  • 2,856
  • 2
  • 20
  • 34
0

When enter prime loan you hit enter after inputing the loan, this enter adds a line break character to the buffer. nextint will get you the next int for your prime interest and then when u read using readLine the line that readline method reads is till a linebreak character whih got inserted in buffer when u press enter, thats why it is automatically giving invalid type. To solve this :

 Scanner input = new Scanner(System.in);            
    System.out.println("Please enter the prime interest rate");
      primeRate = input.nextInt(); 
      primeRate = primeRate/100; 
      type=input.nextLine();  //clears the buffer
      for(x = 0; x < 6; ++x)
      { 
            System.out.println("Please enter a loan type. Choose either Business or Personal. If you don't type it like that you'll get an error."); 
  type = input.nextLine(); 
varun
  • 1,263
  • 1
  • 8
  • 15