From 6dc0b549f69e7b9eb475fe50d61e30a4aff3c9de Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 11 Oct 2020 08:46:53 +0100 Subject: [PATCH] Added package declaration --- Personal/Anagram/src/AnagramGenerator.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Personal/Anagram/src/AnagramGenerator.java diff --git a/Personal/Anagram/src/AnagramGenerator.java b/Personal/Anagram/src/AnagramGenerator.java new file mode 100644 index 0000000..1be1c30 --- /dev/null +++ b/Personal/Anagram/src/AnagramGenerator.java @@ -0,0 +1,48 @@ +package src; + +import java.util.Scanner; +import java.util.ArrayList; +import java.util.Random; + +public class AnagramGenerator { + + static ArrayList getStringList(String userWord) { + /* Return a character list of the string input by the User */ + + // Define fixed length array of the same length as the word + ArrayList charList = new ArrayList<>(); + + // Add each character to the list of characters + for (int i = 0; i < userWord.length(); i++) + charList.add(userWord.charAt(i)); + + return charList; + } + + static String generateAnagram(ArrayList charList) { + /* Returning an Anagram of the word input by the User */ + + StringBuilder stringBuilder = new StringBuilder(); + + while (!charList.isEmpty()) { + int randomIndex = new Random().nextInt(charList.size()); + stringBuilder.append(charList.get(randomIndex)); + charList.remove(charList.get(randomIndex)); + } + return stringBuilder.toString(); + } + + public static void main(String[] args) { + /* Let the user enter in a word and generate an Anagram from it*/ + + Scanner scan = new Scanner(System.in); + System.out.println("Please enter in a word that you would like to see an Anagram of"); + String userWord = scan.next(); + + ArrayList charList = getStringList(userWord); + String finalAnagram = generateAnagram(charList); + + System.out.println("Your Anagram: " + finalAnagram); + scan.close(); + } +}