Advertisement
Guest User

Java 2.0

a guest
Apr 4th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package com.company.softuni;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main2 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String password = scanner.nextLine();
  10.         String command = scanner.nextLine();
  11.  
  12.         while (!command.equals("Done")) {
  13.             String[] tokens = command.split("\\s+");
  14.             String commandName = tokens[0];
  15.  
  16.             switch (commandName) {
  17.                 case "TakeOdd":
  18.                     // Takes only the characters at odd indices and concatenates them together to obtain the new raw password and then prints it.
  19.                     StringBuilder newPass = new StringBuilder();
  20.                     for (int i = 0; i < password.length(); i++) {
  21.                         if (i % 2 == 1) {
  22.                             newPass.append(password.charAt(i));
  23.                         }
  24.                     }
  25.                     password = newPass.toString();
  26.                     System.out.println(password);
  27.                     break;
  28.  
  29.                 case "Cut":
  30.                     int index = Integer.parseInt(tokens[1]);
  31.                     int length = Integer.parseInt(tokens[2]);
  32.  
  33.                     String substr = password.substring(index, index + length);
  34.                     password = password.replace(substr, "");
  35.                     System.out.println(password);
  36.                     break;
  37.  
  38.                 case "Substitute":
  39.                     String symbol = tokens[1];
  40.                     String replacement = tokens[2];
  41.  
  42.                     if (password.contains(symbol)) {
  43.                         password = password.replace(symbol, replacement);
  44.                         System.out.println(password);
  45.                     } else {
  46.                         System.out.println("Nothing to replace!");
  47.                     }
  48.                     break;
  49.             }
  50.  
  51.             command = scanner.nextLine();
  52.         }
  53.         System.out.printf("Your password is: %s", password);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement