Pre*_*_it 12 java exception-handling hashmap
所以我想输入一个数字,然后输入一个人或其他人的名字.这没有问题,但为什么我不能在1个块中放置2个错误异常?
while (true) {
try {
int id = Integer.parseInt( reader.readLine() );
String name = reader.readLine();
if (name.equals("")) {
break;
}
map.put(name, id);
} catch (NumberFormatException | IOException e) {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
当我试图打印我的价值时,我明白了 NumberFormatException
for (Map.Entry<Integer, String> pair: map.entrySet()) {
int id = pair.getKey();
String name = pair.getValue();
System.out.println(id + " " + name);
}
Run Code Online (Sandbox Code Playgroud)
Art*_*ool 21
问题不在代码中,而是在编译器级别.只需检查IDE设置并将编译器级别设置为1.7或更高.
如果您使用IntelliJ并在以下路径中正确设置为7.0,但仍然无法正常工作:
文件 - >项目结构 - >项目设置 - >项目 - >项目语言级别
在模块中设置:
文件 - >项目结构 - >项目设置 - > 模块 - > 项目语言级别
就我而言,我的显式配置可以解决问题pom.xml。文件->项目结构->项目->项目语言级别中的IntelliJ设置已设置为我的JDK 1.7,但IDE一直忽略该设置。
我使用Maven编译器插件将源版本和目标版本指定为1.7
为此,在本<plugins>节中,添加以下行:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9377 次 |
| 最近记录: |