Lesson 3 homework solution
This commit is contained in:
60
src/main/java/com/algos/vtb/exceptions/Main.java
Normal file
60
src/main/java/com/algos/vtb/exceptions/Main.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.algos.vtb.exceptions;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
String[][] myArray = new String[][]{
|
||||
{"1", "2", "3", "4"},
|
||||
{"5", "6", "7b", "8"},
|
||||
{"9", "10", "5", "12"},
|
||||
{"13", "14", "15", "16"}
|
||||
};
|
||||
try {
|
||||
System.out.printf("Cумма = %d%n", getSum(myArray));
|
||||
} catch (MyArrayException e) {
|
||||
System.out.println("Exception processed");
|
||||
}
|
||||
}
|
||||
|
||||
private static int getSum(String[][] myArray) throws MyArraySizeException, MyArrayDataException {
|
||||
|
||||
if (myArray.length != 4) {
|
||||
throw new MyArraySizeException("Размер массива должен быть 4x4!");
|
||||
}
|
||||
|
||||
// for (int i = 0; i < myArray.length; i++) {
|
||||
// if (myArray[i].length != 4) {
|
||||
// throw new MyArraySizeException(i);
|
||||
// }
|
||||
// }
|
||||
|
||||
// IntStream.range(0, myArray.length)
|
||||
// .forEach(i -> {
|
||||
// if (myArray[i].length != 4) {
|
||||
// throw new MyArraySizeException(i);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
IntStream.range(0, myArray.length)
|
||||
.filter(i -> myArray[i].length != 4)
|
||||
.findFirst()
|
||||
.ifPresent(i -> {
|
||||
throw new MyArraySizeException(i);
|
||||
});
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < myArray.length; i++) {
|
||||
for (int j = 0; j < myArray[i].length; j++) {
|
||||
try {
|
||||
sum += Integer.parseInt(myArray[i][j]);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new MyArrayDataException(i, j, myArray);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.algos.vtb.exceptions;
|
||||
|
||||
public class MyArrayDataException extends MyArrayException {
|
||||
|
||||
private int row;
|
||||
|
||||
private int col;
|
||||
|
||||
private Object value;
|
||||
|
||||
public MyArrayDataException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyArrayDataException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public MyArrayDataException(int row, int col, Object[][] array) {
|
||||
super("Неверные данные в массиве [" + row + "][" + col + "]: " + array[row][col]);
|
||||
this.row = row;
|
||||
this.col = col;
|
||||
this.value = array[row][col];
|
||||
}
|
||||
}
|
||||
10
src/main/java/com/algos/vtb/exceptions/MyArrayException.java
Normal file
10
src/main/java/com/algos/vtb/exceptions/MyArrayException.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.algos.vtb.exceptions;
|
||||
|
||||
public class MyArrayException extends RuntimeException{
|
||||
public MyArrayException() {
|
||||
}
|
||||
|
||||
public MyArrayException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.algos.vtb.exceptions;
|
||||
|
||||
public class MyArraySizeException extends MyArrayException {
|
||||
|
||||
int row;
|
||||
|
||||
public MyArraySizeException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyArraySizeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public MyArraySizeException(int row) {
|
||||
super(String.format("Размер массива в строке %d не равен 4", row));
|
||||
this.row = row;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user