These are the details of my project:
Anagrams: The aim of this project is to create a game in which the user is presented with
an anagram of a word and has to guess the right word within a limited number of
attempts.
Features of the Project:
- The user is given a fixed number of attempts to guess the correct word. The number of attempts is dependent on the length of the word.
- After each incorrect attempt the user is provided with a hint of the correct word.
- If the user is not able to guess the right word within the fixed number of attempts the correct word is displayed and the game moves on to the next word.
- Give controls for exiting the game.
Problems I am facing:
I was able to take a random word from an array of strings but wasn't able to compare that to the output as the output is a normal string. I would like to know how to fix this and also how should I proceed further, I don't need the answer for the hint part just want to know how can I compare 2 types of strings.
import java.util.Arrays;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class main {
String A = "words[index]";
static boolean isAnagram(String A, String B) {
if (A.length() != B.length()) {
return false;
} else {
char a[] = A.toLowerCase().toCharArray();
char b[] = A.toLowerCase().toCharArray();
Arrays.sort(a);
Arrays.sort(b);
String sortedA = String.valueOf(a);
String sortedB = String.valueOf(B);
if (sortedA.equals(sortedB)) {
}
}
return false;
}
public static void main(String[] args) {
String[] words = {"Rat", "Car", "Below", "Taste", "Cried", "Study", "Thing", "Chin"};
Random random = new Random();
int index = random.nextInt(words.length);
System.out.println("The given word is: " + words[index]);
Scanner sc = new Scanner(System.in);
String B = sc.next();
if (isAnagram(String A, String B)) {
System.out.println("not an angram");
} else {
System.out.println("Sucess");
}
}
}