Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions src/homeworks/homework2/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package homeworks.homework2;

public class Main {
public static void main(String[] args) {

try {
System.out.println("Сумма всех элементов массива = " + arrayGetSum(arraySizeException));
} catch (MyArraySizeException | MyArrayDataException e) {
System.out.println(e.getMessage());
}

try {
System.out.println("Сумма всех элементов массива = " + arrayGetSum(arrayDataException));
} catch (MyArraySizeException | MyArrayDataException e) {
System.out.println(e.getMessage());
}

try {
System.out.println("Сумма всех элементов массива = " + arrayGetSum(fineArray));
} catch (MyArraySizeException | MyArrayDataException e) {
System.out.println(e.getMessage());
}

}
private static String[][] arraySizeException = {{"1","2","3","4"},{"1","2","3","4"},{"1","2","3","4"},{"1","2","3","4"}, {"1","2","3","4"}};
private static String[][] arrayDataException = {{"1","2","3","4"},{"1","2","3","4"},{"1","2","3","4"},{"1","ЩЩЩЩЩ","3","4"}};
private static String[][] fineArray = {{"1","2","3","4"},{"1","2","3","4"},{"1","2","3","4"},{"1","2","3","4"}};

static void checkArraySize(String arr[][]) throws MyArraySizeException{
if(arr.length != 4 || arr[0].length !=4){
throw new MyArraySizeException();
}
}
static int arrayGetSum(String arr[][]) throws MyArraySizeException, MyArrayDataException {
checkArraySize(arr);
int sum = 0;
int i = 0;
int j = 0;
try {
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[i].length; j++) {
sum = sum + Integer.parseInt(arr[i][j]);
}
}
} catch (NumberFormatException e) {
throw new MyArrayDataException("Ошибка массива в позиции " + i + "," + j);
}
return sum;
}
}
7 changes: 7 additions & 0 deletions src/homeworks/homework2/MyArrayDataException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package homeworks.homework2;

public class MyArrayDataException extends Exception{
public MyArrayDataException(String message){
super(message);
}
}
7 changes: 7 additions & 0 deletions src/homeworks/homework2/MyArraySizeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package homeworks.homework2;

public class MyArraySizeException extends Exception{
public MyArraySizeException(){
super("Ошибка размера массива");
}
}