0

我正在尝试从字符串数组中填充我的 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();
    }
}

}

4

1 回答 1

1

您正在混合java.util.Listjava.awt.List,仅使用java.awt.List

listRegion = new List();
 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]);
            for(String location : arrayLocations)
                 listRegion.add(location);
        }

更正式地说,下面是一个工作代码,其中 region.csv 必须包含处理重复项的 REGION,COUNTRY 格式:

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.List;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class TimeMachine extends Applet implements ItemListener{
    private static final long serialVersionUID = 1L;

    private List listRegion = new List();
    private List listSubRegion = new List();
    private Map<String, Set<String>> regionCountryMap = new HashMap<String, Set<String>>();

    public void init()
    {
        setLayout(new BorderLayout());

        getLocationInfo();
        Panel buttons = new Panel(new BorderLayout());
        buttons.setBackground(Color.cyan);
        buttons.add(listRegion, BorderLayout.WEST);
        buttons.add(listSubRegion, BorderLayout.CENTER);
        add(buttons, BorderLayout.NORTH);
    }

    private void getLocationInfo() {
        String fileName = "e:\\regions.csv";
        File file = new File(fileName);
        try {
            Scanner inputStream = new Scanner(file);
            inputStream.useDelimiter(System.getProperty("line.separator"));
            listRegion = new List();
            while (inputStream.hasNext())
            {
                String data = inputStream.next(); // gets the whole line
                String[] arrayLocations = data.split(",");
                if(arrayLocations.length != 2){
                    System.out.println("Invalid entry in file : "+ Arrays.toString(arrayLocations));
                    continue;
                }
                String region = arrayLocations[0];
                if(!regionCountryMap.containsKey(region))
                {
                    listRegion.add(region);
                    regionCountryMap.put(region, new HashSet<String>());
                }
                regionCountryMap.get(region).add(arrayLocations[1]);//Set handles duplicates as well
            }
            inputStream.close();
            System.out.println("Region, Country list has been parsed successfully");
            listRegion.addItemListener(this);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        Set<String> countries = regionCountryMap.get(listRegion.getSelectedItem());
        listSubRegion.removeAll();
        for(String country : countries) {
            listSubRegion.add(country);
        }
        listSubRegion.validate();
        listSubRegion.repaint();
    }
}
于 2013-05-20T13:28:06.553 回答