0

I present three random letters to the user, lets say ' J U D ' and my user has to give a word for each letter starting with the assigned random letter, for example:

"Just Use Data".

I have been unsuccessful searching for how to validate that the user's input starts with the assigned random letter.

My code

public static void generateLetters(){
    Random randomLetter = new Random();
    int i;
    char[] letter = {'A', 'B', 'C', 'D', 'E', 'F'};
        // It's A-Z, but for typing sake... you get the point
    char[] letters = new char[26];
        for(i = 0; i <= 3; i++){
            letters[i] = letter[randomLetter.nextInt(26)];
            System.out.printf(letters[i] + "   ");
        }
}

The above block will generate letters randomly and works fine. Below, I'll enter basically what I have for the user input.

public static String[] getWord(String[] word){
    System.out.println("Enter a word for your letters: ");
    Scanner wordInput = new Scanner(System.in);
    String inputWord = wordInput.nextLine().toUpperCase();
    return word;
}

Simple thus far and this is my main function:

public statc void main(String[] args){
    generateLetters();
    getWord();
}

I need to take the string returned from getWord() and verify that each word input does begin with the randomly assigned letters. I've been unsuccessful in doing this. All of the syntax I find during my online research just gets me confused.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
khumquat
  • 1
  • 1
  • split the line on white space (String.split()), then check if the number of words is 3, then get the first letter of each word (String.charAt()) and check if it's the right one. Don't name `inputWord` a variable that is supposed to hold a line of text containing 3 words. – JB Nizet Jun 20 '15 at 11:29
  • Welcome to StackOverflow. If possible, please strip your question down to the essentials. http://stackoverflow.com/help/how-to-ask might also be useful for you. Happy StackOverflowing – Micha Wiedenmann Jun 20 '15 at 11:38

3 Answers3

0

generateLetters() should return the generated letters.

getWord() doesn't actually do anything with the user input. Make it return a String that contains the user input.

You can use a separate method to validate the input.

Here's a fixed version of your code.

public static void main(String[] args) {
    char[] letters = generateLetters();
    System.out.println(letters);
    String input = getWord();
    System.out.println("Input is " + (isValid(letters, input) ? "valid" : "invalid"));
}


public static boolean isValid(char[] letters, String input) {
    String[] words = input.split(" ");
    for (int n = 0; n < words.length; n++)
        if (!words[n].startsWith("" + letters[n]))
            return false;
    return true;
}

// Here're the fixed versions of your other two methods:

public static String getWord(){
    System.out.println("Enter a word for your letters: ");
    Scanner wordInput = new Scanner(System.in);
    String inputWord = wordInput.nextLine().toUpperCase();
    wordInput.close();
    return inputWord;
}

public static char[] generateLetters(){
    Random randomLetter = new Random();
    char[] letter = new char[26];
    char c = 'A';
    for (int i = 0; i < 26; i++)
        letter[i] = c++;
    char[] letters = new char[26];
    for(int i = 0; i < 3; i++)
        letters[i] = letter[randomLetter.nextInt(26)];
    return letters;
}
wvdz
  • 16,251
  • 4
  • 53
  • 90
0
import java.util.Random;
import java.util.Scanner;


public class NewClass {

public static void main(String[] args) {
    char[] letters = generateLetters();
    String input = getWord();
    String[] words = input.split(" ");
    if (words.length == letters.length) {
        if (check(letters, words)) {
            System.out.println("Yeah");
        } else {
            System.out.println("Oops");
        }
    } else {
        System.out.println("INVALID");
    }

}

public static Boolean check(char[] letters, String[] words) {
    for (int i = 0; i < letters.length; i++) {
        if (words[i].charAt(0) != letters[i]) {
            return false;
        }
    }
    return true;
}

public static String getWord() {
    System.out.println("Enter a word for your letters: ");
    Scanner wordInput = new Scanner(System.in);
    String inputWord = wordInput.nextLine().toUpperCase();
    return inputWord;
}

public static char[] generateLetters() {
    Random randomLetter = new Random();
    char[] letters = new char[3];//Make it varaible length
    for (int i = 0; i < letters.length; i++) {
        letters[i] = (char) ((int) randomLetter.nextInt(26) + 'A');
        System.out.printf(letters[i] + "   ");
    }
    return letters;
}
}

Method 2 Regex

public static Boolean check(char[] letters, String word) {

    return word.matches(letters[0]+"(\\S)*( )"+letters[1]+"(\\S)*( )"+letters[2]+"(\\S)*");

}
Madhan
  • 5,750
  • 4
  • 28
  • 61
  • concerning generateLetters() I think you better refers to http://stackoverflow.com/questions/2626835/is-there-functionality-to-generate-a-random-character-in-java there is a better way then adding constante (64) –  Jun 20 '15 at 12:27
0

You can try this one.

public static String getWord() {
        System.out.println("Enter a word for your letters: ");
        Scanner wordInput = new Scanner(System.in);
        String inputWord = wordInput.nextLine().toUpperCase();
        wordInput.close();
        return inputWord;
    }

    public static void main(String[] args) {
        char[] letters = generateLetters();
        String input = getWord();
        StringTokenizer st = new StringTokenizer(input, " ");
        int counter = 0;
        boolean isValid = true;
        while (st.hasMoreTokens()) {
            String word = st.nextToken();
            if (!word.startsWith(String.valueOf(letters[counter]))) {
                isValid = false;
                break;
            }
            counter++;
        }
        if (isValid) {
            System.out.println("valid");

        } else {
            System.out.println("Invalid");

        }

    }

This code will only work for the input you have provided, i am assuming thats what you need i.e. if the random letters are ' J U D ' only the input "Just Use Data". will qualify as correct input and not Use Just Data. I have also not checked the boundaries of input.

Johnson Abraham
  • 771
  • 4
  • 12