我正在尝试从字符串数组中填充我的 java awt 列表。字符串数组从 .csv 文件中获取值。(时区)例如澳大利亚、澳大利亚墨尔本、欧洲悉尼、伦敦
注意:有两个列表框。第一个用于地区,第二个用于城市。这个想法是在用户选择第一个区域后更新第二个区域。
我似乎无法从这些值中填充列表
任何帮助将不胜感激谢谢
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();
}
}
}