Java 代码中的NumberFormatException

Java 代码中的NumberFormatException,java,numberformatexception,Java,Numberformatexception,我在Amazon的访谈街网站上解决了连接集问题,我的代码对该网站提供的公共样本测试用例非常有效,但我在第25行得到了隐藏测试用例的NumberFormatException。以下是我的代码中解析输入的部分: public class Solution { static int [][] arr; static int num = 2; static int N; static String output = ""; public static

我在Amazon的访谈街网站上解决了连接集问题,我的代码对该网站提供的公共样本测试用例非常有效,但我在第25行得到了隐藏测试用例的NumberFormatException。以下是我的代码中解析输入的部分:

 public class Solution
 {
     static int [][] arr;
     static int num = 2;
     static int N;
     static String output = "";
     public static void main(String[] args) throws IOException
     {
         int T;
         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
         T = Integer.parseInt(reader.readLine());
         int i, j, k;

         for(i=0;i<T;i++) //the loop for each of the 'T' test cases
         {
            N = Integer.parseInt(reader.readLine()); //line 25
            arr = new int[N][N];

            for(j=0;j<N;j++) //the loops for storing the input 2D array
            {
                for(k=0;k<N;k++)
                {
                    arr[j][k] = Character.getNumericValue(reader.read());
                    reader.read();
                }
            }
公共类解决方案
{
静态int[]arr;
静态int num=2;
静态int N;
静态字符串输出=”;
公共静态void main(字符串[]args)引发IOException
{
int T;
BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
T=Integer.parseInt(reader.readLine());
int i,j,k;
对于(i=0;iT=Integer.parseInt(reader.readLine());这就是问题所在。当用户尝试给出字符串时

它转换成整数并通过循环运行?例如:如果用户将“R”作为字符,那么整数转换将如何进行,循环是否会进一步运行?不正确。

好的,因此我修改了我的程序,以合并LeosLiterak关于使用trim()的建议,新代码如下:

public class Solution
{
    static int [][] arr;
    static int num = 2;
    static int N;
    static String output = "";
    public static void main(String[] args) throws IOException
    {
        int T;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        T = Integer.parseInt(reader.readLine().trim());
        int i, j, k;

        for(i=0;i<T;i++)
        {
            N = Integer.parseInt(reader.readLine().trim());
            arr = new int[N][N];

            for(j=0;j<N;j++)
            {
                String [] temp = reader.readLine().trim().split(" ");
                for(k=0;k<N;k++)
                {
                    arr[j][k] = Integer.parseInt(temp[k]);
                }
            }
公共类解决方案
{
静态int[]arr;
静态int num=2;
静态int N;
静态字符串输出=”;
公共静态void main(字符串[]args)引发IOException
{
int T;
BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
T=Integer.parseInt(reader.readLine().trim());
int i,j,k;

对于(i=0;i在解析为int之前检查其编号

Character.isNumber()

从注释中复制:尝试删除所有白色字符,如空格、制表符等。这些字符不受目标定义的禁止,但无法解析为数字。您必须首先对其进行修剪。

您是否打开了我在问题上的链接或阅读了我问题的注释?其次,错误甚至不在T=Intege行上r、 parseInt(reader.readLine())第25行,有适当的评论。我打开了链接,但它要求注册,我对亚马逊的采访不感兴趣。发布完整的问题会很好。@LeosLiterak我将编辑我的问题以提出问题。请稍等。@LeosLiterak编辑过。空格呢?先尝试修剪一下。