Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.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 为什么它说can';t使用int a=Integer.parseInt(System.in.read())时,将字符串转换为int类型;_Java_Runtime Error - Fatal编程技术网

Java 为什么它说can';t使用int a=Integer.parseInt(System.in.read())时,将字符串转换为int类型;

Java 为什么它说can';t使用int a=Integer.parseInt(System.in.read())时,将字符串转换为int类型;,java,runtime-error,Java,Runtime Error,我无法理解哪里出了问题,因为System.in.read()将ascii no作为字符串,所以使用Integer.parseInt必须解决该问题,并且必须返回控制台上输入的值 编译时抛出错误,无法将String转换为int类型 请检查并告诉我哪里出错了 import java.io.IOException; class AB { public static void main(String[] args)throws IOException { int a

我无法理解哪里出了问题,因为
System.in.read()
将ascii no作为字符串,所以使用
Integer.parseInt
必须解决该问题,并且必须返回控制台上输入的值

编译时抛出错误,无法将
String
转换为
int
类型

请检查并告诉我哪里出错了

import java.io.IOException;

class AB 
{
    public static void main(String[] args)throws IOException 
    {
        int a = Integer.parseInt(System.in.read()); 

        System.out.print(a);
    }
}

System.in.read()
返回一个
int
parseInt
需要一个
字符串。实际错误是“int无法转换为java.lang.String”。您可能应该使用扫描仪从stdin读取int:
Scanner sc=new Scanner(System.in);if(sc.hasNextInt()){int a=sc.nextInt();/*[…]*/}