2014-11-03 55 views
0

我有一項任務,要求提供一種我不熟悉的輸入形式。它要求輸入是一個字符串,我們必須從該字符串中取出一個int和double。我無法弄清楚如何使用所需的輸入語句「console.nextDouble()」和「console.nextInt()」來完成此操作。我包括了下面的相關部分和我寫的代碼。任何有關如何寫出這些內容的見解都會有所幫助。如何使用Java掃描儀控制檯從字符串輸入令牌

說明:爲了獲得最大的5個點上 分配的這部分,你的程序必須接受輸入爲4個令牌和驗證 輸入是在適當的格式。第一個標記將爲 「金額」或「年」(不含引號)。如果第一個標記爲 「金額」,則下一個是貸款金額,應該使用console.nextDouble()在 中讀取。如果第一個標記是「年」,則下一個 標記是年數,應該用 console.nextInt()讀入。如果第一個標記爲「金額」,則第三個標記 應爲「年」,並使用console.nextInt()將年數作爲第四個 標記讀入。如果第一個標記是「年」,則第三個標記應該是「數量」,並且應該用 console.nextDouble()讀入數量。任何其他令牌都被視爲無效輸入。

public static void main(String[] args) { 
    final Scanner console = new Scanner(System.in); 
    System.out.println("Project 2 written by KC"); 
    System.out.println(); 
    System.out.println("Enter the loan amount and loan duration in years,\nfor example amount 3000 years 2"); 
    String input = console.nextLine(); 
    String[] parts = input.split(" "); 
    String part1 = parts[0]; 
    String part2 = parts[1]; 
    String part3= parts[2]; 
    String part4 = parts[3]; 
    if (part1.equals("amount")) { 
     double amount = console.nextDouble(part2); 
     int years = console.nextInt(part4); 
    } else if (part1.equalsIgnoreCase("years")){ 
     int years = console.nextInt(part2); 
     double amount = console.nextDouble(part4); 
    } 
    int years= console.nextInt(); 
    double rate = loanRate(amount); 
    System.out.println("Loan Amount: "+ customFormat(amount)); 
    System.out.println("Loan Period: " + years + " years"); 
    System.out.println("Loan Rate: " + rate + "%"); 
    System.out.println("Monthly Payment: " + customFormat(loanMonthlyPayment(amount, years, rate))); 
    System.out.println("Month" + padOnLeft("Balance", 15) + padOnLeft("Payment", 15) + padOnLeft("Remaining", 15)); 
    System.out.println("Total Payment Amount: " + customFormat(loanTotalPayment(amount, years, rate, (loanMonthlyPayment(amount, years, rate))))); 
    } 
} 

回答

0

這是你的問題。你想使用掃描儀從一個句子中提取單詞,並獲得整數和double值。你給這個片段試一試。我修改了一部分代碼。: -

Scanner scan=new Scanner(System.in); 
    System.out.println("Enter the string:-"); 
    String st=scan.nextLine(); 
    Scanner sc=new Scanner(st); 
    String arr[]=new String[4]; 
    int i=0; 
    double val=0; 
    int n=0; 
    while(i<4){ 
     String st1=sc.next(); 
     arr[i]=st1; 
     i++; 
    } 
    if(arr[0].equalsIgnoreCase("amount")){ 
    n=Integer.parseInt(arr[3]); 
    val=Double.parseDouble(arr[1]); 
} 
    if(arr[0].equalsIgnoreCase("years")){ 
     val=Double.parseDouble(arr[3]); 
     n=Integer.parseInt(arr[1]); 
    } 
0

更改此:

String input = console.nextLine();

要:

String input = console.next();

next()應採取字符串類型。

根據http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

公共字符串下一個(Pattern pattern)在

返回下一個令牌,如果它與指定模式匹配。即使先前調用hasNext(Pattern)返回true,此方法也可能在等待輸入掃描時阻塞。如果匹配結果爲 成功,則掃描儀前進通過與 模式匹配的輸入。

參數: 模式 - 要掃描的返回模式: 下一個標記拋出: NoSuchElementException - 如果沒有更多的標記可用 IllegalStateException - 如果此掃描器已關閉

+0

我覺得這是一個nswer缺乏爲什麼這個工作的全面探索。提到這種情況如何,只有行中的第一個字會被接收,允許他使用'nextDouble()'而不是其他的東西 – 2014-11-03 23:15:37

+0

我可以進一步解釋當我再次訪問PC時 – ryekayo 2014-11-03 23:20:13

+0

並希望有人不會在此之前出現併發布更好的格式化答案?如果我是你的話,我會立即修復它。你的選擇雖然 – 2014-11-03 23:24:22