Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FinalExam.O2;
- import java.util.Scanner;
- public class O1WorldTour {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- StringBuilder input = new StringBuilder(scanner.nextLine());
- String command = scanner.nextLine();
- while (!command.equals("Travel")) {
- String[] type = command.split(":");
- switch (type[0]) {
- case "Add Stop":
- if (Integer.parseInt(type[1]) >= 0 && Integer.parseInt(type[1]) < input.length()) {
- input.insert(Integer.parseInt(type[1]), type[2]);
- }
- System.out.println(input);
- break;
- case "Remove Stop":
- int startIndex = Integer.parseInt(type[1]);
- int endIndex = Integer.parseInt(type[2]);
- if (startIndex >= 0 && endIndex >= 0 && startIndex < input.length() && endIndex < input.length()) {
- input.delete(startIndex, endIndex + 1);
- }
- System.out.println(input);
- break;
- case "Switch":
- input = new StringBuilder(input.toString().replace(type[1], type[2]));
- System.out.println(input);
- break;
- }
- command = scanner.nextLine();
- }
- System.out.println("Ready for world tour! Planned stops: " + input);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment