Results 1 to 12 of 12
  1. #1
    Swiftnsilent is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default [Solved] Problem: Read and Writing Variables to a txt file!

    Hello, all! This is my first post and I have a question. I recently learned how to write and read from a text document. Which is what I want to do... but I cant write Variables for some reason or I do not know how. My purpose for this is to run a program and it saves some variables into a txt doc and later can read from that and put them back into play. All i have here is the reading and writing and that is what I need help with.

    This is to write to the file
    Java Code:
    import java.io.*;
    import java.lang.*;
    import java.util.*;
    
    public class WritetoFileEx
    {
    public String Health;
    	private Formatter x;
    	
    	public  void OpenFile()
    	{
    		try{
    			x = new Formatter("Saves.txt");
    			}
    		catch (Exception e){
    			System.out.println ("ERROR");
    			}
    	}
    	
    	public void addVariables()
    	{
    		x.format("%s%s%s", "lol ", "Yes ", "Health "); 
    //What do I put here for reading ints? or some variable nothing has worked
    	}
    
    	public void closeFile()
    	{
    		x.close();
    	}
    
    }
    This is to read it
    Java Code:
    import java.io.*;
    import java.lang.*;
    import java.util.*;	
    	
    public class ReadFileEx
    {	
    
    private Scanner y;
    
    public void openFile()
    {
    	try {
    	y = new Scanner (new File("Saves.txt"));
    	}
    	
    	catch (Exception e){
    		System.out.println ("ERROR");	
    }
    
    }
    
    public void ReadFile()
    {
    	while(y.hasNext()){
    	String a = y.next();
    	String b = y.next();
    	String c = y.next();
    	System.out.printf("%s %s %s\n", a,b,c);
    }
    
    }
    
    	public void CloseFile()
    	{
    		y.close();
    	}
    }
    This is the main class
    Java Code:
    public class WriteReadTest
    {
    		public static void main (String[] args)
    	{
    		WritetoFileEx test = new WritetoFileEx();
    		ReadFileEx Read = new ReadFileEx();
    		test.OpenFile();
    		test.addVariables();
    		test.closeFile();
    		Read.openFile();
    		Read.ReadFile();
    		Read.CloseFile();
    	}
    }
    If I did not explain my problem well, Please leave a response and I will try to clarify it! Thanks in advance! :D
    Last edited by Swiftnsilent; 05-05-2011 at 11:26 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm not quite sure what you are looking to do. Please do explain. You should be able to write primitive data to a file pretty easily. Check out the Formatter API for more information on methods. I usually would use some sort of output stream(print writer possibly for easier writing.)

    Formatter API

    for instance, this is how to represent an int
    Java Code:
    int x = 5;
    x.format("%d", x);

  3. #3
    Swiftnsilent is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    It is now saying it cant find symbol

    Edit*


    public void addVariables()
    {
    x.format("%d", z);
    }
    WritetoFileEx.java:23: cannot find symbol

    it is declared as public int z = 5; in the main class
    Last edited by Swiftnsilent; 05-04-2011 at 02:13 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Anywhere you put %s looks for a string in the argument list. You can use the variable hello if you initialize it.

    If you use different flags %d, and others it will let your write other primitive types.

  5. #5
    Swiftnsilent is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Regarding my last post anyone have any insights into that?

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    If you have int z=5 in your WriteReadTest class, it is only directly visible to that class. Public variables are not global. The public only refers to what can access that variable; private variables are only accessible by that class, while public variables can be reached by other classes if you have an instance of that class. I realise that might sound a bit confusing, so here's a quick example:

    PHP Code:
    class Firstclass
    {
      public int x=5;
      private int y=6;
    }
    
    class Secondclass
    {
      Firstclass first=new Firstclass();
      public int z=7;
    }
    Now, in Secondclass, you can use first.x to get 5, but you cannot use first.y to get 6. You cannot, however, get the Secondclass-variable z from Firstclass as it looks right now, nor are there any global variables that can be reached directly from any class.

  7. #7
    Swiftnsilent is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    I changed my design to have the writing methods in the same class, below the main method, the int i want to write are in the main method. yet i still get the error:

    SimpleRpg.java:247: cannot find symbol
    symbol : variable PHealth
    location: class SimpleRpg
    x.format("%d%d%d%d", PHealth, PMagic, PSwords, PGold);

    All I want to do is to write these variables to a text file and then in another command read them and put them back into use. It is not finding them for some reason? anything I am missing (they are declared)

    Java Code:
    import java.io.*;
    import java.lang.*;
    import java.util.*;
    
    public class SimpleRpg
    {
    		private Formatter x;
                    private Scanner y;
    	public static void main (String[] args)
    	{
    	
    		int Travel = 0; //Main Switch
    		boolean Loop = true; //Main Loop
    		boolean FirstTime = true;
    		Scanner input = new Scanner (System.in);
    		Random Gen = new Random();
    		int MonsterGen;
    		int Towncon;
    		int Shopitem; //Shop
    		
    		//Player Variables
    		String Pname;
    		int PHealth  = 20, PMagic = 5, PSwords = 1, PGold = 6;
    		String RoleChoice = "";
    		int PChoiceAttack = 0;
    		int AttackChoiceint = 0;
    		
    		//Monster Variables
    		int MHealth = 1;
    		int MAttack = 1;
    //code continues
    Java Code:
    //code above 
    } //Main Method Ending Bracket
    	
    public  void OpenFile()
    	{
    		try{
    			x = new Formatter("Saves.txt");
    			}
    		catch (Exception e){
    			System.out.println ("ERROR");
    			}
    	}
    	
    	public void addVariables()
    	{
    		x.format("%d%d%d%d", PHealth, PMagic, PSwords, PGold);
    	}
    
    	public void closeFile()
    	{
    		x.close();
    	}
    
    	public void openFile()
    {
    	try {
    	y = new Scanner (new File("Saves.txt"));
    	}
    	
    	catch (Exception e){
    		System.out.println ("ERROR");	
    }
    
    }
    
    public void ReadFile()
    {
    	while(y.hasNext()){
    	int a = y.nextInt();
    	System.out.printf("%d\n", a);
    }
    
    }
    
    	public void CloseFile()
    	{
    		y.close();
    	}
    
    	
    	
    } //Class ending Bracket
    Any suggestions? what is the best way to go about this?
    Last edited by Swiftnsilent; 05-04-2011 at 11:15 PM.

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Variables you want to access in more than one method should be declared as class member variables, not local variables.

    Class code should not go in the main method, it should go in the class constructor. Use the main method to create an instance object of your class.

    The main method is known as an 'entry point', it's where you put code that can create an instance of the class for running an application or for testing the class.

  9. #9
    Swiftnsilent is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    How do i declare it as global variables, or class variables, or whatever lets me use the same variables in different classes?

  10. #10
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    There are no global variables. The best you can do is pass an instance of your class along to other classes to access its public variables.

  11. #11
    Swiftnsilent is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks all! I have it working and it is saving and reading ints and strings! I fixed my main problems and learned a lot in the processes, much thanks!

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad you solved it, you can mark your thread solved with the thread tools at the top of the page.

Similar Threads

  1. Store links in text file and read into variables
    By africanhacker in forum New To Java
    Replies: 1
    Last Post: 03-31-2011, 05:18 PM
  2. Replies: 4
    Last Post: 11-03-2010, 06:17 PM
  3. writing in file read from another file..
    By UJJAL DHAR in forum New To Java
    Replies: 6
    Last Post: 05-07-2010, 05:41 PM
  4. Writing to file problem
    By PeraPisar in forum Advanced Java
    Replies: 12
    Last Post: 09-16-2009, 11:00 AM
  5. Problem while writing xml file
    By Rajesh Sahu in forum Java Servlet
    Replies: 0
    Last Post: 12-23-2008, 04:53 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
  •