-8
#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;
            }

i think i have a problem on the process i can only enter 2 integers in the 2d array i cant fill all the slot in the array this is what compiler says

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

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:504) at java.lang.Integer.parseInt(Integer.java:527) at arr2d.main(arr2d.java:31)

Trick
  • 9
  • 3
  • possible duplicate of [Creating Two-Dimensional Array](http://stackoverflow.com/questions/12231453/creating-two-dimensional-array) – duffymo Nov 27 '13 at 13:39
  • Do you know what a `NumberFormatException` is? It might help to look it up. – keyser Nov 27 '13 at 13:39
  • 2
    `java.lang.NumberFormatException: For input string: ""` the message is pretty clear: you're trying to convert an empty string to a number, which can't succeed. Either check whether the string is empty, catch the exception and handle accordingly, or check your code why the string is empty (if it is not user-entered). – Thomas Nov 27 '13 at 13:39
  • Its because the input string you got from br.readLine() was empty i.e. "". This must have happened when you just press 'enter' key without entering number. Instead add a check to see if the value entered is something, and not null. You will have to handle situations also to check if the input is a number and not some text.. Good luck – Arunkumar Nov 27 '13 at 13:41

3 Answers3

0

Just try to use Scanner class instead of BufferedReader as sometime when you enter empty string by mistake then you get NumberFormatExceptionfrom Integer.parseInt.
Example,

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

Scanner class has inbuilt function which can wait until some desired primitive type value is entered.
Note the inbuilt functions like nextInt, nextByte etc may throw InputMismatchException if you are entering non-numeric values when a numeric value is needed.

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
0

You got a NumberFormatException because you typed an empty string "" instead of an Integer and this empty String cannot be parsed.

Your program runs fine. I only get the exception when I press Enter without putting any input (in other words passing as input an empty string).

Artem Tsikiridis
  • 682
  • 6
  • 11
0

I see nothing wrong I just ran it through and everything worked. I used your exact test data as well. I'm assuming you just put in wrong data.

Mason T.
  • 1,567
  • 1
  • 14
  • 33