I am trying to populate my java awt list from a string array. The string array gets the values from a .csv file. (Timezones) e.g Australia,Melbourne Australia,Sydney Europe,London
Note: There are two list boxes. First one for region and second one for City. The idea is to have the second one update after the user selects a region in the first one.
I can't seem to be able to populate the list from these values
Any help will be appreciated Thanks
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();
The error appears in listRegion = Arrays.asList(arrayLocations);
Entire code
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?listRegion? What error do you get?