-2

I wish to implement a variable (in Java) whose value is either stored somewhere or is not reset every time I run the program. It's related to a "Booking reference Number" for a flight program. I know database connectivity but make a new data base for one variable is pretty pointless. Any ideas as to what I should/can do?
Also I don't want the numbers to be random I want them in order like if the first booking ID is 100 then the next one should be 101 and so on.

OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
Potterapple
  • 55
  • 2
  • 9
  • 8
    Write it to a file. Read it from the file. – Sotirios Delimanolis Jun 29 '15 at 17:00
  • Is there no other way than this, since this will add another file to the program. – Potterapple Jun 29 '15 at 17:03
  • 6
    Hire a person to remember the number between runs. You will need something external no matter what. – Sotirios Delimanolis Jun 29 '15 at 17:03
  • Sure, write it to some storage on the web, E.g. Google Cloud Storage. It's not going to be easier than writing to a file though. – Andy Turner Jun 29 '15 at 17:04
  • Ah okay thanks. Ill have to do that now. – Potterapple Jun 29 '15 at 17:06
  • I think op want something similar to the resource file in c#, where you can save automatically variables in some "hidden" files. Unfortunately, I'm not familiar with java, so someone else have to explain – asdfasdf Jun 29 '15 at 17:09
  • btw, note that sequential booking IDs are a dangerous idea to do in a file. What if the recorded (previous) booking ID is 100, and then two people run the program at the same time? They'll both get booking ID 101. If you want sequential IDs, a database is probably your best bet. – yshavit Jun 29 '15 at 17:10
  • @yshavit Tough that kind of situation will not arrive since this just a personal project but yes thanks for the tip it will come handy later on! – Potterapple Jun 29 '15 at 17:13
  • @asdfasdf, chances are that feature is just using [OS-specific hidden files](https://stackoverflow.com/questions/1294989/make-a-file-folder-hidden-on-windows-with-java). Perhaps the C# environment has special support for managing these files, but I bet the idea is the same. – DavidS Jun 29 '15 at 17:14

2 Answers2

1

You should write the variable to a file and then read it from the file the next time you run the program.

Daniel
  • 6,595
  • 9
  • 38
  • 70
1

Organize your data in a structure and then serialize it.When you re-run your program, look for that serialized version in the file system, if there is any, read it. Viola.!

Alp
  • 3,027
  • 1
  • 13
  • 28
  • Can you please expand a little on this? I'm slightly new to Java. I can make it into a Structure but how to serialize it? and what is serialization? – Potterapple Jun 29 '15 at 17:04
  • 2
    Viola? Aww, I was thinking of oboe. – Andy Turner Jun 29 '15 at 17:05
  • @Potterapple Serialization means converting the variable into some form that can be written to a file. – Daniel Jun 29 '15 at 17:05
  • 1
    Google "Java serialization", @Potterapple. There are many tutorials. You'll discover that it has several uses, and the use Alp has described is one of them. – DavidS Jun 29 '15 at 17:06
  • Ill use files for now until I understand what serialization means. Thanks. – Potterapple Jun 29 '15 at 17:10
  • @Potterapple Check out [this tutorial](http://www.tutorialspoint.com/java/java_serialization.htm) please. I am sure you can find more as DavidS suggested – Alp Jun 29 '15 at 17:11