What I am working on is reading in a file and passing it to an ArrayList which I have already done with this:
public ArrayList readInPhrase() {
String fileName = "wholephrase.txt";
ArrayList<String> wholePhrase = new ArrayList<String>();
try {
//creates fileReader object
FileReader inputFile = new FileReader(fileName);
//create an instance of BufferedReader
BufferedReader bufferReader = new BufferedReader(inputFile);
//variable to hold lines in the file
String line;
//read file line by line and add to the wholePhrase array
while ((line = bufferReader.readLine()) != null) {
wholePhrase.add(line);
}//end of while
//close buffer reader
bufferReader.close();
}//end of try
catch(FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "Unable to open file '" +
fileName + " ' ", "Error",
JOptionPane.INFORMATION_MESSAGE, null);
}//end of file not found catch
catch(Exception ex) {
JOptionPane.showMessageDialog(null, "Error while reading in file '"
+ fileName + " ' ", "Error",
JOptionPane.INFORMATION_MESSAGE, null);
}//end of read in error
return(wholePhrase);
}//end of readInPhrase
The problem that I am now having is that I would like to go through this ArrayList and randomly select one phrase from it to eventually append asterisk's to part of the phrase that is selected. I have tried a variety of different ways to do this.
This is the last attempt that I have tried:
public String getPhrase(ArrayList<String> wholePhrase) {
Random random = new Random();
//get random phrase
int index = random.nextInt(wholePhrase.size());
String phrase = wholePhrase.get(index);
return(phrase);
}//end of getPhrase
I am not entirely sure where it is that I am getting lost at. Any help that can be offered will be greatly appreciated, Thank you in advance!
Aucun commentaire:
Enregistrer un commentaire