什么我工作在一个文件中读取并将它传递给我已经有这个做一个ArrayList:如何将ArrayList方法传递给同一类中的其他方法?
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
,我现在遇到的问题是,我想经过此ArrayList和从它随机选择一个短语最终将星号的 附加到所选短语的一部分。我尝试了各种不同的方式来做到这一点。
这是我试图在最后一次尝试:据我所看到
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
你实际上没有解释发生了什么问题。 – elhefe
*“我不完全确定我在哪里迷路”*同上。我们也失败了,因为不清楚你试图做什么不同于你已经做的。我的意思是,它不能像*“追加星号的”*部分一样简单,因为字符串连接很容易。 – Andreas
为什么你要在返回值中加上括号? 'return'后没有空格。使它看起来像一个方法调用。 'return wholePhrase;'和'return phrase;'是你应该怎么做的。 – Andreas