Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- //package inputHandler;
- import java.util.Scanner;
- import java.util.Random;
- public class Main {
- private static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- // Main menu (new text, new number and exit) message if you choose to exit.
- String userInput;
- //String remove;
- while (true) {
- System.out.println("Please enter 'new text', 'new number' or 'exit'."); //prompt user for input
- userInput = sc.nextLine(); //get input
- switch (userInput) {
- case "new text":
- newText(); //call the method newText
- break;
- case "new number":
- newNumber(); //call the method newNumber
- break;
- case "exit":
- System.exit(0); //exit program
- break;
- default:
- System.out.println("Invalid input"); //print message to user
- }
- }
- }
- private static void newText () {
- // loops until breaks.
- while (true) {
- System.out.println("Please choose between following: remove char, text value, randomize or write return to get back to main menu.");
- // Wating to get user input.
- String userInput = sc.nextLine();
- // Choosing between options from user input.
- switch (userInput) {
- case "remove char":
- System.out.println("Please enter a line of text");
- userInput = sc.nextLine();
- System.out.println("Please enter the char you want removed");
- String remove = sc.nextLine();
- System.out.println(removeChar(userInput, remove));
- break;
- case "text value":
- System.out.println("Please enter a line of text");
- userInput = sc.nextLine();
- System.out.println("Your text value is " + textValue(userInput));
- break;
- case "randomize":
- System.out.println("Please enter a line of text");
- userInput = sc.nextLine();
- randomize(userInput);
- break;
- case "return":
- //Back to menu
- break;
- // if no result, write text to user.
- default:
- System.out.println("Please enter valid input");
- }
- }
- }
- //Method removeChar() this method replaces one char with another.
- private static String removeChar (String userInput, String remove){
- return userInput.replace(remove, "");
- }
- private static int textValue(String userInput) {
- int value = 0;
- // loops trough all chars in the string.
- for (char c : userInput.toCharArray()) {
- // Failsafe, if user input is not any of english chars, break.
- if (getValueFromChar2(Character.toLowerCase(c)) < 0) {
- System.out.println("Input error. " + c + " do not return any points");
- break;
- }
- // adds for each char to int value.
- else {
- value += getValueFromChar2(Character.toLowerCase(c));
- }
- }
- return value;
- }
- //Method randomize() this method shuffles characters within a string.
- private static String randomize (String userInput){
- char[] characters = userInput.toCharArray(); //Converts the string to an array of chars.
- for (int i = 0; i < characters.length; i++) { //Replaces each char with a random one within the original string
- int randomIndex = randomWithRange(characters.length);
- char temp = characters[i];
- characters[i] = characters[randomIndex];
- characters[randomIndex] = temp;
- }
- return new String(characters); //Returns a string created from the chars
- }
- private static void newNumber() {
- // loops until breaks.
- while (true) {
- System.out.println("Please choose between following: remove char, text value, randomize or write return to get back to main menu.");
- // Waiting to get user input.
- String userInput = sc.nextLine();
- // Choosing between options from user input.
- switch (userInput) {
- case "get random number":
- System.out.println("Please write the bound of the number you want to random");
- // waiting for user input.
- userInput = sc.nextLine();
- System.out.println("Your random number is " + randomWithRange(Integer.parseInt(userInput)));
- break;
- case "print plus":
- //TODO add text value function
- break;
- case "create calculator":
- // TODO add randomzie function
- break;
- case "return":
- // Breaking the loop.
- break;
- // if no result, write text to user.
- default:
- System.out.println("Please enter valid input");
- }
- }
- }
- //Metoden printPlus - använd randommetoden som Ludvig gör
- public static int printPlus ( int userInput){
- String s = "";
- while (true) {
- System.out.println("Do you want to print a specific number or a random one? Please enter 'Specific' or 'Random'");
- String input = sc.nextLine(); //Get user input to determine whether to randomize or not
- if (input.equals("Specific")) {
- int userNumber = sc.nextInt(); //Get a specific number from the user
- for (int i = 0; i <= userNumber; i++) {
- s = s + "+";
- System.out.println(s);
- }
- } else if (input.equals("Random")) {
- int range = 10;
- double d = randomWithRange(range); //call the method getRandomNumber(int range)
- for (int i = 0; i <= d; i++) { //loop the random number
- s = s + "+";
- System.out.println(s); //print the plus
- }
- }
- }
- }
- private static int getValueFromChar2(char c) {
- // Making the string of the alphabet into chars in an array, starting with a space to make this value to zero.
- char alpa[] = " abcdefghijklmnopqrstuvwxyz".toCharArray();
- int atChar = 0;
- // looping the array and give +1 value at atChar each time the array is not the c value.
- for (atChar = 0; alpa[atChar] != c; atChar++) {
- if (atChar > alpa.length) {
- // the char value do not exist, returning -1 to make you see the error.
- return -1;
- }
- }
- return atChar;
- }
- private static int randomWithRange ( int bound){
- Random newRandom = new Random();
- int myNumber = newRandom.nextInt(bound);
- return myNumber;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement