Skip to content

Commit 51dbd99

Browse files
authored
Merge pull request mouredev#3743 from worlion/worlion/java/ejercicio10
#10 - Java
2 parents 85c4d42 + c71a1f1 commit 51dbd99

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
4+
public class Worlion {
5+
6+
7+
public static final String GREEN = "\u001B[32m";
8+
public static final String RED = "\u001B[31m";
9+
public static final String RED_BACKGROUND = "\u001B[41m";
10+
public static final String ANSI_RESET = "\u001B[0m";
11+
12+
public static void main(String[] args) {
13+
new Worlion().run();
14+
}
15+
16+
public void run() {
17+
System.out.println("Resultado obtenido: "+div(7, 0));
18+
19+
testArray();
20+
21+
testExtra();
22+
23+
}
24+
/*
25+
* EJERCICIO: Excepciones en Java
26+
*/
27+
28+
public int div(int a, int b) {
29+
System.out.println("\nDividimos "+a+" entre "+ b+":");
30+
int result = 0;
31+
try {
32+
result = a/b;
33+
} catch (ArithmeticException e) {
34+
System.err.println(RED +"ERROR:"+ ANSI_RESET+" You can not divide by 0");
35+
}
36+
return result;
37+
}
38+
39+
public void testArray() {
40+
List<String> list = Arrays.asList(new String[]{"A", "B", "C"});
41+
System.out.println("\nRecorremos la lista "+list);
42+
try{
43+
for(int i = 0; i <= list.size(); i++) {
44+
System.out.println("Position: "+i+" - Value: "+list.get(i));
45+
}
46+
}
47+
catch (ArrayIndexOutOfBoundsException e) {
48+
System.err.println(RED +"ERROR:"+ ANSI_RESET + e);
49+
}
50+
System.out.println("Chimpun!");
51+
}
52+
53+
/* DIFICULTAD EXTRA (opcional):
54+
*/
55+
56+
class myCustomException extends Exception {
57+
public myCustomException(String errorMessage) {
58+
super(errorMessage);
59+
}
60+
}
61+
62+
private boolean isVocal(char c){
63+
return c == 'a' ||c == 'e' ||c == 'i' ||c == 'o' ||c == 'u';
64+
}
65+
66+
private char exceptionsFunction(String s, int num) throws Exception {
67+
68+
69+
if(s.isEmpty()){
70+
throw new myCustomException("The string must be not empty");
71+
}
72+
char c = s.charAt(num);
73+
if(isVocal(c)){
74+
throw new Exception("Odio las vocales!!");
75+
}
76+
return s.charAt(num);
77+
}
78+
79+
private void testExtra() {
80+
System.out.println(" \n ---- 🌩 DIFICULTAD EXTRA 🌩 ----\n");
81+
82+
String[] strings = {"","patata", "carcamusa", "ascapatipucyuelaimepipedo", "choripan"};
83+
int[] indexes = {2,-1,20, 11, 2};
84+
int errorCount = 0;
85+
86+
for (int i = 0; i < strings.length; i++) {
87+
try {
88+
System.out.println("Word: " + strings[i] + " - Index: "+ indexes[i] + " - CharAt: "+exceptionsFunction(strings[i], indexes[i]));
89+
}
90+
catch (myCustomException e){
91+
System.err.println(RED +"ERROR - MY CUSTOM EXCEPTION: "+ ANSI_RESET + e);
92+
errorCount++;
93+
}
94+
catch (StringIndexOutOfBoundsException e){
95+
System.err.println(RED +"ERROR - NOS HEMOS SALIDO :(: "+ ANSI_RESET + e);
96+
errorCount++;
97+
}
98+
catch (Exception e) {
99+
System.err.println(RED +"ERROR - OTRAS: "+ ANSI_RESET + e);
100+
errorCount++;
101+
}
102+
}
103+
System.out.println("Ejecución terminada. Se han producido (y recuperado) "+errorCount+" errores");
104+
}
105+
}

0 commit comments

Comments
 (0)