samedi 20 juin 2015

How to determine if words in an input string start with random assigned letters in Java?

Okay so I am working on a project and it basically assigns three random letters to the user, lets say ' J U D ', and the user has to type something for each letter like "Just Use Data". I have it setup to work, but what I need to find out and so far have been unsuccessful searching for, is how to validate that input to make sure that each word starts with the assigned random letter. Basically, here's the code.

To generate letters:

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... so then in public static void main, here's what I got.

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

*** Now, I cannot paste the exact code for a few reasons, but this should give you an idea of what I am trying to do. I am newer to Java, and so far this works on my end. 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, but don't have much experience with Arrays and sorting through them, either. All of the syntax I find just gets me confused but I'm sure it's something simple that I'm just not seeing or haven't yet learned.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire