Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package final_exam_prep;
- import java.util.Scanner;
- public class Username {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- String command = scanner.nextLine();
- while (!command.equals("Sign up")) {
- String[] commands = command.split("\\s+");
- switch (commands[0]) {
- case "Case": {
- if (commands[1].equals("upper")) {
- input = input.toUpperCase();
- System.out.println(input);
- } else if (commands[1].equals("lower")){
- input = input.toLowerCase();
- System.out.println(input);
- } else {
- continue;
- }
- }
- break;
- case "Reverse": {
- int start = Integer.parseInt(commands[1]);
- int end = Integer.parseInt(commands[2]);
- String substring = "";
- if (notOutOfBounds(start, end + 1, input.length())) {
- substring = input.substring(start, end + 1);
- StringBuilder sb = new StringBuilder();
- for (int i = substring.length() - 1; i >= 0; i--) {
- String letter = String.valueOf(substring.charAt(i));
- sb.append(letter);
- }
- System.out.println(sb.toString());
- }
- }
- break;
- case "Cut": {
- String substring = commands[1];
- if (input.contains(substring)) {
- System.out.println(input.replace(substring, ""));
- } else {
- System.out.printf("The word %s doesn't contain %s.%n", input, substring);
- }
- }
- break;
- case "Replace": {
- char symbol = commands[1].charAt(0);
- input = input.replaceAll(String.valueOf(symbol), "*");
- System.out.println(input);
- }
- break;
- case "Check": {
- char symbol = commands[1].charAt(0);
- if (input.contains(String.valueOf(symbol))) {
- System.out.println("Valid");
- } else {
- System.out.printf("Your username must contain %c.%n", symbol);
- }
- }
- break;
- }
- command = scanner.nextLine();
- }
- }
- public static boolean notOutOfBounds(int index, int index2, int size) {
- if (index >= 0 && index < size && index2 >= 0 && index2 < size) {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement