Java - Weird behaviour after implementing statistics

Good. So after figuring some stuff out I came up with this solution to my problem. It asks the user where to generate the directory and its files. Then, everytime the application gets opened it increments the integer in timesPlayed.txt by one.

http://pastebin.com/2FU5HkYG

So far so good. I then implemented this in a small game I made. When I tried to execute it however it gave me a NoSuchElementException error at a scanner in an other class in the game. When I disable the statistics class however everything works fine. 

I would also like to keep the path to the directory stored somewhere so the application won't have to ask for it every time it gets executed.

If someone could help me it would be greatly appreciated. There are not as much Java dudes at the LinusTechTips forum. :)

Thank you.

You have a couple of things wrong with your code that could cause a NoSuchElementException.  First, in your create file you do not check to ensure you have an initial value.  There could be the case where the file exists, but there is no value.

I cannot see how you are using the class in your game code, but all of your data members and methods are static.  I assume you made them static so you can call them from main. You should make most, probably all non-static and most of your data members non-static as well, and create and use object instances of Modify.

What is most likely causing the NoSuchElementException is that your Scanner and PrintWriter are stepping on each other causing the exception.

You should also restructure your code, making all your path setup and directory and file creation as part of your constructor and make those methods private.  This may fix your problem.

Take a look at this restructuring of your code:

http://pastebin.com/sqenAW9Q

If this does not solve your problem, you may need to ensure you only read the value from the file once--during initialization, store that as a static data member.  Then when you update the number of times played, increment the static value and write the updated value to your file.

Another unrelated problem with your code is that you do not check to ensure your directory path was actually created.  If the directory creation fails, this could cause additional problems with your code.

If you do not want to read the storage directory from the command line, the easiest thing to do is create a Java properties file and read the directory location from there.  You can read properties files from either a specific location on the file system or from the classpath.  Reading it from the classpath is usually better.  Reading a properties file from the classpath means that when you deploy your application, you do not have to worry about having to create a directory structure and copying the properties file to that particular location.  All you have to do is package your properties with your application or just put the properties file somewhere on your application's classpath.

Here is your code restructured to read the storage directory location from a properties file:

http://pastebin.com/b5yLDt1U

Sample properties file:

http://pastebin.com/R6132AJ7

Thank you for your help. I already solved my problem in a different way, but I have seen how to improve it after your suggestions. I will start working on it.

Here is the solution I came up with by myself, I will start improving it right away. http://pastebin.com/kGzSc8ZD