Results 1 to 4 of 4
  1. #1
    gaeronf is offline Member
    Join Date
    Nov 2012
    Location
    PA
    Posts
    7
    Rep Power
    0

    Default Variable Keeps Resetting, I need it to stop, but it is a little bit more complicated.

    I am currently working on a project that requires files to be generated from data obtained from the user. The only problem is naming the files. For instance, I need the files to be named File1.txt, File2.txt, File3.txt,etc. When I run this program (it has GUI) and keep it running it will create the files like previously described. However, if I close the program, then run it again, (I assume the variable resets) and it starts back at File1.txt, etc. I need it to, no matter if the program has stopped and started 10000 times, to keep going at that increment. Like I said, I have everything fine except the problem of the variable resetting.

    Java Code:
    try{	
                    FileWriter toFile = new FileWriter("File" + filenumber + ".txt");
                    PrintWriter out = new PrintWriter (toFile);
                    //out.println statement will go here
                    out.close();
                    filenumber++;//increases filenumber by one to change the to-be file's name 
              } catch (IOException e1) {
    	        e1.printStackTrace();
                 }
    Obviously filenumber is the variable associated with the problem I am having.
    Last edited by gaeronf; 12-01-2012 at 03:39 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Variable Keeps Resetting, I need it to stop, but it is a little bit more complica

    If you need to maintain the sequence then you have to store this sequence number in a file. This file should store the last file number that have been created. So when your program stopped the last number will be stored in this file. When you run your program the next time it first read the next sequence value from this file and increment it. This way you can always continue the sequence.

  3. #3
    gaeronf is offline Member
    Join Date
    Nov 2012
    Location
    PA
    Posts
    7
    Rep Power
    0

    Default Re: Variable Keeps Resetting, I need it to stop, but it is a little bit more complica

    Quote Originally Posted by wsaryada View Post
    If you need to maintain the sequence then you have to store this sequence number in a file. This file should store the last file number that have been created. So when your program stopped the last number will be stored in this file. When you run your program the next time it first read the next sequence value from this file and increment it. This way you can always continue the sequence.
    Sir, thank you for your speedy reply. I will do that right now, and will come back with an update.

  4. #4
    gaeronf is offline Member
    Join Date
    Nov 2012
    Location
    PA
    Posts
    7
    Rep Power
    0

    Default Re: Variable Keeps Resetting, I need it to stop, but it is a little bit more complica

    Works flawlessly. Thank you so much for your help.

Similar Threads

  1. Resetting graphics
    By PhQ in forum New To Java
    Replies: 10
    Last Post: 01-11-2012, 02:03 AM
  2. Boolean value not resetting
    By BillyB in forum New To Java
    Replies: 1
    Last Post: 03-30-2011, 04:28 PM
  3. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 01:50 PM
  4. Complicated Method
    By Desmond in forum New To Java
    Replies: 5
    Last Post: 03-17-2010, 12:31 PM
  5. Replies: 0
    Last Post: 04-12-2009, 04:06 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
  •