Results 1 to 3 of 3
  1. #1
    peachyco is offline Member
    Join Date
    Nov 2007
    Rep Power

    Default Reading a file for use

    Hi, y'all!

    I'm having some difficulty with storing information from my customer records. What happens after a transaction is the saving of the transaction in this format:

    Java Code:
    Customer No.
    Cashier Code
    yyyy/MM/dd (date and time stamped upon completion of transaction)
    <Item Code>:<Quantity Bought>:<Price Subtotal><Flag if item was cancelled>
    Now, at the start of the program, I must load this file to determine the last customer number assigned, so that the next customer will have the next customer no. in the sequence. My problem is how to read the file so that I can determine that last customer number. I was planning on using StreamTokenizer, but I'm unsure of how I'm gonna use it this time. Maybe read the customer numbers in an array/vector of integers and all others in an array/vector of Strings?

    Here's a sample of the customer_records.dat file:

    Java Code:
    2007/11/01 11.32.10
    1:2:10:2007/11/01 11.30.56:
    3:1:5:2007/11/01 11.28.33:
    9:3:15:2007/11/01 11.12.08:Cancelled
    2007/11/01 15.00.02
    5:5:25:2007/11/01 14.58.02:
    Thanks in advance for any suggestion/comment/advice! :)

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Rep Power


    I would use a BufferedReader. Look into it and if you need any help with it, post a reply

  3. #3
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Newport, WA
    Rep Power


    You can read the file backwards, until you find a number that is a normal Double num:
    Java Code:
    		BufferedReader reader = new BufferedReader(new FileReader(
    		String s;
    		StringBuilder tmp = new StringBuilder();
    		while ((s = reader.readLine()) != null) {
    			tmp.append(s.toLowerCase() + "\n");
    		double num = 0.0;
    		String[] arr = tmp.toString().split("\n");
                    // read the array backwards, starting with the last line
    		for (int i = arr.length - 1; i >= 0; i--) {
    			try {
    				num = Double.valueOf(arr[i]);
                                    // if the number cannot be converted to a Double, it would mean that it is one of the other lines
                                    // if it can be converted, then break out of the for loop
    			} catch (NumberFormatException e) {
    				// just for a test mess
    				System.out.println("Not on this line");
                    // just to make sure that the value we got was greater than 0
    		if (num > 0)
                            System.out.println("No customer records found. A possible error occured");
    Last edited by staykovmarin; 11-27-2007 at 03:53 AM.

Similar Threads

  1. Reading a properties file
    By peiceonly in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 05:17 PM
  2. Reading a File into ByteArray
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 03:19 PM
  3. Reading a file
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-30-2007, 12:23 PM
  4. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 PM
  5. Reading from a file
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 12:02 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