mercredi 4 octobre 2017

How to print randomly generated letters using the random object into a 2D array

I'm trying to fill a 2D array with the reference variable called letters with randomly generated upper case letters using the Random object. I've tried doing it in both classes now but I still get a few errors that I've never encountered before. Any help on this is greatly appreciated.

Below are the errors I'm getting in the WordSearch class and where they are located:

-I get an error that says, "char someChar = (char)(rand.nextInt(26) + 65);" 
The error reads, "Syntax error on token ";", { expected after this token."

-I'm also getting an error at the end of my for loop where the } is.
The error reads, "Syntax error, insert "}" to complete Block."

-Lastly, I'm getting an error on the line that says, "public search(){"
The error reads, "Return type for the method is missing."




import java.util.Random;
import java.util.Scanner;

public class WordSearchTest {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    int seed;
    String word = " ";
    String again = "y";

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a number from 1 - 9999:\n");
    seed = keyboard.nextInt();
    keyboard.nextLine();       //Consume the remaining new line
    while(seed < 1 || seed > 9999) {
        System.out.print("You must choose a number between 1 and 9999:\n");
        seed = keyboard.nextInt();
        keyboard.nextLine();   //Consume the remaining new line
    }

    while(again.equalsIgnoreCase("y")) {
        System.out.print("Choose a word to search for:\n");
        word = keyboard.nextLine();
        System.out.print("Would you like to search for another word? (Y = Yes and N = No)\n");
        again = keyboard.nextLine();
        System.out.print(again);
        while(!again.equals("Y") && !again.equals("y") && !again.equals("N") && !again.equals("n")) {
            System.out.print("Invalid response. Y or N?\n");
            again = keyboard.nextLine();
            }
        }

    //Random rand = new Random(seed);

    //char someChar = (char)(rand.nextInt(26) + 65);    

    //Instantiates a WordSearch object with reference variable puzzles and passes the arguments to the WordSearch constructor
    WordSearch puzzles = new WordSearch(seed, word);

    puzzles.search();

    System.out.print("Terminating...");
    System.exit(0);

    }

}


import java.util.Random;

public class WordSearch {

private int seedNum;
private String wordGiven;
private int index = 0;
private char someCharz;
char[][] letters;
private char[][] lettersFound;


public WordSearch(int seeded, String wordUser) {
    seedNum = seeded;
    wordGiven = wordUser;
    //someCharz = charz;
}


Random rand = new Random(seedNum);

char someChar = (char)(rand.nextInt(26) + 65);      

letters = new char[4][4];

lettersFound = new char[4][4];


for(int col = 0; col < letters[0].length; col++)
{
    for(int rowz = 0; rowz < letters.length; rowz++)
    {
        System.out.print(someCharz);
    }
    index++;
}


public search() {
    System.out.print(letters);
}


/**
 * @return the seedNum
 */
public int getSeedNum() {
    return seedNum;
}


/**
 * @param seedNum the seedNum to set
 */
public void setSeedNum(int seedNum) {
    this.seedNum = seedNum;
}


/**
 * @return the wordGiven
 */
public String getWordGiven() {
    return wordGiven;
}


/**
 * @param wordGiven the wordGiven to set
 */
public void setWordGiven(String wordGiven) {
    this.wordGiven = wordGiven;
}

}




Aucun commentaire:

Enregistrer un commentaire