while using BufferedReader to read 2 numbers in same line I was having an Error: java.lang.NumberFormatException: For input string: "2 3"

huangapple go评论61阅读模式
英文:

while using BufferedReader to read 2 numbers in same line I was having an Error: java.lang.NumberFormatException: For input string: "2 3"

问题

Iam new to BufferedReader. I was trying to take input of 2 numbers in the same line for ex: 2 3. For that I have written the below code.

import java.io.*;
public class Main
{
    public static void main(String[] args)throws IOException{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(bufferedReader.readLine().trim());

        int M = Integer.parseInt(bufferedReader.readLine().trim());
        System.out.println("N="+N+" M="+M);
    }
}

So Iam encountering the below error.

Exception in thread "main" java.lang.NumberFormatException: For input string: "2 3"                                                 
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)                                            
        at java.lang.Integer.parseInt(Integer.java:580)                                                                             
        at java.lang.Integer.parseInt(Integer.java:615)                                                                             
        at Main.main(Main.java:15)                                                                                                  

What's wrong with the code. Thanks in advance.

英文:

Iam new to BufferedReader. I was trying to take input of 2 numbers in the same line for ex: 2 3. For that I have written the below code.

import java.io.*;
public class Main
{
	public static void main(String[] args)throws IOException{
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(bufferedReader.readLine().trim());

        int M = Integer.parseInt(bufferedReader.readLine().trim());
        System.out.println("N="+N+"M="+M);
	}
}

So Iam encountering the below error.

Exception in thread "main" java.lang.NumberFormatException: For input string: "2 3"                                                 
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)                                            
        at java.lang.Integer.parseInt(Integer.java:580)                                                                             
        at java.lang.Integer.parseInt(Integer.java:615)                                                                             
        at Main.main(Main.java:15)                                                                                                  

What's wrong with the code. Thanks in advance.

答案1

得分: 1

当在同一行输入多个输入时,您应该定义一个用于分隔输入的字符。在您的示例中,似乎是通过空格来完成的。

您需要使用 split() 创建一个字符串形式的单行输入的响应数组,然后使用 Integer.parseInt() 将它们分配给您需要的变量。

import java.io.*;
public class Main
{
    public static void main(String[] args) throws IOException {
        // 创建 BufferedReader 和两个整数
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int N, M;

        // 从用户接收输入,并使用空格将其拆分为数组
        // '\\s+' 匹配任何大小的空白字符
        String[] inputs = bufferedReader.readLine().split("\\s+");

        N = Integer.parseInt(inputs[0]);
        M = Integer.parseInt(inputs[1]);

        System.out.println("N=" + N + " M=" + M);
    }
}
英文:

When taking in multiple inputs on the same line, you should define a character that will be used to separate inputs. In your example, it seems like this is done through whitespace.

You need to create an array of responses from the single line input as a string using split(), and then assign them to the variables you need using Integer.parseInt().

import java.io.*;
public class Main
{
    public static void main(String[] args) throws IOException {
        // Create BufferedReader and two ints
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int N, M;

        // Take in inputs from user, and split them into an array using whitespace
        // '\\s+' matches at any whitespace size
        String[] inputs = bufferedReader.readLine().split("\\s+");

        N = Integer.parseInt(inputs[0]);
        M = Integer.parseInt(inputs[1]);

        System.out.println("N=" + N + " M=" + M);
    }
}

huangapple
  • 本文由 发表于 2020年8月1日 18:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63204023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定