Results 1 to 6 of 6
  1. #1
    markip is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Parsing txt file that contains pairs

    Hello everyone. I need some help parsing this file, can you help? I thought of processing line by line using scanner and then split each line in the ":" . But then i stuck, i only need the right part of the ":" so that i can save it to a class e.g. John Doe, 133456593, married etc. Thank's in advance for any help provided.
    Java Code:
    Name: John Doe
    TRN: 133456593
    Status: Married
    Income: 22500
    Receipts:
    
    Receipt ID: 1
    Date: 25/2/2014
    Kind: Basic
    Amount: 2000
    Company: Hand Made Clothes
    Country: UK
    City:London
    Street: Cambridge
    Number: 10
    
    Receipt ID: 2
    Date: 28/2/2014
    Kind: Entertainment
    Amount: 500
    Company: Floca Cafe
    Country: Italy
    City: Rome
    Street: Piazza
    Number: 4
    Last edited by markip; 05-09-2015 at 09:16 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Parsing txt file that contains pairs

    You're on the right track. But you need to post your code and ask specific questions.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    markip is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Re: Parsing txt file that contains pairs

    Quote Originally Posted by jim829 View Post
    You're on the right track. But you need to post your code and ask specific questions.

    Regards,
    Jim
    Thank you for your answer. This is what i tried.

    Java Code:
    public Taxpayer parse(File file) throws FileNotFoundException {
    		Scanner scanner = new Scanner(file);
                    String[] tokens = null;
                    String[] values = null;
    		while(scanner.hasNext()){
    			String line = scanner.nextLine();
    			tokens = line.split(":");
    			
    			String value = tokens[1];
    			for(int i=0; i<tokens.length; i++){
    				values[i] = value;
    				System.out.println(values[i]);
    			}
    What i did doesn't work, i got a NullPointerException Erro at line values[i] = value. So, what i want to do is take the details John Doe, 133456593, Married etc. and put it in the constructor of a class named Taxpayer(name, taxRegistrationNumber, status .......).
    How should i continue?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Parsing txt file that contains pairs

    Quote Originally Posted by markip View Post
    i got a NullPointerException Erro at line values[i] = value.
    I don't see where you assigned a value other than null to the array values.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    markip is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Re: Parsing txt file that contains pairs

    Quote Originally Posted by DarrylBurke View Post
    I don't see where you assigned a value other than null to the array values.

    db
    yes i figured it out and now this is what i did. But i still can't parse integers because after the ":" there is a space. This is what i did:

    Java Code:
    public Taxpayer parse(File file) throws FileNotFoundException {
    		Scanner scanner = new Scanner(file);
    		String[] tokens = null;
    		
    		String name, status, date, kind, company, country, city, street;
    		int afm, income, receiptId, amount, streetNumber;
    		
    		String line = scanner.nextLine();
    		tokens = line.split(":");
    		name = tokens[1];
    		line = scanner.nextLine();
    		tokens = line.split(":");
    		afm = Integer.parseInt(tokens[1]);
    		line = scanner.nextLine();
    		tokens = line.split(":");
    		status = tokens[1];
    		line = scanner.nextLine();
    		tokens = line.split(":");
    		income = Integer.parseInt(tokens[1]);
    		System.out.println(name);
    		System.out.println(afm);
    		System.out.println(status);
    		System.out.println(income);
    		System.out.println("Receipts:");
    		
    		scanner.nextLine(); //Receipts:
    		
    		
    		while(scanner.hasNext()){
    			scanner.nextLine(); //empty line before receipt id
    			line = scanner.nextLine();
    			tokens = line.split(":");
    			receiptId = Integer.parseInt(tokens[1]);
    			line = scanner.nextLine();
    			tokens = line.split(":");
    			date = tokens[1];
    			line = scanner.nextLine();
    			tokens = line.split(":");
    			kind = tokens[1];
    			line = scanner.nextLine();
    			tokens = line.split(":");
    			amount = Integer.parseInt(tokens[1]);
    			line = scanner.nextLine();
    			tokens = line.split(":");
    			company = tokens[1];
    			line = scanner.nextLine();
    			tokens = line.split(":");
    			country = tokens[1];
    			line = scanner.nextLine();
    			tokens = line.split(":");
    			city = tokens[1];
    			line = scanner.nextLine();
    			tokens = line.split(":");
    			street = tokens[1];
    			line = scanner.nextLine();
    			tokens = line.split(":");
    			streetNumber = Integer.parseInt(tokens[1]);
    			System.out.println(receiptId);
    			System.out.println(date);
    			System.out.println(kind);
    			System.out.println(amount);
    			System.out.println(company);
    			System.out.println(country);
    			System.out.println(city);
    			System.out.println(street);
    			System.out.println(streetNumber);
    		}
    		
    		
    		
    		
    		
    		
    		
    		return null; 
    	}

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Parsing txt file that contains pairs

    I recommend you sprinkle some print statement throughout your program. That is the first step in debugging. In this case, check to see what the values of tokens[0] and tokens[1] are. You should also show us the reported error (probably a stack trace).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help parsing file
    By monkeyjr97 in forum New To Java
    Replies: 6
    Last Post: 10-10-2013, 04:48 PM
  2. reached end of file while parsing- using bluej
    By prendergast36 in forum New To Java
    Replies: 1
    Last Post: 09-08-2011, 09:00 PM
  3. HTML FILE parsing.
    By makpandian in forum New To Java
    Replies: 8
    Last Post: 11-02-2010, 03:00 PM
  4. parsing xml file
    By mp.saaravanan in forum New To Java
    Replies: 1
    Last Post: 05-02-2010, 02:55 PM
  5. Parsing XML File
    By rstar7 in forum XML
    Replies: 1
    Last Post: 10-10-2008, 03:31 AM

Posting Permissions

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