How to save data in a Java program - java

Is there a way to make a collection of class files/objects and then have them used in an interactive main file? So let's say I want to make a program to store information interactively where different classes are designed to hold different information. Then I would have an interactive main file where I made instances of these classes which would collectively hold the information I want stored. And then any changes or anything I do in this interactive main file is then saved.
I understand that this might be a very odd inquiry and maybe some other program might be useful for this. If so, feel free to point me in the right direction.

Here are two solutions that are good for the purpose you mentioned in your comment.
The first one is called Serialization. This let's you save your java object to your hard drive, and retrieve it later.
The second, (and in this case, more preferable option in my opinion), is using a Database.
A database is a compliment to your program, that stores data. You can then use "Queries" to access this data, and update it. Almost every database software is compatible with java.
I would look into MySQL
The reason I think a database would be better for your purpose is that they are already highly optimized, and are designed to have multiple people accessing and writing to them at once. If you wanted just want one person to use this program at a time however, serialization might be easier to implement.

Absolutely! Your main class would use the standard input (perhaps Scanner input = new Scanner(System.in);) and output (System.out.println()). To interact with your other classes, most simply, just put them in the same folder (if you are interested take a look at Java packages). If you have a Dog class in the same folder as your main class, you can freely create Dog objects in your main class. I hope this helps!
As a side note, because you mentioned storing information with different classes, you might be interested in the Java Collections Framework.

Related

Saving all results in Scala REPL

Is there an easy way to save all the values of variables in scala REPL?
There is :save command in scala but it just saves the history of commands and the next time we need to recalculate everything from scratch.
I know that I can manually serialize/de-serialize everything I'm interested in, but there are two main difficulties (also applicable to java):
It is hard to manually write serialize/de-serialize code for every defined (Serializeable) variable and it is not extensible for later use.
It is only possible to save Serializable objects. I know that saving (hibernating) an arbitrary object may results in problems (especially for objects working with external resources), but whether there would be a problem or not, depends on the state of the program. Sometimes the programmer is sure that in the current situation there would be no problem saving the variables. I think there should be a way for the programmer to take the responsibility of saving everything, even the objects not explicitly defined Serializable.
I appreciate answers solving any of these problems.

How to write a program that dynamically creates Objects?

I am trying to write code for my latest class assignment where we create a Harness Record System, the code needs to keep track of Harness's in a system, be able to create new Harnesses and edit/loan/check old ones. I have gone down the route of using a GUI rather than the console as it is easier for the user to use. Although, I'm having trouble coming up with ideas on how to write code that differentiates old Harnesses written into the code, with new Harnesses that overwrite the previous input of the user.
So my question is, is there any way to write a code that can create objects once the user clicks a certain button, so that when that button is clicked the information inputted by the user into that object comes up, rather than just the most recent input of the user.
I know this is a silly and badly worded question, but my brain is just fried at the moment, I'm in need of help.
My code is below, I'm using two different classes and the SWT kit, if anyone could have a look at my code and help me out I would be so grateful, really struggling to find a way to overcome this:
Harness Class: http://pastebin.com/HqJqGfTN
HarnessSelection Class: http://pastebin.com/EE4C2WCs
The 'new' keyword is how you create new object instances. I haven't read all of your code but make sure you understand what the 'static' keyword does - it makes all instances of that class share the same value for that field. Perhaps all of your harness instances are sharing the same static variables.

Permanently saving the value of a string

I am currently making a flash card program, and I want the user to be able to put in their own questions and answers, then test themselves. My only problem is that i want the values they enter to be there permanently until changed by them. How do I do this? (P.S: if you need the code, I can give it.)
I assume that you are new to programming and you have not yet worked with persistence of any context. In this case, for your simple example, the Java Properties class might be a good entry point into the field of file persistence.
In general, there are plenty of ways to persist data: databases, files, web storage, etc... It depends on your application and what you want to do with the data. For an example of the Java Properties file see for example this tutorial: http://www.mkyong.com/java/java-properties-file-examples/
Well without seeing any of the code, it is hard to tell you exactly how you should approach this, but in general you will need some method of persistence to save this type of information, ie. a database, flat file, etc. Try looking into sqlite or even XML for storage and retrieval.

Java- Quitting the programme midway and resuming where you left off?

I am doing a java project and have several methods. Would like to know if its possible to quit the programme after the 3rd method for example and then when resuming the programme it continues from there? Any information regarding this would be helpful
THank you
Not without some kind of external persistence mechanism, no.
Once the process is done, it's done.
Why do you want to do this? There may be a better solution.
Otherwise, it would be along the lines of saving data in some particular format (JSON, YAML, etc..) and then have a parser that could determine where the last program left off. It's somewhat vague, so it also depends on what you're attempting to "save".
Potentially could do something hacky if you ran the program with a wrapper.
Edit:
You could serialize your classes, which in turn would allow you to be able to save the fields of any objects you have, however everything you have would need to implement Serializable and be saved to a file.
So after a little bit of googling, I found that there is also the XStream library, which serializes objects to XML without needing to implement an interface. In that sense, you could serialize objects themselves, but wouldn't be able to, say, continue from the middle of a method. (Having a string you can parse to start in different locations could be one option).

Java Object Oriented Design

I'm just starting to learn and the books say that I can use "pre-coded" classes and objects. How do I call these classes/objects in the beginning of my program? I understand basically what they are, and the idea that I can use these classes/objects in place of writing fresh code every time, but I cannot seem to figure out where I find these things and how I implement them.
You certainly talk about the Java classes that come in JRE/JDK.
Those are used by including the jar in your classpath and provides the "default" java classes.
Like String in java.util package.
If you want to look at them, in the JDK you'll find the sources of these class.
"Pre-coded", or pre-written Java classes, are pretty much the same concept as the Java API - someone has written the code for you, was kind enough to document how you can use the code, and you may create instances (as necessary) through the prescribed way.
Say, for instance, I want an ArrayList holding Strings. I would then code ArrayList<String> words = new ArrayList<String>(). You wouldn't have to go through the process of writing a dynamic self-expanding vector.

Categories