Hey there guys, I want to make a program that will track the number of times I have clicked a button.
Thing is, I want this to write the information down and load it every time, so it doesn't start at 0 every time you start the program.
How would I go on about writing the info and then reading it and converting it into an int object?
I am pretty much lost here :S

Recommended Answers

All 4 Replies

Create a JFrame, and a button. Button code should increase an integer variable by one. When the program loads, read the first line of a txt file and set the variable equal to the integer value of that. Do some error checking on that. Use a BufferedReader to read the file. Before exit, overwrite the file saving the variable.

I already figured that out, but I have no idea how to link the line of a text file to a variable.

  • read the lines in as a set.
  • iterate over the set
  • use the split method of String to separate the tokens
  • cast the tokens (if need be) and pass them as arguments to a constructor

Write the count variable to a PrintSream - that will write it as a String that you can check by opening the file in any text editor.
Read it back in as a String then use Integer.parseInt to convert that back to an int variable.

This is a beginner-level tutorial that covers what you need to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.