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

Thread: Problem changing values in a save file.

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem changing values in a save file.

    Sorry if I sound like I don't know what I'm talking about, but anyways. I'm trying to change the value of bricks in my save file and for some reason it can't read the information.


    	public void addRecords(int brick, int log, int stone, int house){
    		x.format("%s%s\n%s%s\n%s%s\n%s%s", "brick", " "+brick, "log", " " +log, "stone", " "+stone, "house", " "+house);
    	}

    This writes information to my files. ^

    	public void addResource(String resource, int amount){
    		openFile();
    		readfile check = new readfile();
    		switch(resource){
    		case "brick":
    			brick = check.readResource("brick");
    			System.out.println("original brick: " + brick);
    			brick = brick + amount;
    			System.out.println("after math" + brick);
    		break;
    		addRecords(brick, log, stone, house);
    		closeFile();
    	}

    In my case "brick", It doesn't set the value when I try to read it. Here is the readResource method.

    	public int readResource(String resource){
    		System.out.println("Looking for " + resource);
    		String res = resource;
    		openFile();
    		while(x.hasNext()){
    			String a = x.next();
    			String b = x.next();
    			if( a.equals(res)){
    				closeFile();
    				int c = Integer.parseInt(b);
    				return c;
    			}
    			if(x.hasNext() == false){
    				closeFile();
    				System.out.println("Resource not found.");
    			}
    		}
    		return 0;
    	}

    If anybody could help explain this to me I would appreciate it.

    Short version:

    brick = check.readResource("brick");
    This doesn't set brick to any value.
    Last edited by LavaJava; July 26th, 2014 at 05:18 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,290
    My Mood
    Mellow
    Thanks
    191
    Thanked 668 Times in 655 Posts

    Default Re: Problem changing values in a save file.

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Add some more prints to you readResource() method to determine what is happening. Is the file being opened? Is the while loop executing? Are a and b being assigned? If so, what are they being assigned to? Once you've confirmed what is and isn't happening in that method, you can continue troubleshooting from there.

    Others might also suggest using a debugger if you have one to monitor what the program is doing or not doing.

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

    LavaJava (July 26th, 2014)

  4. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem changing values in a save file.

    Thank you for the response!

    I have come to the conclusion that the while loop is not running. When I call the readResource() method from the main function it works just fine, but when it's called from another method it can't find the next line.

    Still testing... The text file is going blank for some reason...

    AMAZING!!! Turns out that I was opening the file as a formatter!
    Last edited by LavaJava; July 26th, 2014 at 07:43 PM.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,290
    My Mood
    Mellow
    Thanks
    191
    Thanked 668 Times in 655 Posts

    Default Re: Problem changing values in a save file.

    I'm not sure how you got there, but congrats on figuring it out.

Similar Threads

  1. Replies: 0
    Last Post: February 20th, 2014, 11:34 AM
  2. [SOLVED] Issues with changing values in for loops
    By ryan.boykin in forum Loops & Control Statements
    Replies: 3
    Last Post: August 4th, 2013, 09:50 PM
  3. Replies: 1
    Last Post: July 26th, 2013, 03:25 AM
  4. problem while changing file data
    By badhen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 17th, 2012, 01:19 PM
  5. values of variables changing despite not modifying them
    By AndyKow in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2011, 12:39 PM