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]}]