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 7 of 7

Thread: Keeping Variable Values

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Keeping Variable Values

    Hello, I was just wondering if it's possible to keep the variable values every time someone runs the program without using anything web based. So for example: x=0, when the person runs the program x=1. The next time the person starts that program I want x to = 1. Is this possible?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Keeping Variable Values

    It may be. If you read in the values from a text file. The trouble is that they'd have to have the text file I think somewhere on their computer. Also, if they got rid of it or it wasn't in the same directory as the text file, it couldn't find it. Also, I fear that you'd have to create a text file to start with.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    The_Mexican (November 26th, 2010)

  4. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Keeping Variable Values

    Thank you, this works.

    EDIT: Okay, so this now brings up the problem of being able to always have the location of the file the same. So for me it would be C:\\UserName\ThisPartIsProbablyTheSame but how do I get the name given to the username each time? Is that possible?
    Last edited by The_Mexican; November 26th, 2010 at 05:18 PM.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Keeping Variable Values

    You would have to keep track of what OS the user is running (see: Find Operating Systems Information,Java Code to Get Operating System Information)

    Depending on their OS, where the standard user data location is different, and how to get this information is also different.

    Ex.:
    For windows 7 (possibly older versions of Windows too, don't know about this for sure), you can find the user's profile with the environment variable USERPROFILE

    System.out.println("The current user's profile is located at " + System.getenv("USERPROFILE"));

  6. #5
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Keeping Variable Values

    Thanks! I solved the problem using:
    (System.getenv("USERPROFILE")+"\\Downloads\\txtfile.txt"));
    I haven't actually tested this, but I assume it'll work for at least all windows users.

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Keeping Variable Values

    Load the file relative to the application and not he users hard drive, this will remove any platform specific paths. Simply create a dummy file, then do some string operations to get the true file's path...assuming your application is distributed as a jar:
    File file = new File("temp");//create a file - this will be in the same directory as your jar
    String dir = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4 );//remove the temp - this gives you the directory
    File myFile = new File(dir + "myfilename.txt" );
    //read file, or create a new one if it does not exist

  8. The Following User Says Thank You to copeg For This Useful Post:

    The_Mexican (November 27th, 2010)

  9. #7
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Keeping Variable Values

    Ah thanks copeg! So this will make it so that no matter where the text file resides, the program will find it, cool.

Similar Threads

  1. Keeping objects in webservice
    By ashleypursglove in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 16th, 2010, 12:02 PM
  2. Able to set, but not get values
    By Simple in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 23rd, 2010, 04:01 PM
  3. keeping an drawing centered in Graphics
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 27th, 2010, 11:26 PM
  4. How to share variable values amongst different classes?
    By igniteflow in forum Object Oriented Programming
    Replies: 8
    Last Post: August 20th, 2009, 08:53 AM
  5. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM