Reputation: 1
I had a file with two different entries, one from males and other from females. Trying to read the file and store using hashmap with the name as key and corresponding ID as value. Can some one help me to figure out how to store them in two different maps.In other words, if male direct it to (map.males) and if females direct it to (map.females). Thank you very much. Here is the sample input and my code without direction!!!!!!!
**Males**
Rob 1
John 3
Josh 7
Anand 9
Paul 5
Norm 8
Alex 4
**Females**
Kally 43
Kate 54
Mary 23
Amanda 13
Mariam 15
Alyssa 18
Christina 24
import java.io.*;
import java.util.*;
class ReadFileAndStoreHashmap {
public static void main(String[] args) {
try{
Scanner scanner = new Scanner(new FileReader("C:\"));
HashMap<String, String> map = new LinkedHashMap<String, String>();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split(" ");
if(columns.length == 2)
map.put(columns[0], columns[1]);
System.out.println(map);
}
}catch (Exception e){
System.out.println(e.toString());
}}}
Upvotes: 0
Views: 155
Reputation: 159754
Assuming you have 2 files... better to have this in one method:
private static Map<String, String> getMap(String mapFile) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader(mapFile));
Map<String, String> map = new LinkedHashMap<String, String>();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().trim().split(" ");
if (columns.length == 2) {
map.put(columns[0], columns[1]);
}
}
return map;
}
and assign as needed:
Map<String, String> malesMap = getMap("map.males");
Map<String, String> femalesMap = getMap("map.females");
Note the trim() to handle the leading whitespace.
Upvotes: 0
Reputation: 7642
It is a bit unclear exactly what you are asking. If both are in one file and delimited by the Females, then just switch maps when you see that:
import java.io.*;
import java.util.*;
class ReadFileAndStoreHashmap {
public static void main(String[] args) {
try{
Scanner scanner = new Scanner(new FileReader("C:\"));
HashMap<String, String> maleMap = new LinkedHashMap<String, String>();
HashMap<String, String> femaleMap = new LinkedHashMap<String, String>();
Map<String,String> currentMap = maleMap;
while (scanner.hasNextLine()) {
String nextLine = scanner.nextLine();
if (nextLine.equals("**Females**") {
currentMap = femaleMap;
} else {
String[] columns = nextLine.split(" ");
if(columns.length == 2) {
currentMap.put(columns[0], columns[1]);
}
}
System.out.println(currentMap);
}
}catch (Exception e){
System.out.println(e.toString());
}}}
Upvotes: 1
Reputation: 2190
If I am correct you want to use two maps for males and females. Well the solution could be as follows.
Scanner scanner = new Scanner(new FileReader("C:\"));
HashMap<String, String> males= new HashMap<String, String>();
HashMap<String, String> females= new HashMap<String, String>();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split(" ");
if(columns.length == 2){
If(its a male) //define your logic to decide gender
males.put(columns[0], columns[1]);
else if(its a female)
females.put(columns[0], columns[1]);
else
//do nothing
}
Upvotes: 0