Почему валидатор может не принимать решение? Код приложен ниже.
Вывод в консоль:
C:\Users\admin\.jdks\openjdk-15.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\lib\idea_rt.jar=52558:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\bin" -Dfile.encoding=UTF-8 -classpath D:\IDEA_project\JavaRushTasks\out\production\2.JavaCore com.javarush.task.task14.task1419.Solution
java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
at java.base/java.io.FileReader.<init>(FileReader.java:75)
at com.javarush.task.task14.task1419.Solution.initExceptions(Solution.java:53)
at com.javarush.task.task14.task1419.Solution.main(Solution.java:27)
java.lang.UnsupportedOperationException
at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1062)
at com.javarush.task.task14.task1419.Solution.initExceptions(Solution.java:67)
at com.javarush.task.task14.task1419.Solution.main(Solution.java:27)
java.lang.ArithmeticException: / by zero
java.lang.NullPointerException: Cannot invoke "String.trim()" because "s" is null
java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
java.lang.NumberFormatException: For input string: "digital value"
java.io.FileNotFoundException: very_bad_file.txt (The system cannot find the file specified)
java.lang.ArrayStoreException: java.lang.Integer
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
java.lang.NegativeArraySizeException: -10
Process finished with exit code 0package com.javarush.task.task14.task1419;
import sun.reflect.annotation.ExceptionProxy;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
Нашествие исключений
*/
/*Нашествие исключений
Заполни список exceptions десятью различными исключениями.
Первое исключение уже реализовано в методе initExceptions.*/
/*Требования:
1. Список exceptions должен содержать 10 элементов.
2. Все элементы списка exceptions должны быть исключениями (потомками класса Throwable).
3. Все элементы списка exceptions должны быть уникальны.
4. Метод initExceptions должен быть статическим.*/
public class Solution {
public static List<Exception> exceptions = new ArrayList<Exception>();
public static void main(String[] args) {
initExceptions();
for (Exception exception : exceptions) {
System.out.println(exception);
}
}
private static void initExceptions() { //the first exception
try {
float i = 1 / 0;
} catch (Exception e) {
exceptions.add(e);
}
//напишите тут ваш код
try {
String s=null;
s.trim();
} catch (NullPointerException e) {
/*1*/ exceptions.add(e);
}
FileReader fr = null;
try {
File file = new File("file.txt");
fr = new FileReader(file); char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); // prints the characters one by one
} catch (IOException e) {
/*2*/exceptions.add(e);
e.printStackTrace();
}
try {
List aList = new ArrayList();
aList.add('a');
aList.add('b');
List newList = Collections.unmodifiableList(aList);
newList.add('c');
} catch (UnsupportedOperationException e) {
e.printStackTrace();
}
/*try {
String s="";
s.charAt(10);
} catch (IndexOutOfBoundsException e) {
*//*2*//* exceptions.add(e);
}*/
/*try {
int [] v = new int[5];
v[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
*//*3*//* exceptions.add(e);
}*/
try {
Object x = new Integer(0);
System.out.println((String)x);
} catch (ClassCastException e) {
/*4*/ exceptions.add(e);
}
try {
String s="digital value";
int a = Integer.parseInt(s);
} catch (NumberFormatException e) {
/*5*/exceptions.add(e);
}
try {
FileInputStream f = new FileInputStream ("very_bad_file.txt");
} catch (FileNotFoundException e) {
/*6*/exceptions.add(e);
}
try {
Object o[] = new String[5];
o[2] = new Integer(200);
} catch (ArrayStoreException e) {
/*7*/exceptions.add(e);
}
String str = "Hello World";
try {
char charAtNegativeIndex = str.charAt(-1); // Trying to access at negative index
char charAtLengthIndex = str.charAt(11); // Trying to access at index equal to size of the string
} catch (StringIndexOutOfBoundsException e) {
/*8*/ exceptions.add(e);
}
try {
int[] v = new int[-10];
} catch (NegativeArraySizeException e) {
/*9*/exceptions.add(e);
}
//напишите тут ваш код
/*try {
int [] numbers = new int[2];
for (int i = 0; i == numbers.length; i++)
numbers[i] = i+1;
} catch (IndexOutOfBoundsException e) {
*//*2*//*exceptions.add(e);
}
try {
int i = 12 / 0;
} catch (ArithmeticException e) {
*//*3*//* exceptions.add(e);
}
FileReader fr = null;
try {
File file = new File("file.txt");
fr = new FileReader(file); char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); // prints the characters one by one
} catch (IOException e) {
*//*4*//*exceptions.add(e);
e.printStackTrace();
}
try {
Object i = Integer.valueOf(42);
String s = (String)i;
} catch (ClassCastException e) {
*//*5*//* exceptions.add(e);
}
try {// Since Double class extends Number class
// only Double type numbers
// can be stored in this array
Number[] a = new Double[2];
// Trying to store an integer value
// in this Double type array
a[0] = new Integer(4);
} catch (ArrayStoreException e) {
*//*6*//* exceptions.add(e);
}
File file = new File("E://file.txt");
try {
FileReader fileReader = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
*//*7*//* exceptions.add(e);
}
try {
String a = null; //null value
System.out.println(a.charAt(0));
}
catch (NullPointerException e) {
*//*8*//*exceptions.add(e);
}
try {
// "string" is not a number
int num = Integer.parseInt ("string") ;
System.out.println(num);
} catch (NumberFormatException e) {
*//*9*//* exceptions.add(e);
}
String str = "Hello World";
try {
char charAtNegativeIndex = str.charAt(-1); // Trying to access at negative index
char charAtLengthIndex = str.charAt(11); // Trying to access at index equal to size of the string
} catch (StringIndexOutOfBoundsException e) {
*//*10*//* exceptions.add(e);
}*/
}
}