Avoid Yubaba's error in Java

Introduction This article https://qiita.com/Nemesis/items/c7192a7c510788d2cba2 This is plagiarism. sorry. Then this article could be a spoiler for the movie "Spirited Away". Only people who say "OK!" Should go down.

Code

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.

What is try-catch? In short, try-catch is like "If you do a process that is likely to cause an error in try and throw an error, you will go into catch". Processing that is likely to cause an error is, for example, inputting a character string, FilleWriter, or DB connection. There is also finally, which is code that runs with or without an error.

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 ...)

Finally I'm sorry for the omission article. I'm sorry for the author of the original article. I did it because I was a little bored studying Java Silver ... 11/05 Fixed Yubaba method nicely. By calling this method, it is possible to make the name one letter at any time.

Recommended Posts

Avoid Yubaba's error in Java
Partization in Java
Today's java error
Changes in Java 11
Rock-paper-scissors in Java
java error countermeasures
Avoid character code error in java when using VScode extension RUN-CODE
Pi in Java
FizzBuzz in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Java Math.sqrt Error Pit
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Error handling in Graphql-ruby
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Confront Java Floating Point Error
Try using RocksDB in Java
Read binary files in Java 1
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
NameError in Income # index error
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
java setter getter error resolution
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.