gpt4 book ai didi

java - 通过缓冲读取器接受 char 值并避免 NumberFormatException

转载 作者:行者123 更新时间:2023-12-02 11:42:06 25 4
gpt4 key购买 nike

我通过缓冲读取器接受字符值,但收到 NumberFormatException。我在不同情况下使用 char 值(菜单驱动程序)。我如何仍然接受和使用 char 值?{

System.out.println("Enter the type of pattern required");
char ch =(char)buf.read();
System.out.println("Enter the size");
int x=Integer.parseInt(buf.readLine());
switch(ch)
{
case 'A':
//other cases

}输入字符后就会出现运行时错误。谢谢。

最佳答案

假设您输入了 a<Enter>

您的缓冲阅读器读取 a字符:

char ch =(char)buf.read();

然后它读取行尾:

buf.readLine()

该行的末尾是一个空字符串。因此出现错误消息(您应该仔细阅读,因为它告诉您要尝试解析为整数的内容,从而解释问题):

java.lang.NumberFormatException: For input string: ""

关于java - 通过缓冲读取器接受 char 值并避免 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48476067/

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