2014-12-08 36 views
0

我正在爲java的最終項目工作,我需要做一個項目,獲取一堆關於貸款的細節。然後,我應該把5個貸款放入一個數組中,然後獲取詳細信息,然後顯示所有細節,但由於某種原因,在我的循環中會自動給出無效的類型錯誤。該錯誤是這樣的:循環自動跳過我的類型= input.nextInt()部分

請輸入基準利率

請輸入貸款類型。

選擇商業或個人。如果你不這樣輸入,你會得到一個錯誤。

您輸入了無效類型。請重新啓動並重試。

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); 
    } 

} 
+0

它不是。我需要知道如何在循環中5次獲得這些細節。多數民衆贊成在我的項目說,但是當我這樣做時,它會自動去其它條款說它是一個無效的類型 – buckley183 2014-12-08 05:21:08

+0

當你不輸入任何稀釋在你的'類型'然後將執行其他部分,你寫'System.exit 0);'....請刪除該行並嘗試執行。 – 2014-12-08 05:21:42

+0

然後它繼續將所有內容都設置爲0.之後,我得到了跳過下一行的東西,但是因爲它表明第一次通過錯誤循環並導致所有內容都爲0,因爲我無法輸入類型。 – buckley183 2014-12-08 05:24:17

回答

0

當輸入優質貸款時,您在輸入貸款後點擊回車,此回車將一個換行符添加到緩衝區。 nextint將爲您獲得下一個int,以便獲得您的主要興趣,然後當您使用readLine讀取readline方法讀取的行時,直到您按下輸入時插入緩衝區中的換行符,這就是爲什麼它會自動給出無效類型。 要解決這個問題:

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(); 
1

更新您的線路:

type = input.next();