Advertisement
Guest User

Password Reset

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