Created
February 20, 2017 08:34
-
-
Save JonathanParser/126b5aca103729077900bcbee2ae6cc7 to your computer and use it in GitHub Desktop.
Geekbrains Java 2
Урок 2. Исключения
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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