-4

It's Java code that I translated from C code, and I'm having an error while parsing.

My error is in line 19. Maybe I have more than one error.

Input:

3 3
1 2 2
3 5 6
8 8 9

Output:

YES
The amount of money won is: 13.00

Code:

import java.util.*;
public class lottary {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int rows = scan.nextInt();
        int cols = scan.nextInt();
        int[][] arr = new int[rows][cols];

        for (int r = 0; r < rows; r++) {

            var line = scan.nextLine().split(" ");
            int c = 0;
            for (var item : line)


                arr[r][c++] = Integer.parseInt(((item)));//My error is from here
        }

        double d1 = 0, d2 = 0;
        for (int k = 0; k < rows; k++) d1 += arr[k][k];
        for (int k = 0; k < rows; k++) d2 += arr[k][rows - k - 1];


        double s1 = 0, s2 = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                if (col > row) s1 += arr[row][col];
                if (col < row) s2 += arr[row][col];
            }
        }
        if (d1 == d2 && s1 % 2 == 0 && s2 % 2 != 0)
            System.out.println("Yes");
        else {
            System.out.println("No");
            return;
        }
        double sd = 0;
        for (int k = 0; k < rows; k++)
            if (arr[k][k] % 2 == 0) sd += arr[k][k];


        double sr = 0;
        for (int k = 0; k < cols; k++) {
            if (arr[0][k] % 2 == 0) sr += arr[0][k];
            if (arr[rows - 1][k] % 2 == 0) sr += arr[rows - 1][k];
        }
        // 
        double sc = 0;
        for (int k = 0; k < rows; k++) {
            if (arr[k][0] % 2 != 0) sc += arr[k][0];
            if (arr[k][cols - 1] % 2 != 0) sc += arr[k][cols - 1];
        }
        double avr = (s2 + sd + sr + sc) / 4.0;
        System.out.printf("The amount of money won is: {0:f2}", avr);
    }
}
khelwood
  • 46,621
  • 12
  • 59
  • 83
  • 2
    What is the error and which line is 19? – takendarkk May 23 '21 at 11:47
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – tevemadar May 23 '21 at 12:19

0 Answers0