Advertisement
Didart

Hogwarts

Dec 5th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Hogwarts {
  4.  
  5.     private static boolean isValid(int index, String spell) {
  6.         return (index >= 0 && index < spell.length());
  7.     }
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         String spell = scanner.nextLine();
  13.  
  14.         String input = scanner.nextLine();
  15.         while (!input.equals("Abracadabra")) {
  16.             String[] data = input.split(" ");
  17.             String command = data[0];
  18.  
  19.             switch (command) {
  20.                 case "Abjuration":
  21.                     spell = spell.toUpperCase();
  22.                     System.out.println(spell);
  23.                     break;
  24.                 case "Necromancy":
  25.                     spell = spell.toLowerCase();
  26.                     System.out.println(spell);
  27.                     break;
  28.                 case "Illusion":
  29.                     int index = Integer.parseInt(data[1]);
  30.                     char letter = data[2].charAt(0);
  31.  
  32.                     if (isValid(index, spell)) {
  33.                         char[] spellArrayChar = spell.toCharArray();
  34.                         spellArrayChar[index] = letter;
  35.                         spell = String.valueOf(spellArrayChar);
  36.                         System.out.println("Done!");
  37.                     } else {
  38.                         System.out.println("The spell was too weak.");
  39.                     }
  40.                     break;
  41.                 case "Divination":
  42.                     String firstSubstring = data[1];
  43.                     String secondSubstring = data[2];
  44.  
  45.                     spell = spell.replaceAll(firstSubstring, secondSubstring);
  46.                     System.out.println(spell);
  47.                     break;
  48.                 case "Alteration":
  49.                     String subString = data[1];
  50.                     spell = spell.replaceAll(subString, "");
  51.                     System.out.println(spell);
  52.                     break;
  53.                 default:
  54.                     System.out.println("The spell did not work!");
  55.             }
  56.             input = scanner.nextLine();
  57.         }
  58.     }
  59. }
  60.  
  61.  
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement