【问题标题】:I want to convert this C code to java code but im having a issue in inputting in array我想将此 C 代码转换为 java 代码,但我在输入数组时遇到问题
【发布时间】:2013-12-13 04:26:02
【问题描述】:
#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;
            }

我认为我的过程有问题我只能在二维数组中输入 2 个整数我无法填充数组中的所有槽这是编译器所说的

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

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

【问题讨论】:

  • Creating Two-Dimensional Array 的可能重复项
  • 你知道NumberFormatException 是什么吗?查找它可能会有所帮助。
  • java.lang.NumberFormatException: For input string: "" 消息很清楚:您正在尝试将空字符串转换为数字,但无法成功。检查字符串是否为空,捕获异常并进行相应处理,或者检查您的代码为什么字符串为空(如果它不是用户输入的)。
  • 这是因为您从 br.readLine() 获得的输入字符串为空,即“”。当您只按“输入”键而不输入数字时,一定会发生这种情况。而是添加一个检查以查看输入的值是否为某个值,而不是 null。您还必须处理情况以检查输入是否是数字而不是文本。祝你好运

标签: java c arrays


【解决方案1】:

您收到了 NumberFormatException,因为您键入了一个空字符串 "" 而不是整数,并且无法解析这个空字符串。

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

【讨论】:

  • 感谢先生的信息,我应该研究 NumberFormatException
  • @Trick 不客气!如果您认为我的回答对您有所帮助,则应该投票。 :) 干杯。
【解决方案2】:

我没有发现任何问题,我只是运行它并且一切正常。我也使用了您的确切测试数据。我假设你只是输入了错误的数据。

【讨论】:

    【解决方案3】:

    尝试使用Scanner 类而不是BufferedReader,因为有时您错误地输入了empty 字符串,然后您会从Integer.parseInt 得到NumberFormatException。
    例如,

    Scanner s = new Scanner(System.in);
    System.out.println("Enter input :");
    int text= s.nextInt();
    

    Scanner 类具有内置函数,可以等待输入一些所需的原始类型值。
    注意像 nextInt、nextByte 等内置函数可能会抛出 InputMismatchException如果您在需要数值时输入非数值。

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2021-04-05
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      相关资源
      最近更新 更多