Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据.txt文件中的信息检查用户输入_Java_File_Validation_Loops - Fatal编程技术网

Java 根据.txt文件中的信息检查用户输入

Java 根据.txt文件中的信息检查用户输入,java,file,validation,loops,Java,File,Validation,Loops,我正在创建一个项目,我们在其中进行货币兑换。其中一个步骤是我必须让用户输入他们想从美元兑换成什么类型的货币。我必须做一个while循环来检查并确保输入的文本文件是正确的 文本文件中只有以下内容 CD 1.05 0.93 MP 0.11 0.095 欧盟1.554 1.429 因此,人们会输入CD MP或EU,只是不知道如何检查它。使用eclipse时,txt文件位于项目文件夹中 public class Communication { public String askForCode()

我正在创建一个项目,我们在其中进行货币兑换。其中一个步骤是我必须让用户输入他们想从美元兑换成什么类型的货币。我必须做一个while循环来检查并确保输入的文本文件是正确的

文本文件中只有以下内容

CD 1.05 0.93
MP 0.11 0.095
欧盟1.554 1.429

因此,人们会输入CD MP或EU,只是不知道如何检查它。使用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行来验证它吗?

您可以使用
showOptionDialog

    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]);
    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");

甚至是更严格的
showInputDialog

    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]);
    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");

这会将用户可用的选项限制为您希望他们使用的选项

有关更多详细信息,请参阅


您可以读取文本文件,将值存储在数组中,
列表
映射
,并直接在对话框中使用代码来增加灵活性…

文件。readAllLines(Path,Charset)
将文件中的行读取到ArrayList,您可以使用
toArray()将其更改为字符串数组
。您可以使用
toPath()
将文件更改为路径。然后,您可以将每一行的开头与输入的字符串进行比较,直到找到正确的行。

因此,用户将输入
CD 1.05 0.93
,您想检查它是什么货币?请阅读每行对不起,用户只输入货币代码,数字是汇率。他们只会进入CD、MP或EU