Skip to content

Instantly share code, notes, and snippets.

@JonathanParser
Created February 20, 2017 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanParser/126b5aca103729077900bcbee2ae6cc7 to your computer and use it in GitHub Desktop.
Save JonathanParser/126b5aca103729077900bcbee2ae6cc7 to your computer and use it in GitHub Desktop.
Geekbrains Java 2 Урок 2. Исключения
package com.geegbrainsJava2lesson2;
/**
* Created by Jack Sparrow on 20.02.2017.
*/
public class GenArray {
String data;
String[][] res;
public GenArray(String data) throws NumberFormatException, ArrayIndexOutOfBoundsException {
this.data = data;
res = new String[4][4];
int q = 0;
for (String i : data.split("\n")) res[q++] = i.split("\\s");
return;
}
public int sumdata() {
int sum = 0;
for (int i = 0; i < res.length; i++) {
for (int j = 0; j < res.length; j++) {
sum += Integer.parseInt(res[i][j]);
}
}
return sum / 2;
}
}
package com.geegbrainsJava2lesson2;
/**
* Created by Jack Sparrow on 20.02.2017.
*/
public class Main {
public static void main(String[] args) {
try {
GenArray genArray = new GenArray("1 3 1 2\n2 3 2 2\n5 6 7 1\n3 3 1 0");
System.out.println(genArray.sumdata());
}
catch (NumberFormatException e)
{
System.out.println("В одной из ячеек полученной матрицы не число");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Размер матрицы, полученной из строки, не равен 4x4");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment