I'm trying to make a copy of my 2D array with reference variable alphabet in my WordSearch class and then print both out to the console. I was able to print out the first array with reference variable alphabet inside of the search() method in WordSearch class but when I call that method inside my getLetters() method in the WordSearch class I get two new arrays that are copies of each other inside the console. Can somebody tell me why I can't get this to work? Thank you for taking a look.
import java.util.Scanner;
import java.util.Random;
public class WordSearchTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int seed;
String word = " ";
String again = "y";
char[] newWord = null;
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("Enter a word to search for on the word search board:\n");
word = keyboard.nextLine();
newWord = word.toCharArray();
System.out.print("Would you like to search for another word? (Y = Yes and N = No)\n");
again = keyboard.nextLine();
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();
}
}
char[][] letters = new char[4][4];
WordSearch puzzles = new WordSearch(seed, newWord, letters);
puzzles.search();
System.out.println();
puzzles.getLetters();
System.out.print("\nTerminating...");
System.exit(0);
}
}
import java.util.Random;
*/
public class WordSearch {
private int seedNum;
private char randChars;
private char[] wordArray;
private char[][] lettersFound;
private char[][] alphabet;
public WordSearch(int seeded, char[] wordA, char[][] alpha) {
seedNum = seeded;
wordArray = wordA;
alphabet = alpha;
}
public char[][] search() {
for(int rowz = 0; rowz < alphabet.length; rowz++)
{
System.out.print("+---+ " + "+---+ " + "+---+ " + "+---+\n");
for(int col = 0; col < alphabet[0].length; col++)
{
Random rand = new Random(seedNum);
char someChar = (char)(rand.nextInt(26) + 65);
alphabet[rowz][col] = someChar;
System.out.print("| " + alphabet[rowz][col] + " | ");
seedNum += 26;
}
System.out.print("\n+---+ " + "+---+ " + "+---+ " + "+---+\n");
}
return alphabet;
}
public void getLetters() {
lettersFound = search();
for(int rowz = 0; rowz < alphabet.length; rowz++)
{
System.out.print("+---+ " + "+---+ " + "+---+ " + "+---+\n");
for(int col = 0; col < alphabet[0].length; col++)
{
System.out.print("| " + lettersFound[rowz][col] + " | ");
}
System.out.print("\n+---+ " + "+---+ " + "+---+ " + "+---+\n");
}
}
}
Aucun commentaire:
Enregistrer un commentaire