Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Norm

Thread: Saving variables in applet?

  1. #1
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Saving variables in applet?

    Is there any way to save variables while I'm using applet as single runnable .jar file?
    For example if I start app first time some variable has value of 100. While using app it changes to 200. After closing app it disapear and next run gives me 100 again instead of 200. Is there any way to save that 200?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Saving variables in applet?

    Applets typically don't save anything locally. Some applets use a database on a server where they could save data.
    Nozz likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: Saving variables in applet?

    So, if I want for example save some things even in .txt file, I can't do that in applet. Should I make application then instead of applet?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Saving variables in applet?

    Applets are for a web app to be used by many users in a browser.

    What are the planned users for your app?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: Saving variables in applet?

    Ok, doesn't matter.
    Back to the main topic.
    I got this:
    Java Code:
    		private Formatter x;
    		private Scanner y;
    		
    		public void openFile()
    		{
    			try
    			{
    				x = new Formatter("config.ini");
    				y = new Scanner(x);
    			}
    			catch(Exception e)
    			{
    	            System.err.println("File does not exist, or exist?"); // error text
    			}
    		}
    		
    		public void writeFile()
    		{
    			x.format("%s", "bestPoints = "+bestPoints);
    			x.format("%s", "\nachiCount = "+achiCount);
    		}
    		
    		public void readFile()
    		{
    			while(y.hasNext())
    			{
    				bestPoints = y.nextInt();
    				achiCount = y.nextInt();
    			}
    		}
    		
    		public void closeFile()
    		{
    			x.close();
    		}
    And there is small problem.
    Saving ints to file is working. After closing program in config.ini is everything fine.
    But after starting program again it restarts file and writes 0 again instead of reading int that is in file.
    I tried few things, but nothing helps here. How to do this?
    I'm calling it in this order:
    openFile();
    readFile();
    writeFile();
    closeFile();

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Saving variables in applet?

    Make a small, complete program that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: Saving variables in applet?

    Well, I can't even handle easiest read and write...
    Java Code:
    package test;
     
    import java.util.*;
    import java.io.*;
     
        public class testConf
        {
            private Formatter x;
            private Scanner y;
            int a;
    
            public void openFile()
            {
                try
                {
                    x = new Formatter("config.ini");
                    y = new Scanner("config.ini");
                }
                catch(Exception e)
                {
                    System.err.println("File does not exist, or exist?"); // error text
                }
            }
    
            public void readFile()
            {
    
                System.out.println("Not Readed: "+a);
    
                while(y.hasNextInt())
                {
                    System.out.println("- READ -");
                    a = y.nextInt();
                }
                System.out.println("Readed: "+a);
    
            }
            
            public void writeFile()
            {
                System.out.println("- WRITE -");
                x.format(""+a);
    
            } 
            
            public void closeFile()
            {
                x.close();
            }
    
        }
    Java Code:
    package test;
     
     
    public class main {
        public static void main(String args[]){
         
        testConf testConf = new testConf();
         
        testConf.openFile();
        testConf.readFile();
        testConf.writeFile();
        testConf.closeFile();
         
        System.out.println("END a: "+testConf.a);
        }
    }
    Sometimes that gives me an error:
    ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
    JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [util.c:838]
    No matter what I write in file manually.
    Always console gives me:
    Not Readed: 0
    Readed: 0
    - WRITE -
    END a: 0

    And an empty file after all.


    While I changed int variable to String, it gives me filename wrote in file and in console.
    It seems like first thing after runing is delete everything from file. Then read file(if int, gives 0, if string, gives filename). And then write it to file(if int, gives 0, if string, gives filename).

    And one more thing.
    While I delete everything about write, read works.
    And if I delete everything about read, write works...
    Last edited by Nozz; 04-14-2014 at 01:29 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Saving variables in applet?

    Try doing some debugging by printing the values of the Scanner methods that are used to control the loop.

    Also try commenting out the classes and methods that write the file to test only the reading logic. Edit the config.ini file and put a number in it.
    Last edited by Norm; 04-14-2014 at 01:45 AM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: Saving variables in applet?

    What?
    Loop? What loop?
    K. I'm tired, going to sleep.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Saving variables in applet?

    While I delete everything about write, read works.
    And if I delete everything about read, write works...
    Yes, you can not mix them as you are trying to do.
    Read the API doc for the classes to see what the constructors do.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,165
    Rep Power
    6

    Default Re: Saving variables in applet?

    Quote Originally Posted by Nozz View Post
    Loop? What loop?
    Line 30...

    Java Code:
    while(y.hasNextInt())
                {
                    System.out.println("- READ -");
                    a = y.nextInt();
                }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 03-11-2012, 03:13 PM
  2. Replies: 1
    Last Post: 03-08-2010, 07:25 PM
  3. Replies: 0
    Last Post: 02-26-2009, 04:13 AM
  4. saving variables to the harddrive
    By Talon876 in forum New To Java
    Replies: 1
    Last Post: 11-02-2007, 02:30 AM
  5. Saving Variables
    By Fish in forum New To Java
    Replies: 6
    Last Post: 06-25-2007, 09:20 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
  •