【发布时间】:2019-11-24 17:40:13
【问题描述】:
所以我是 Java 的初学者,我很难学习,所以请放轻松。我正在研究一种解决方案,该解决方案将用户输入英国货币并找到用户输入国家/地区的汇率。不知道我是否走在正确的轨道上,但下面是用户输入要转换的货币金额和国家后的代码。我需要读取基于 Web 的 CSV 文件并解析为 POJO,但我无法让 in.useDelimiter 工作。
public static Double findExchangeRateAndConvert(String currencyType, double amount) throws IOException {
URL url = new URL("https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/842362/exrates-monthly-1119.csv");
try (var in = new Scanner(
new BufferedReader(
new InputStreamReader(url.openStream())))) {
var line = "";
in.useDelimiter(",");
while (in.hasNextLine()) {
line = in.nextLine();
System.out.println(line);
if (line.contains(currencyType)) {
System.out.println("I found it.");
System.exit(0);
}
}
}
return null;
}
【问题讨论】:
-
如果您正在阅读整行,则无需使用逗号作为分隔符
-
我建议使用 Jackson ObjectMapper 之类的东西将 csv 解析为 POJO,不过