0

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();
    }
}

}

3
  • Wouldn't it be easier to use a java.util.List? Commented May 20, 2013 at 13:15
  • 1
    What is listRegion? What error do you get? Commented May 20, 2013 at 13:15
  • Can you show java.util.List in a GUI window ? Commented May 20, 2013 at 13:16

1 Answer 1

1

You are mixing both java.util.List and java.awt.List, use only 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);
        }

More formally, below is a working code where region.csv must contain REGION,COUNTRY format where duplicates are handled:

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();
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks so much. That worked perfectly. Is there anyway to only display [0] part of the array in the listRegion ?
Try using listRegion.select(index)
Maybe I worded my question wrong but that didn't work. I tried 'list.region.add(location,1)' However that still displayed [1] parts of the array in the first list box. I only wants the regions to appear in their. Thanks for your help though but any other ideas
two things, you are not filling listSubRegion anywhere in the code and there is no logic implemented for maintaining region and country (subregions) association.
Can I contact you outside this website to talk further about this. I would really appreciate your assistance with this project. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.