Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Username {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- StringBuilder username = new StringBuilder(scanner.nextLine());
- String input = scanner.nextLine();
- while (!input.equals("Registration")){
- String command = input.split(" ")[0];
- switch (command){
- case "Letters":
- String option = input.split(" ")[1];
- if (option.equals("Lower")){
- username = new StringBuilder (username.toString().toLowerCase());
- } else if (option.equals("Upper")){
- username = new StringBuilder (username.toString().toUpperCase());
- }
- System.out.println(username);
- break;
- case "Reverse":
- int startIndex = Integer.parseInt(input.split(" ")[1]);
- int endIndex = Integer.parseInt(input.split(" ")[2]);
- if (isValidIndex(startIndex,username) && isValidIndex(endIndex, username)){
- System.out.println(new StringBuilder(username.subSequence(startIndex, endIndex+1)).reverse());
- }
- break;
- case "Substring":
- String substring = input.split(" ")[1];
- if (username.toString().contains(substring)){
- username = new StringBuilder (username.toString().replace(substring, ""));
- System.out.println(username);
- } else {
- System.out.printf("The username %s doesn't contain %s.%n",username,substring);
- }
- break;
- case "Replace":
- char character = input.split(" ")[1].charAt(0);
- username = new StringBuilder (username.toString().replaceAll(String.valueOf(character), "-"));
- System.out.println(username);
- break;
- case "IsValid":
- character = input.split(" ")[1].charAt(0);
- if (username.toString().contains(String.valueOf(character))){
- System.out.println("Valid username");
- } else {
- System.out.printf("%c must be contained in your username.%n",character);
- }
- break;
- }
- input = scanner.nextLine();
- }
- }
- public static boolean isValidIndex(int index, StringBuilder username){
- return index >= 0 && index < username.length();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment