2014-11-14 88 views
0

我正在創建一個項目,我們在該項目中進行貨幣兌換。其中一個步驟是我必須讓用戶輸入他們想要從美元轉換成什麼類型​​的貨幣。我必須做一個while循環來檢查並確保輸入對於文本文件是正確的。根據.txt文件中的信息檢查用戶輸入

的文本文件,只是有這個

CD 1.05 0.93
MP 0.11 0.095
歐盟1.554 1.429

因此,人會進入CD MP或歐盟只是不知道如何檢查它。使用eclipse和txt文件在項目文件夾中。

public class Communication { 
    public String askForCode(){ 

    String currencyCode = JOptionPane.showInputDialog(null, "We exchange the following Currencies: \n " 
      + "CD (Canadian Dollar)\n" 
      + "MP (Mexican Peso) \n" 
      + "EU (Euro) \n" 
      + "Enter the Currency Code"); 

    File myFile = new File("dailyrates.txt"); 
    while (!currencyCode.equals){ 

    } 
    return currencyCode; 

} 

我會用file.next行來驗證它嗎?

+0

所以你用戶會輸入'CD 1.05 0.93',你想檢查它是什麼貨幣?在每行中閱讀 – smushi

+0

對不起,用戶只輸入貨幣代碼的數字是匯率。他們只會進入CD,MP或EU – Logan

回答

1

你可以使用showOptionDialog ...

Options

Object[] options = {"CD", 
     "MP", 
     "EU"}; 
    int n = JOptionPane.showOptionDialog(null, 
        "Enter the FROM Currency Code", 
        "From Currency", 
        JOptionPane.YES_NO_CANCEL_OPTION, 
        JOptionPane.QUESTION_MESSAGE, 
        null, 
        options, 
        options[2]); 

,甚至更嚴格的showInputDialog ...

Option

Object[] possibilities = {"CD", "MP", "EU"}; 
    String s = (String) JOptionPane.showInputDialog(
        null, 
        "Enter the FROM Currency Code", 
        "From Currency", 
        JOptionPane.PLAIN_MESSAGE, 
        (Icon)null, 
        possibilities, 
        "CD"); 

這將限制用戶availab樂選項,你希望他們使用什麼

更多細節

How to Make Dialogs您可以閱讀文本文件,在陣列中存儲的值,ListMap並直接使用該對話框中的代碼添加更多靈活性...

0

Files.readAllLines(Path, Charset)將讀取文件中的行到ArrayList,您可以將其更改爲toArray()的字符串數組。您可以使用toPath()將文件更改爲路徑。然後,您可以將每行的開頭與輸入的字符串進行比較,直到找到正確的行。

相關問題