我無法從CSV文件中抓取隨機元素。這裏是我的代碼從CSV文件中抓取隨機元素
public String[] generateNames(String Name)
BufferedReader br = new BufferedReader(newFileReader("c:\\users\\listofcities.csv"));
Random generator = new Random();
int row = generator.nextInt(1);
String[] arrayValues = new String[row];
for (int i=1; i<arrayValues.length; i++) {
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, "\n");
while (st.hasMoreTokens()) {
//get next token and store it in the array
arrayValues[row] = st.nextToken();
}
}
row++;
}
//close the file
br.close();
return arrayValues;
}
每當我有行值小於1的任何其他我得到一個arrayoutofbounds例外。任何人都可以把我放在正確的軌道上,因爲我的代碼是性交。