从String Array中填充java.awt.list

时间:2013-05-20 13:13:54

标签: java arrays string csv

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

}

1 个答案:

答案 0 :(得分: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();
    }
}