我想從字符串數組填充我的java awt列表。 字符串數組獲取.csv文件中的值。 (時區) e.g 澳大利亞,墨爾本 澳大利亞,悉尼 歐洲,倫敦從字符串數組填充java.awt.list
注:有兩個列表框。第一個爲地區,第二個爲城市。 這個想法是在用戶選擇第一個區域後進行第二個更新。
我似乎無法能夠從這些值填充列表
任何幫助將不勝感激 感謝
String fileName = "C:\\Users\\Seb\\IdeaProjects\\TimeMachine\\src\\regions.csv";
File file = new File(fileName);
try {
Scanner inputStream = new Scanner(file);
inputStream.useDelimiter(System.getProperty("line.separator")); //Stops the white spaces creating a new entry in array
while (inputStream.hasNext()) {
String data = inputStream.next(); //gets the whole line
String[] arrayLocations = data.split(",");
System.out.println(arrayLocations[0]);
System.out.println(arrayLocations[1]);
listRegion = Arrays.asList(arrayLocations);
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
出現在listRegion = Arrays.asList(arrayLocations);
整個代碼
錯誤import java.applet.Applet;
import java.awt.*;
import java.awt.List;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class TimeMachine extends Applet
{
private List listRegion = new List();
private List listSubRegion = new List();
public void init()
{
setLayout(new BorderLayout());
Panel buttons = new Panel(new BorderLayout());
buttons.setBackground(Color.cyan);
buttons.add(listRegion, BorderLayout.WEST);
buttons.add(listSubRegion, BorderLayout.CENTER);
add(buttons, BorderLayout.NORTH);
}
public void getLocationInfo()
{
String fileName = "C:\\Users\\Seb\\IdeaProjects\\TimeMachine\\src\\regions.csv";
File file = new File(fileName);
try {
Scanner inputStream = new Scanner(file);
inputStream.useDelimiter(System.getProperty("line.separator")); //Stops the white spaces creating a new entry in array
while (inputStream.hasNext()) {
String data = inputStream.next(); //gets the whole line
String[] arrayLocations = data.split(",");
System.out.println(arrayLocations[0]);
System.out.println(arrayLocations[1]);
listRegion = Arrays.asList(arrayLocations);
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
使用'java.util.List'會不會更容易? – Keppil
什麼是'listRegion'?你會得到什麼錯誤? – BobTheBuilder
你可以在GUI窗口中顯示java.util.List嗎? –