I am new to java and I would like to ask you if in my indexWord() the storing of each String's character is correct? And if not can you suggest me some other way to do it!
(My program is at the early stages yet as you can see
Cheers
import java.util.*;
import acm.program.*;
public class Hangman extends ConsoleProgram {
public String[] wordList = new String[3];
public Random rand = new Random();
public void run(){
setWords();
pickWord();
indexWord(pickWord());
}
//This class stores some words in an array for the game
public void setWords(){
String s1 = "lagoon";
String s2 = "java";
String s3 = "dog";
wordList[0] = s1;
wordList[1] = s2;
wordList[2] = s3;
}
//This class picks a random word from the wordList array using the random generator
public String pickWord(){
String currentWord = "";
int randomNum = rand.nextInt(3);
currentWord = wordList[randomNum];
//println(currentWord);
return currentWord;
}
//This class indexes and stores each character from a given string to an array
public char[] indexWord(String str){
char[] wordCh = new char[str.length()];
for(int i = 0; i <= str.length(); i++){
wordCh[i] = str.charAt(i);
//println(wordCh[i]);
}
return wordCh;
}
}

New Topic/Question
Reply



MultiQuote







|