yubaba.java
import java.util.Random;
import java.util.Scanner;
public class yubaba {
public static void main(String[] arts){
System.out.println("It's a contract. Write your name there.");
Scanner keiyakusho = new Scanner(System.in);
String name = keiyakusho.nextLine();
Yubaba(name);
}
private static void Yubaba(String name) {
try {
System.out.println("Hung."+name +"I mean. It's a luxurious name.");
Random random = new Random();
int newNameIndex = random.nextInt(name.length());
String newName = name.substring(newNameIndex,newNameIndex+1);
System.out.println("From now on your name is"+newName+"It is. Mind you,"+newName+"That's right. I'll reply when I understand"+newName+"!!");
}catch(Exception e) {
System.out.println("Thank you Chihiro. my name is" + e);
System.out.println("I remember when you put null in me and fell. You tried to pick up the error");
}
}
}
I split Yubaba a little. An error occurs when the poster of the original article inputs. This is a slightly improved version of the problem. When null is entered in the string input, I remember that Haku's real name was IllegalArgumentException.
trycatch.java
try {
//Processing that is likely to cause an error
}catch(Exception e) {
//What to do if an error occurs
}finally{
//The final process to be executed regardless of whether an error occurs or not
}
It looks like this. (I won't write in detail, but I hope it will be the starting point for the error ...)
Recommended Posts