I have two classes Hangman and words which are as follows:
Hangman class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hangman;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Adam2_000
*/
public class Hangman {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String player = "";
String selection;
Scanner scan = new Scanner(System.in);
words words = new words();
String easyWords1[] = words.easyWords;
String mediumWords1[] = words.mediumWords;
String hardWords1[] = words.hardWords;
System.out.println("Welcome to Hangman version 1");
System.out.println("Please choose a difficulty");
System.out.println("A: Easy");
System.out.println("B: Medium");
System.out.println("C: Hard");
char iChoice;
do {
selection = scan.nextLine().toUpperCase();
} while (selection.isEmpty());
iChoice = selection.charAt(0);
if (iChoice != 'X') {
switch (iChoice) {
case 'A':
System.out.println("You have choosen easy:");
System.out.println(words.getEasyWords());
break;
case 'B':
System.out.println("You have choosen Medium");
System.out.println(words.getMediumWords());
break;
case 'C':
System.out.println("You have choosen Hard");
System.out.println(words.getHardWords());
break;
}
}
}
}
words class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hangman;
import java.lang.reflect.Array;
import java.util.Random;
/**
*
* @author Adam2_000
*/
public class words extends Hangman {
String[] easyWords = {"Bee", "Car", "Fish", "Shed"};
String[] mediumWords = {"House", "Sheep", "Castle", "Phone"};
String[] hardWords = {"Octagon", "Crocodile", "Chocolate", "Motorbike"};
public String[] getEasyWords() {
return easyWords;
}
public void setEasyWords(String[] easyWords) {
this.easyWords = easyWords;
}
public String[] getMediumWords() {
return mediumWords;
}
public void setMediumWords(String[] mediumWords) {
this.mediumWords = mediumWords;
}
public String[] getHardWords() {
return hardWords;
}
public void setHardWords(String[] hardWords) {
this.hardWords = hardWords;
}
@Override
public String toString() {
return "words{" + "easyWords=" + easyWords + ", mediumWords=" + mediumWords + ", hardWords=" + hardWords + '}';
}
}
When I try to display each array I am getting the references and not the strings can anybody help me please?
... I am getting the references and not the strings ...
That's just how the method is defined in the class Object.
Since, you can't override that behavior for arrays, you can either iterate through it or you can use toString (static) utility method from java.util.Arrays.
You're seeing something like
[Ljava.lang.String;@17bd6a1
which is the Object#toString respresentation of a String array, To display the contents, you can use:
Arrays.toString(words.getEasyWords())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With