我想把这个C代码转换成java代码,但我在数组中输入时遇到了一个问题

我想把这个C代码转换成java代码,但我在数组中输入时遇到了一个问题,java,c,arrays,Java,C,Arrays,线程“main”java.lang.NumberFormatException中的异常:用于输入 字符串:“”位于 forInputString(NumberFormatException.java:65) 位于java.lang.Integer.parseInt(Integer.java:504) java.lang.Integer.parseInt(Integer.java:527)位于 main(arr2d.java:31) 只需尝试使用Scanner类而不是BufferedReader,

线程“main”java.lang.NumberFormatException中的异常:用于输入 字符串:“”位于 forInputString(NumberFormatException.java:65) 位于java.lang.Integer.parseInt(Integer.java:504) java.lang.Integer.parseInt(Integer.java:527)位于 main(arr2d.java:31)


只需尝试使用
Scanner
类而不是
BufferedReader
,因为有时当您错误地输入
empty
字符串时,您会从
Integer.parseInt获得
NumberFormatException
例如

Scanner
类具有内置函数,可以等待输入所需的基本类型值。

注意如果您在需要数值时输入非数值,则内置函数如
nextInt
nextByte
等可能会引发
InputMismatchException

您输入了空字符串,因此出现了NumberFormatException而不是整数,并且无法解析此空字符串


你的程序运行良好。只有当我按Enter键而不输入任何输入时(换句话说,作为输入传递一个空字符串),才会出现异常。

我没有看到任何错误,只是运行了一遍,一切正常。我也使用了你精确的测试数据。我假设您输入了错误的数据。

可能重复的数据您知道什么是
NumberFormatException
吗?查找它可能会有所帮助。
java.lang.NumberFormatException:对于输入字符串:“”
消息非常清楚:您正在尝试将空字符串转换为数字,但无法成功。检查字符串是否为空,捕获异常并进行相应处理,或者检查代码为什么字符串为空(如果不是用户输入的)。这是因为从br.readLine()获得的输入字符串为空,即“”。当您只按“回车”键而不输入数字时,一定发生了这种情况。相反,添加一个检查,查看输入的值是否为某个值,而不是null。您还必须处理一些情况,以检查输入是否是数字而不是文本。。祝你好运谢谢你的消息先生我应该研究一下NumberFormatException@Trick不客气!如果你认为我的回答对你有帮助,你应该投赞成票干杯
#include <stdio.h>
#include <conio.h>
#define MAXROW 10
#define MAXCOL 10

int main()
{
     int arr2d[MAXROW][MAXCOL];
     int i, j, row, col;
     int sumRow[MAXROW] = {0,0,0,0,0,0,0,0,0,0};
     int sumCol[MAXCOL] = {0,0,0,0,0,0,0,0,0,0};
     int totalRow = 0;
     int totalCol = 0;
     system("cls");    
     puts ("Enter the number of rows: "); 
     scanf ("%d", &row);
     puts ("Enter the number of cols: "); 
     scanf ("%d", &col);
     if ((row <= MAXROW) && (col <= MAXCOL))
     {
        printf("Enter %d values: \n", (row * col));
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                scanf("%d", &arr2d[i][j]); 
                totalRow += arr2d[i][j];  
            }
            sumRow[i] = totalRow;
            totalRow = 0;
        } 
        // getting the sum of cols
        for (j = 0; j < col; j++)
        {
            for (i = 0; i < row; i++)
            {
                 totalCol += arr2d[i][j]; 
            }
            sumCol[j] = totalCol;
            totalCol = 0;
        }
        puts("Matrix: \n");
        // printing
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                printf("\t%d\t", arr2d[i][j]);
            }

            printf("\t=%d", sumRow[i]);
            printf("\n");
        }

        for (i = 0; i < col; i++)
        {

            printf("\t=%d\t", sumCol[i]);
        }    
        puts ("\n");
     }
     else
     {
         puts ("row and / or col has exceeded the maximum value.");    
     }

     system("pause"); 
 }

==================================================================
THIS IS MY CODE IN JAVA

import java.io.*; 
public class arr2d 
{

    public static void main(String[] args)throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int maxr = 10, maxc = 10;
        int[][] arr2d = new int[maxr][maxc];
        int x,y,row,col,trow= 0,tcol = 0;
        int[] sumCol = {0,0,0,0,0,0,0,0,0,0};
        int[] sumRow = {0,0,0,0,0,0,0,0,0,0};
        System.out.print("Enter the number of rows: "); row = Integer.parseInt(br.readLine());
        System.out.print("Enter the number of cols: "); col = Integer.parseInt(br.readLine());
        //process
        if( (row <= maxr) && (col <= maxc))
        {
            System.out.println("Enter "+ row*col+ " values");
            for(x=0; x<row; x++)// i think i have the problem here
            {
                for(y=0; y<col; y++)
                {
                    arr2d[x][y] = Integer.parseInt(br.readLine());
                    trow += arr2d[x][y];
                }
                sumRow[x] = trow;
                trow = 0;

            }
            for(y=0; y<col; y++)
            {
                for(x=0; x<row; x++)
                {
                    tcol += arr2d[x][y];

                }
                sumCol[y] = tcol;
            }
Enter the number of rows: 2 
Enter the number of cols: 2 
Enter 4 values 
1 
2 after i enter the 2nd value this what happens
Scanner s = new Scanner(System.in);
System.out.println("Enter input :");
int text= s.nextInt();