Advertisement
NadezhdaGeorgieva

01 Password Reset

Dec 4th, 2020
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package bg.softuni.javafundamentals;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Fin03_20Apr_PasswordReset {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String password = scanner.nextLine();
  9.         String input = scanner.nextLine();
  10.  
  11.         while (!input.equals("Done")){
  12.             String[] tokens = input.split("\\s+");
  13.             switch (tokens[0]){
  14.                 case "TakeOdd":
  15.                     StringBuilder sb = new StringBuilder();
  16.                     for (int i = 0; i < password.length(); i++) {
  17.                         if (i % 2 != 0){
  18.                             sb.append(password.charAt(i));
  19.                         }
  20.                     }
  21.                     password = sb.toString();
  22.                     System.out.println(password);
  23.                     break;
  24.                 case "Cut":
  25.                     int index = Integer.parseInt(tokens[1]);
  26.                     int length = Integer.parseInt(tokens[2]);
  27.                     int endIndexExclusive = index + length;
  28.                    
  29. //                    String firstPart = password.substring(0, index);
  30. //                    String lastPart = password.substring(endIndexExclusive);
  31. //                    password = firstPart + lastPart;
  32.  
  33.                     String substringToRemoveItsFirstOccurrence = password.substring(index, endIndexExclusive);
  34.                     password = password.replace(substringToRemoveItsFirstOccurrence, "");
  35.                     System.out.println(password);
  36.  
  37.                     break;
  38.                 case "Substitute":
  39.                     String substring = tokens[1];
  40.                     String substitute = tokens[2];
  41.  
  42.                     if (password.contains(substring)){
  43.                         password = password.replaceAll(substring, substitute);
  44.                         System.out.println(password);
  45.                     } else {
  46.                         System.out.println("Nothing to replace!");
  47.                     }
  48.                     break;
  49.                 default:
  50.                     System.out.println("Unknown command: " + tokens[0]);
  51.                     break;
  52.             }
  53.             input = scanner.nextLine();
  54.         }
  55.         System.out.printf("Your password is: %s", password);
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement