0

This is the text file:

#Person
PRIM_KEY=personId
SEX=gender
YEARS=age
NAME=fullName
#Automobil
PRIM_KEY=registrationNumber
MAKE=manufacturer
TYPE=model

Read the file:

Scanner scanner = new Scanner(new FileReader("C:/workspace/column-mapping.txt"));

When I encounter #Person, in the following map defined:

Map<String, String> personMap = new LinkedHashMap<String, String>();

I want to store the key-value pairs below it. So store these key-value pairs in personMap:

PRIM_KEY=personId
SEX=gender
YEARS=age
NAME=fullName

Similarly when I encounter #Automobil, in the following map

Map<String, String> automobilMap = new LinkedHashMap<String, String>();

I want these key-value pairs stored:

PRIM_KEY=registrationNumber
MAKE=manufacturer
TYPE=model

When I read the file, how to store these key-value pairs in two different maps depending upon, in this example, #Person and #Automobil?

EDIT Sample Code:

    Scanner scanner = new Scanner(new FileReader("C:/workspace/column-mapping.txt"));
    Map<String, String> personMap = new LinkedHashMap<String, String>();
    Map<String, String> automobilMap = new LinkedHashMap<String, String>();
    String line;

    while (scanner.hasNext()) {
         line = scanner.next();
         if (!line.startsWith("#") && !line.isEmpty()) {
             String[] columns = line.split("=");

             personMap.put(columns[0], columns[1]);
         }


    }
    System.out.println(personMap);

This way I can put all key-value pairs in one map personMap. But depending upon sections, I want to be able to put it in different maps.

SASM
  • 1,292
  • 1
  • 22
  • 44
  • 1
    And your question is? – RealSkeptic Jul 29 '15 at 08:24
  • possible duplicate of [how to read a config file with sections using Java](http://stackoverflow.com/questions/3728823/how-to-read-a-config-file-with-sections-using-java) – Buurman Jul 29 '15 at 08:27
  • 1
    Well ... I would suggest you write some code that fulfills the requirement that you posted. Or ... you pay some money to someone who writes the code for you. I would do ... – Seelenvirtuose Jul 29 '15 at 08:28
  • You already have 90% of the required code - you just need to remember the Map that you are currently adding to and switch that map reference depending on the text behind the "#" character. – nutfox Jul 29 '15 at 09:05

2 Answers2

4

I want to propose you two different approaches. The first approach is simply solving your problem with a an if case, in the second approach I am supposing you want to store multiple Automobil and multiple Person so I am creating two classes to solve the problem.

I hope this can be useful, it was interesting to make this code

First approach

public static void main(String args[]) throws FileNotFoundException{
Scanner scanner = new Scanner(new FileReader("C:/workspace/column-mapping.txt"));
    Map<String, String> personMap = new LinkedHashMap<String, String>();
    Map<String, String> automobilMap = new LinkedHashMap<String, String>();
    String line= scanner.nextLine();

    while(scanner.hasNext()){

        Map<String, String> currentMap = null;
        if(line.equals("#Person")){
            currentMap=personMap;
        }
        if(line.equals("#Automobil")){
            currentMap=automobilMap;
        }
        while(scanner.hasNext()){
            line=scanner.nextLine();
            if(line.startsWith("#"))
                break; //restart the loop
            String splitted[] = line.split("=");
            currentMap.put(splitted[0], splitted[1]);
        }            
    }

}

Second approach

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new FileReader("C:/workspace/column-mapping.txt"));
    Map<String, Person> personMap = new LinkedHashMap<String, Person>();
    Map<String, Car> automobilMap = new LinkedHashMap<String, Car>();
    String line;

    while (scanner.hasNextLine()) {
        line = scanner.nextLine();
        if(line.equals("#Person")){
            String primaryKey = scanner.nextLine().split("=")[1];
            String sex = scanner.nextLine().split("=")[1];
            String years = scanner.nextLine().split("=")[1];
            String name = scanner.nextLine().split("=")[1];
            personMap.put(primaryKey, new Person(sex,years,name));
        }
        if(line.equals("#Automobil")){
            String primaryKey = scanner.nextLine().split("=")[1];
            String make = scanner.nextLine().split("=")[1];
            String type = scanner.nextLine().split("=")[1];
            automobilMap.put(primaryKey, new Car(make,type));
        }

    }

    Set<String> personKeys = personMap.keySet();
    Set<String> automobilKeys = automobilMap.keySet();

    for(String k : personKeys){
        System.out.println("Person: "+k);
        System.out.println(personMap.get(k));
    }

    for(String k : automobilKeys){
        System.out.println("Car: "+k);
        System.out.println(automobilMap.get(k));
    }
}

The two classes for the second approach

public class Person {
boolean male; //true for male, false for female
int years;
String name;
String surname;

public Person(String gender, String age, String fullName){
    if(gender.equals("male"))
        male = true;
    else
        male = false;
    years=Integer.parseInt(age);
    String splitted[] = fullName.split(" ");
    name = splitted[0];
    surname = splitted[1];
}

@Override
public String toString(){
    String gender = "male";
    if(!male)
        gender = "female";
    return "SEX = "+gender+"\nYEARS = "+years+"\nNAME = "+name+" "+surname;
}
}

public class Car {
String make;
String type;

public Car(String manufacturer,String model){
    make=manufacturer;
    type=model;
}

@Override
public String toString(){
    return "MAKE = "+make+"\nTYPE = "+type;
}

}

Niles
  • 489
  • 1
  • 8
  • 18
1

Not the most elegant solution, but got it working.

Scanner scanner = new Scanner(new FileReader("C:/workspace/column-mapping.txt"));
Map<String, String> personMap = new LinkedHashMap<String, String>();
Map<String, String> automobilMap = new LinkedHashMap<String, String>();

String line;
boolean person = false;
boolean automobil= false;

while (scanner.hasNext()) {
     line = scanner.next();
     if(line.startsWith("#Person") && !line.isEmpty()){
        line = scanner.next();
        person = true;
        automobil = false;
     }

     if(line.startsWith("#Automobil") && !line.isEmpty()){
        line = scanner.next();
        automobil = true;
        person = false;
     }

     if(person){
        if (!line.startsWith("#") && !line.isEmpty()) {
            String[] columns = line.split("=");
            for(String str:columns){
                 System.out.println(str);
            }
            personMap.put(columns[0], columns[1]);
        }
     }

     if(automobil){
         if (!line.startsWith("#") && !line.isEmpty()) {
             String[] columns = line.split("=");
             for(String str:columns){
                  System.out.println(str);
             }
             automobilMap.put(columns[0], columns[1]);
         }
      }



}
System.out.println("personMap"+personMap);
System.out.println("automobilMap"+automobilMap);
SASM
  • 1,292
  • 1
  • 22
  • 44