![](/img/trans.png)
[英]How to populate array with text from a file using Scanner and then randomly select the text from the array?
[英]how to randomly select an image from an arraylist using scanner in java
我正在嘗試掃描圖像文件夾並將其添加到arraylist。 我想結合使用掃描儀和隨機變量,以從陣列列表中隨機選擇圖像之一,並將其附加到私有ImageIcon圖標。 “ FemaleFaces”是包含圖像的目錄。 到目前為止,這是我的代碼,感謝您的幫助!
private ImageIcon iconex;
File f = new File("FemaleFaces");
Scan = new Scanner(f);
ArrayList<ImageIcon> files = new ArrayList<ImageIcon>();
while(Scan.hasNext())
{
files.add(new ImageIcon(Scan.next()));
}
Scan.close();
int Ffindex = new Random().nextInt(files.size());
iconex = files.get(Ffindex);
線程“主”中的異常java.io.FileNotFoundException:FemaleFaces(訪問被拒絕),位於java.io.FileInputStream.open(本機方法),位於java.io.FileInputStream。(未知源),位於java.util.Scanner。(未知)來源)在人類(Human.java:66)在testerobjects.main(testerobjects.java:19)
Scan.next()
返回一個String
。 您需要ImageIcon
該值構造一個ImageIcon
。
類似於...
files.add(new ImageIcon(Scan.next()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.