Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Hogwarts {
- private static boolean isValid(int index, String spell) {
- return (index >= 0 && index < spell.length());
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String spell = scanner.nextLine();
- String input = scanner.nextLine();
- while (!input.equals("Abracadabra")) {
- String[] data = input.split(" ");
- String command = data[0];
- switch (command) {
- case "Abjuration":
- spell = spell.toUpperCase();
- System.out.println(spell);
- break;
- case "Necromancy":
- spell = spell.toLowerCase();
- System.out.println(spell);
- break;
- case "Illusion":
- int index = Integer.parseInt(data[1]);
- char letter = data[2].charAt(0);
- if (isValid(index, spell)) {
- char[] spellArrayChar = spell.toCharArray();
- spellArrayChar[index] = letter;
- spell = String.valueOf(spellArrayChar);
- System.out.println("Done!");
- } else {
- System.out.println("The spell was too weak.");
- }
- break;
- case "Divination":
- String firstSubstring = data[1];
- String secondSubstring = data[2];
- spell = spell.replaceAll(firstSubstring, secondSubstring);
- System.out.println(spell);
- break;
- case "Alteration":
- String subString = data[1];
- spell = spell.replaceAll(subString, "");
- System.out.println(spell);
- break;
- default:
- System.out.println("The spell did not work!");
- }
- input = scanner.nextLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement