Search code examples
javafile-iotext-filesfileinputstream

Java reading formatted text file and dividing it


I'm trying to understand file I/O for class and I understand the basics but I'm having trouble understanding how to manage whats in the input file, the input file is formatted like this:

BusinessContact:firstName=Nikolaos;lastName=Tsantalis SocialNetworkAccount:socialNetworkType=SKYPE;accountID=tsantalis

Basically my contact (which BusinessContact extends from) object has attributes of firstName, lastName and middleName, it also has object attributes such as SocialNetworkAccount and such....

I don't need to be explained how my objects are formatted, those have been done all I'm trying to understand is how my file.txt in inputed into my program to set my Contact to a BusinessContact as well as setting the first and last name accordingly,

Thanks

EDIT: Im specifically told to use the split method which makes sense but I'm also told (1) create a common method for the parsing of attributes that returns a map where the keys correspond to the attributeNames and the values to the attributeValues (in this way you can reuse the same code)


Solution

  • You can use the Scanner class with different delimiters like below:

    Scanner in = new Scanner(/**source*/);
    
    in.useDelimiter(":");
    
    String firstName, lastName;
    
    String firstWord = in.next();
    
    Scanner nameScanner = new Scanner(in.nextLine());
    
    nameScanner.useDelimiter(";");
    
    firstName = getName(new Scanner(nameScanner.next()));
    
    lastName = getName(new Scanner(nameScanner.next()));
    
    
    private String getName(Scanner nameScanner){
        nameScanner.useDelimiter("=");
    
        String nameTitle = nameScanner.next();
    
        return nameScanner.next();
    }
    

    This way you read the text in parts as follows as follows:

    BusinessContact:firstName=Nikolaos;lastName=Tsantalis
                                     firstName=Nikolaos;lastName=Tsantalis
                                     firstName=Nikolaos;lastName=Tsantalis

    I hope this makes sense.

    NOTE: This code reads only the first line. If you want to read the second i guess its not hard to modify it. If you want the second line too or if you have any issues let me know and i will update the answer.

    EDIT: I just noticed that every line is formated the same way so basically you can use the same code for every line. Maybe in a loop like:

    Scanner input = new Scanner(/**source*/);
    
    while(input.hasNextLine()){
        Scanner in = new Scanner(input.nextLine());
        ...
        ....
        //The above code
    }
    

    String.split() method:

    Scanner in = new Scanner(System.in);
    
    String[] first = in.nextLine().split(":");
    String[] second = first[1].split(";");
    String[] thirdA = second[0].split("=");
    String[] thirdB = second[1].split("=");
    
    for(int i = 0; i < thirdA.length; i++){
        System.out.println(thirdA[i]);
        System.out.println(thirdB[i]);
    }
    

    For the first line, the above code will print:

    firstName
    lastName
    Nikolaos
    Tsantalis
    

    Hope this helps.