Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to read different data segments from a text file?

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to read different data segments from a text file?

    i am trying to read this data from a text file

    //contains an account no. of type string followed by account balance of type double
    3000 1200.00
    1000 300.00
    4000 8000.00
    // the 0 represents the start of a new data segment
    0
    // contains a transaction type either DEP "deposit" or WID "withdrawal" of type String followed by an account no. of type String and "amount" of the particular transaction either
    DEP 3000 600.00
    WID 4000 2000.00
    // the PRINT represents the end of file and print all the account information
    PRINT

    The question is, Read the data from the file, store the account information in objects in an array and perform the specific transactions and finally print the Information

    i am able to read the text file and store the first data segment containing the account no and balance into an object in an array
    however, my problem is reading the data segment from 0 and storing the transactions

    PLEASE HELP..ANY SUGGESTIONS..
    Regards
    aNiL


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: How to read different data segments from a text file?

    You can use regular expressions matching. Read all the text file into a string, then try to identify patterns in it, compose regular expressions and apply them. For example, one regular expression may be used to identify different data segments, other regular expression can be used to parse some particular data segment and so on.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to read different data segments from a text file?

    i am thankful for your support but using regular expressions matching is to advance for my course at this time.
    i am looking for a piece of code which would include a while loop that will loop through the data storing the account information until "0" is found, when found Read transaction segment and if DEP call a deposit method which i already have else if WID call the withdrawal method. this would run until PRINT is found and when found print all account information

    this is my code wHich is working 2 read the first segment
    public void loadAccounts(String fileName)throws IOException{
    Scanner in= new Scanner(new File(fileName));
    while(in.hasNext()){
    String no=in.next();
    double bal=in.nextDouble();
    BankAccount ba=new BankAccount(no,bal);
    }

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: How to read different data segments from a text file?

    You can't use just "0" as a delimiter: you have numbers with zeros in your data, zeros in them will be interpreted as a delimiter. So you need to use a more complex delimiter, perhaps a symbol that is never used at all in the data.
    If you want to do that without regex, one possible way may be to loop through every symbol of the read data, to remember indices of the delimiters (maybe, in an array). Then you loop through indices and read data segments between two adjacent indices.

    --- Update ---

    You can't use just "0" as a delimiter: you have numbers with zeros in your data, zeros in them will be interpreted as a delimiter. So you need to use a more complex delimiter, perhaps a symbol that is never used at all in the data.
    If you want to do that without regex, one possible way may be to loop through every symbol of the read data, to remember indices of the delimiters (maybe, in an array). Then you loop through indices and read data segments between two adjacent indices.

Similar Threads

  1. Using for loop to read in data from text file using Scanner class
    By Scippi in forum Loops & Control Statements
    Replies: 23
    Last Post: February 26th, 2013, 10:53 PM
  2. Read text file
    By cardamis in forum Java IDEs
    Replies: 2
    Last Post: November 4th, 2011, 11:59 PM
  3. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM