Java convert converts string to int when using BufferedReader

How to convert string to int when using BufferedReader?

System.out.println("input a number");

int n=Integer.parseInt(br.readLine(system.in));

But for some reason, it doesn't work

The error message says:

No suitable method (Java. Io. InputStream) was found for readLine

It also says br ReadLine not applicable

Solution

Inputstreamreader needs to be specified in the BufferedReader constructor Inputstreamreader converts a byte stream to a character stream As others have mentioned, you can recognize the exceptions that can be thrown from this code, such as IOException and numberformatexception

BufferedReader br = new BufferedReader(new InputStreamReader(system.in));
System.out.println("input a number");
int n = Integer.parseInt(br.readLine());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>