Results 1 to 12 of 12
  1. #1
    holy.water is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Problem in parsing text

    Hi guys, i have a text file which has the following data inside.

    Account Id = 123
    Name = Matt Damon
    Address = 465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
    DOB = 10-10-1970
    Phone Number = 790-3233
    Account Balance = 405600.00

    Account Id = 126
    Name = Ben Affleck
    Address = 200 Hunting Street, Singapore 784563
    DOB = 25-10-1968
    Phone Number = 432-4579
    Account Balance = 530045.00

    I have tried to use the split method to split the data into tokens and put them into the various category like account id, name, address etc...but how do it use the token to fit into the category correctly? For instance, Name is Ben Affleck from the token? Thank you.

    Java Code:
    public void readRecords() {
            try {
                File myfile = new File("customers.txt");
                Scanner sc = new Scanner(myfile);
                String str;
                String temp[] = null;
    
                while (sc.hasNext()) {
                    str = sc.nextLine();
                    temp = str.split("=");
                    int k = temp.length;
                    String condition="";
                    int i=0;
                    if(i<k) {
                        condition=temp[i];
                       System.out.println(condition);
                      records[i].setAccountId(temp[1]);
                      records[i].setName(temp[1]);
                      records[i].setAddress(temp[1]); 
                      records[i].setDOB(temp[1]);
                      records[i].setPhone(temp[1]);
                      i++;
                    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by holy.water View Post
    Hi guys, i have a text file which has the following data inside.

    Account Id = 123
    Name = Matt Damon
    Address = 465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
    DOB = 10-10-1970
    Phone Number = 790-3233
    Account Balance = 405600.00

    Account Id = 126
    Name = Ben Affleck
    Address = 200 Hunting Street, Singapore 784563
    DOB = 25-10-1968
    Phone Number = 432-4579
    Account Balance = 530045.00

    I have tried to use the split method to split the data into tokens and put them into the various category like account id, name, address etc...but how do it use the token to fit into the category correctly? For instance, Name is Ben Affleck from the token? Thank you.

    Java Code:
    public void readRecords() {
            try {
                File myfile = new File("customers.txt");
                Scanner sc = new Scanner(myfile);
                String str;
                String temp[] = null;
    
                while (sc.hasNext()) {
                    str = sc.nextLine();
                    temp = str.split("=");
                    int k = temp.length;
                    String condition="";
                    int i=0;
                    if(i<k) {
                        condition=temp[i];
                       System.out.println(condition);
                      records[i].setAccountId(temp[1]);
                      records[i].setName(temp[1]);
                      records[i].setAddress(temp[1]); 
                      records[i].setDOB(temp[1]);
                      records[i].setPhone(temp[1]);
                      i++;
                    }
    }
    If you split on the "=" character you end up with leading and/or trailing spaces; i.e for the line "Name = Ben Affleck" you get two tokens "Name " and " Ben Affleck" (note the leading/trailing spaces). Better trim() those tokens first before you use them. Check the first token to see what member of your record object to set.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    holy.water is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    If you split on the "=" character you end up with leading and/or trailing spaces; i.e for the line "Name = Ben Affleck" you get two tokens "Name " and " Ben Affleck" (note the leading/trailing spaces). Better trim() those tokens first before you use them. Check the first token to see what member of your record object to set.

    kind regards,

    Jos
    Howe come when i tried this section of code, the value returned back is so weird. As in when temp[0] is returned back, it returns this info

    Account Id
    Name
    Address
    DOB
    Phone Number
    Account Balance

    And when i tried temp[1], it returns the value in the whole chunk.

    Let's say if i want to store the value in the array, how do i do that?

    Thanks.


    Java Code:
     if(i<k) {
                        condition=temp[i];
                       System.out.println(condition);
                      records[i].setAccountId(temp[1]);
                      records[i].setName(temp[1]);
                      records[i].setAddress(temp[1]); 
                      records[i].setDOB(temp[1]);
                      records[i].setPhone(temp[1]);
                      i++;
                    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by holy.water View Post
    Howe come when i tried this section of code, the value returned back is so weird. As in when temp[0] is returned back, it returns this info

    Account Id
    Name
    Address
    DOB
    Phone Number
    Account Balance

    And when i tried temp[1], it returns the value in the whole chunk.

    Let's say if i want to store the value in the array, how do i do that?

    Thanks.


    Java Code:
     if(i<k) {
                        condition=temp[i];
                       System.out.println(condition);
                      records[i].setAccountId(temp[1]);
                      records[i].setName(temp[1]);
                      records[i].setAddress(temp[1]); 
                      records[i].setDOB(temp[1]);
                      records[i].setPhone(temp[1]);
                      i++;
                    }
    Your program logic isn't correct: you read one line each time through the loop so you get one attribute (i.e. the account, the name, the adress etc). per read. You split the line so in temp[0] ends up the attribute name and in temp[1] the attribute value is stored. You set all attribute with that temp[1] value. As I wrote before you should check (according to the temp[0] value) which attribute to set.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    holy.water is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Your program logic isn't correct: you read one line each time through the loop so you get one attribute (i.e. the account, the name, the adress etc). per read. You split the line so in temp[0] ends up the attribute name and in temp[1] the attribute value is stored. You set all attribute with that temp[1] value. As I wrote before you should check (according to the temp[0] value) which attribute to set.

    kind regards,

    Jos
    so does that mean that i have to write the code in this way?

    Java Code:
    if(temp[0].equals("Account Id")
    set account_id
    else if(...)
    set name

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by holy.water View Post
    so does that mean that i have to write the code in this way?

    Java Code:
    if(temp[0].equals("Account Id")
    set account_id
    else if(...)
    set name
    Yep, that's a good start; you also have to decide when you have to populate a next record when another line has been read.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    holy.water is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    ok when we want to populate new record

    if(empty line)
    increment the records index for new record

    but all this happens in the while loop right?

  8. #8
    holy.water is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    When i tried this code, it did not print out anything assuming i followed the steps above.

    Java Code:
    if (temp[0].equalsIgnoreCase("Name")) {
                        System.out.println("test");
                    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by holy.water View Post
    ok when we want to populate new record

    if(empty line)
    increment the records index for new record

    but all this happens in the while loop right?
    Yep, and may I suggest you do the processing of a line in a separate method? That simplifies your program logic; e.g.

    Java Code:
    while ((line= br.readLine()) != null) {
       if (line is empty)
          currentRecord++;
       else
          processLine(line, currentRecord);
    }
    All of the nitty gritty stuff is handled in the processLine( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by holy.water View Post
    When i tried this code, it did not print out anything assuming i followed the steps above.

    Java Code:
    if (temp[0].equalsIgnoreCase("Name")) {
                        System.out.println("test");
                    }
    Reread my first reply: you have to trim() the temp[0] and temp[1] values because you want to get rid of any leading and/or trailing spaces.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    holy.water is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Let's say i tried this code why does it only read the last record?

    Java Code:
     if (str.trim() != null || str.length() != 0) {
                        if (temp[0].trim().equalsIgnoreCase("Account Id")) {
                            records[i].setAccountId(temp[1].trim());
                        } else if (temp[0].trim().equalsIgnoreCase("Name")) {
                            records[i].setName(temp[1].trim());
                        } else if (temp[0].trim().equalsIgnoreCase("Address")) {
                            records[i].setAddress(temp[1].trim());
                        } else if (temp[0].trim().equalsIgnoreCase("DOB")) {
                            records[i].setDOB(temp[1].trim());
                        } else if (temp[0].trim().equalsIgnoreCase("Phone Number")) {
                            records[i].setPhone(temp[1].trim());
                        } else if (temp[0].trim().equalsIgnoreCase("Account Balance")) {
                            double balance = Double.parseDouble(temp[1].trim());
                            records[i].setBalance(balance);
                        } else {
                            
                        }
                    } else {
                        i++;
                      
                    }
                }
    
    
    public void read() {
            for (int i = 0; i < count; i++) {
                
                System.out.println("Account Id: " + records[i].getAccountId());
                System.out.println("Name: " + records[i].getName());
                System.out.println("Address: " + records[i].getAddress());
                System.out.println("DOB: " + records[i].getDOB());
                System.out.println("Phone Number: " + records[i].getPhone());            
                System.out.println("Account Balance: " + records[i].getBalance());            
                System.out.println();
            }
        }

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by holy.water View Post
    Let's say i tried this code why does it only read the last record?
    Just a guess (it can't be determined from the code you've posted): all your records in your array refer to the same Record object.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Parsing Problem in Java
    By vidya in forum Advanced Java
    Replies: 4
    Last Post: 02-01-2010, 03:48 PM
  2. Parsing Problem in Java
    By vidya in forum New To Java
    Replies: 2
    Last Post: 02-01-2010, 01:52 PM
  3. Parsing Text Files
    By coder09 in forum New To Java
    Replies: 6
    Last Post: 02-18-2009, 12:08 PM
  4. parsing/storing large text data
    By hkansal in forum New To Java
    Replies: 4
    Last Post: 10-19-2008, 06:34 PM
  5. parsing problem
    By Pooja Deshpande in forum Advanced Java
    Replies: 4
    Last Post: 05-29-2008, 01:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •