0

Java beginner here, please be patient. So I need to import a txt file that looks like this

A

Germany

India

B

Vietnam

China

and the program will need to compare the country that the user typed then determine whether it belongs to group A or B and return the group. I have been told to use ArrayList. At the moment my code looks like

public class regionCountry
{
   public String region;
   public ArrayList <String> countryList;
} 

public class destination
{
   public static void main (String [] args)
   {
      ArrayList<regionCountry> rc = new ArrayList<regionCountry>;
   }
}

but I still have no idea what to do. Any help will be much appreciated.

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
bisonsausage
  • 71
  • 3
  • 12
  • Have you read documentation on reading text files? Start with that and show some code on how far you get, then we will help you. – ruthless Sep 12 '14 at 05:29
  • this [link](http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java) will get you started. – Ker p pag Sep 12 '14 at 05:33
  • If you're talking about the scanner and new File() then yes. It looks something like Scanner c = new Scanner(new File("c:/myfile.txt")); My question is how to determine if the country belongs to the specific group as there are multiple countries in one group. – bisonsausage Sep 12 '14 at 05:36
  • Your naming convention for naming classes is wrong. Use Capitalized. Your code to next reader should be good read. – Pramod S. Nikam Sep 12 '14 at 05:43

1 Answers1

1

You can follow following steps.

1. Read your file (You can use Scanner)
2. Split data and store them in a `ArrayList`. 

Now let's try to work on these.

How to read a file?

 File file=new File("yourFilePath");
 Scanner scanner=new Scanner(file);
 while (scanner.hasNextLine()){
      // now you can get content of file from here.
 }

Then split content and create an instance of your object set region and add country list.

Note:

Use proper naming conversion. change regionCountry to RegionCountry. Class name should start with capital letter.

Make all variables private and add public getters and setters.

Edit: for your comment. How to determine the group and country?

 File file=new File("/home/ruchira/Test.txt");
 Scanner scanner=new Scanner(file);
 RegionCountry regionCountry = null;
 List<RegionCountry> regionCountryList=new ArrayList<>();
 List<String> groupList=new ArrayList<>();
 groupList.add("A");
 groupList.add("B");
 List<String> countryList = null;
  while (scanner.hasNextLine()){
    String line=scanner.nextLine();
    if(!"".equals(line)){
        if(groupList.contains(line.trim())){
         if(regionCountry!=null&&groupList.contains(regionCountry.getRegion())){
             regionCountryList.add(regionCountry);
           }
          regionCountry=new RegionCountry();
          regionCountry.setRegion(line);
          countryList=new ArrayList<>();
          }else {
          countryList.add(line);    // those will never be null in this logic
          regionCountry.setCountryList(countryList);
         }
       }
   }
  regionCountryList.add(regionCountry);// last group you have to take from here.
  System.out.println(regionCountryList);

Out put: (I have override toString() in RegionCountry )

[RegionCountry{region='A', countryList=[Germany, India]}, RegionCountry{region='B', countryList=[Vietnam, China]}]

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115