-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainClass.java
More file actions
77 lines (67 loc) · 2.44 KB
/
Copy pathMainClass.java
File metadata and controls
77 lines (67 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
int row;
int column;
Scanner sc = new Scanner(System.in);
System.out.println("Enter values for a two-dimensional array. Choose the size. Row and Column: ");
row = sc.nextInt();
column = sc.nextInt();
String[][] newArray = new String[row][column];
try {
createArr(newArray, sc, row, column);
// printArr(newArray);
// calcArr(newArray, row, column);
System.out.println("Sum is " + +calcArr(newArray, row, column));
} catch (MyArraySizeException e) {
e.printStackTrace();
} catch (MyArrayDataException e) {
e.printStackTrace();
}
}
private static void createArr(String[][] array, Scanner sc, int row, int column) {
if (row != 4 || column != 4) throw new MyArraySizeException(row, column);
System.out.println("Create Array ");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println("New row " + i + " column " + j);
array[i][j] = sc.next();
}
}
printArr(array);
calcArr(array, row, column);
}
private static int calcArr(String[][] array, int row, int column) {
int sum = 0;
int[][] arrayInt = new int[row][column];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
try {
arrayInt[i][j] = Integer.parseInt(array[i][j]);
sum += arrayInt[i][j];
} catch (NumberFormatException e) {
throw new MyArrayDataException(i, j);
}
}
}
printArrInt(arrayInt);
return sum;
}
private static void printArr(String[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
private static void printArrInt(int[][] array) {
System.out.println("This is arr Int ");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
}