-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
57 lines (44 loc) · 1.61 KB
/
Main.java
File metadata and controls
57 lines (44 loc) · 1.61 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
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String str = "500 3 1 2\n2 3 2 2\n5 6 7 1\n300 3 1 0";
int summa = 0;
String[][] arr = null;
try {
arr = strToArray(str);
summa = sum(arr);
System.out.println(summa);
}catch (SizeExeption e){
System.out.println("Матрица имела неверный размер!");
}catch (NumberFormatException e){
System.out.println("Один из элементов не является числом!");
}
}
public static String[][] strToArray (String source) throws SizeExeption{
String[][] returnedArray = new String[4][4];
String[] array_splitted = source.split("\n");
if (array_splitted.length != 4) {
throw new SizeExeption("Matrix size incorrect!");
}
for (int i = 0; i < array_splitted.length; i++){
String[] row = array_splitted[i].split("\\s");
if (row.length != 4){
throw new SizeExeption("Matrix size incorrect!");
}else{
for (int j = 0; j < row.length; j++) {
returnedArray[i][j] = row[j];
}
}
}
return returnedArray;
}
public static int sum(String[][] source) throws NumberFormatException{
int result = 0;
for (int i = 0; i < source.length; i++) {
for (int j = 0; j < source.length; j++) {
result+= Integer.parseInt(source[i][j]);
}
}
return result / 2;
}
}