Results 1 to 4 of 4
  1. #1
    providence is offline Member
    Join Date
    Oct 2010
    Location
    Canada
    Posts
    20
    Rep Power
    0

    Default Saving information to access later

    Hello,

    I'm rather new to both these forums and java. At the moment I'm trying to write a program to help with the day-to-day of my restaurant. Everything is going surprisingly well, except I'm having serious trouble getting my program to save figures and re-access them on another run of the program. What happens is java deletes and re-creates the .txt file I've stored my information in every time I run, then re-creates it.
    The goal is to have this file store the 'double' amount I've saved for taxes each day separated by a blank space. Therefore I can get the program to access these numbers and add them up so I can see how much I've saved in total.
    Any help on how I can do this will be greatly appreciated.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    I'd need to see some of the code with how you have set up your File Reader and Writer.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    providence is offline Member
    Join Date
    Oct 2010
    Location
    Canada
    Posts
    20
    Rep Power
    0

    Default

    Below is how I have it set up at the moment:

    PrintStream diskCarry = new PrintStream("carryOverStore.txt");
    Scanner diskCReader = new Scanner(new File("carryOverStore.txt"));

    Then, in any given run, I write something to the 'carryOverStore.txt' file:

    diskCarry.print(carryOver);

    And I'm trying to use this to read the file in a later run:

    if (diskCReader.hasNext()) {

    prevTtl = diskCReader.nextDouble();

    } else {

    prevTtl = 0.0;

    }

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Without seeing the rest of your code, all I could come up with was a little example for how to receive the doubles from the file.

    Java Code:
    public static double readFromFile(String fileName)
    {
    	double data = 0.0;
    	try
    	{
    		File inFile = new File(fileName);
    		Scanner getDouble = new Scanner(inFile);
    		while(getDouble.hasNextDouble())
    		{
    			data += getDouble.nextDouble();
    		}
                    getDouble.close();
    	}
    	catch(Exception e)
    	{
    		return data;
    	}
    	return data;
    }

    It uses basic I/O to read each double from the file and keep a running total of the sum. If nothing is in the file it returns 0.0. Hopefully you can use this layout to get an idea for how to write information back into the file.
    Last edited by joshdgreen; 10-31-2010 at 12:01 AM.
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Saving and loading information
    By applewood13 in forum New To Java
    Replies: 1
    Last Post: 09-30-2010, 06:29 AM
  2. Replies: 0
    Last Post: 08-02-2010, 11:26 AM
  3. Default Access (package access) confusion
    By gauravrajbehl in forum New To Java
    Replies: 1
    Last Post: 11-18-2009, 11:48 AM
  4. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 07:20 PM
  5. Replies: 1
    Last Post: 08-07-2007, 08:51 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
  •