gpt4 book ai didi

java - 如何处理 *.txt 文件中缺少键的异常?属性文件?

转载 作者:行者123 更新时间:2023-12-01 16:38:09 25 4
gpt4 key购买 nike

首先,我是 Java 新手。

我正在尝试获取属性 key ,或者如果未找到 key ,则会出现异常。

Enumeration<Object> enumeration = properties.keys();

System.out.print("Enter the key name: ");
String getProp = bufferedReader.readLine();
String keys = null;

while(enumeration.hasMoreElements()) {
keys = (String)enumeration.nextElement();
if (!getProp.equals(keys)) {
throw new IllegalArgumentException("KEY IN " + name + ".properties FILE NOT FOUND");
}
}

但由于某种原因,即使 key 存在,我也会得到异常。

我做错了什么?再说一次,我是 Java 新手,所以请不要这么严厉地打击我。

也许这个条目

!getProp.equals(keys)

在Java中是不正确的?我不知道Java中的“不等于”模拟是什么。

最佳答案

如果枚举中的第一个键不是正确的键,则会抛出异常,因此需要在 while 循环结束后抛出该异常。

Enumeration<Object> enumeration = properties.keys();

System.out.print("Enter the key name: ");
String getProp = bufferedReader.readLine();
String keys = null;

boolean foundKey = false;
while(enumeration.hasMoreElements()) {
keys = (String)enumeration.nextElement();
if (getProp.equals(keys)) {
foundKey = true;
break;
}
}

//only throw the exception when you've tried every single key
if (!foundKey) throw new IllegalArgumentException("KEY IN " + name + ".properties FILE NOT FOUND");

关于java - 如何处理 *.txt 文件中缺少键的异常?属性文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61916208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com