When will I need to serialize an object? - java

I recently watched a tutorial about serializing objects in java, but it left me wondering when can I use this?
From what I understand, the object is converted into a file, which can be converted back by another program, but could I not program the object into the other program? Please explain what this could be used for, thanks!

Example:
A user logs in to a game launcher , the launcher gets the user details and saves it as a serialized object.
When the user starts the game, the game looks for the file, gets the object and doesn't have to look online , that way the game starts much faster.
Hope you understand the use of it :)

Related

Indenting a tree file structure from string input

Click here to see a screenshot of the assignment
Here is how the Navigation.csv looks like, where I take data from
https://pastebin.com/JXnaRTzi <-- Click on the link for code - this is my code so far, I am reading the file and making objects from each entry
Guys I need help with this assignment. I chose to do it in Java, but really it doesn't matter. I need advise and help for making the right approach on this kind of problem so it can work on a larger file. What kind of data structures should I use and maybe if it's no bother, give me a solution. I am a new developer and I'm trying to get into backend and frankly I'm a bit lost.
Click on links above to see the task details.

What is the "shoutOutCannedMessage" method?

I am trying to do a homework assignment where the application asks the user to input 10 strings, then displays them all, and finally ask the user to select 1 to display a second time. For extra credit, I'm using a HashMap (named shout) to store the data instead of an ArrayList.
My professor wants me to output the 10 strings using the shoutOutCannedMessage() method.
MY QUESTION: What is the shoutOutCannedMessage() method and how do I use it in this situation? Why can't I just do System.out.println(shout);, which worked completely fine when I tried it?
I cannot find anything online about this method in terms of a general description, cannot find it anywhere in our textbook, etc.
I tried shoutOutCannedMessage(shout); to no avail. I get the error message:
cannot find symbol
symbol: method shoutOutCannedMessage(HashMap<Integer,String>)
location: class ShoutBox
This is for an online class so we don't have lectures and it'd take 48 hours to get a response via email from the professor. I've been trying to watch thenewboston's video tutorials to guide me through this class and haven't found anything relevant to this assignment.
SECOND QUESTION: How do I then proceed to have a user select just one key from the HashMap to be displayed, using the shoutOutCannedMessage() method?
Thank you very much to anybody who offers assistance!
EXACT ASSIGNMENT INSTRUCTIONS:
You will create the ShoutBox class for your Virtual World. Your ShoutBox class will have a shoutOutCannedMessage() method that returns a String type. The shoutOutCannedMessage will use an Array or an ArrayList to store 10 messages of type String. For those of you that are more advanced with your Java skills, you could use a HashMap for the data structure. You can load this data structure with 10 messages of your choosing. For example, one message could be “I need Java!” You can initialize your Array or ArrayList with the messages or have the user enter the messages. The choice is yours. The shoutOutCannedMessage() will loop through the data structure to first display all canned messages and allow the user to select one. The shoutOutCannedMessage() will return the selected message String.
I think you are missing a key concept in this lesson. The instructor wants YOU to implement the shoutOutCannedMessage() method. Of course you can't find it anywhere online, etc. because your professor made up the name for this method. It is your homework to implement it and code the method. It should be easy enough as you are using the HashMap so each time the user enters in a string it'll be put into the Hash. Then just ask the user which one they want to repeat, by entering in an integer, and if you've coded things properly it will then output their selection.

How to save map data in a tilebased game

I have a bit of a rookie question regarding storage of map data i a java game. I'm making a 2d tile-based game, where each tile has an id (1, 2, 3, 4 and so on...). I currently store the game data in a .txt file, where each number is seperated by a tab. Therefrom i read it via scanners .nextInt.
This method works fine, but it seems to give me a problem:
Since the level file is just a normal .txt file it is easily edited by anyone. This means that the user could just change a few numbers in the file with any kind of text editor and thereby easily cheat.
How would this be avoided? Should i store the map data differently or should i use some form of encryption?
One solution to this would be to store the data in a database, such as derby. Once the game has been exported as a jar file the casual player will never even see the file, and if they do get their hands on it, probably wouldn't know what to do with it.
Here is a nice tutorial on how to set up and connect to a simple derby database.
http://www.vogella.com/tutorials/ApacheDerby/article.html
good luck.
Java has a Serializable interface that can be used to save java objects.
API docs can be found here: http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
This doesn't make it impossible for people to cheat and change the maps just more difficult.
If you're really worried about it you could use some form of encryption or add a checksum to further prevent tampering.

Recalling information form an array in list form

I am trying to recall a list of employees from an array in Java. I can either choose to do them individually via their ID number or bring all employee info up at once. I know how to code everything else but I cannot find a clear tutorial on how to recall the data and project it onto the text box I have programmed into my GUI. I have the array already created in a different class but I need to get the information from that one and display it in the GUI. Any help would be appreciated. Thanks.
If you want more info just let me know.
[The image is a screenshot of the simple GUI I made.]
There are many ways to do this, but one simple way that may help you to understand this process is as follows:
Implement a store method that simply writes all data to a file in a format of your choice. XML is common, as is TSV or CSV. Some of those are very easy to use, as predefined classes exist to handle them partially or completely.
You will have to read the data you want to store from whereever you have it in your application, possibly in the text boxes directly.
Accordingly implement a restore method that reads the file and fills it back into your data structure and/or the text boxes.

Please help, my program is not accepting MySQL floats

I have to use a java program . I need to understand it and then modify it. The program has a source folder. It has a lot of java files. the program has a GUI interface. i have imported the program in eclipse and i can run the program. i want to learn and understand the code but since its too big i want to only read the relevant portions of the code which i need to understand to modify / add features to the program. This program requires input data . this data can be in a CSV file or a MySql database. the immediate problem i am facing with the program is that i need to use float data as input data in one of the columns. when i put data in decimal form in the CSV file . the program has no problem in accepting it and processing it. but when i create a MYSQL database table with floating datatype as one of the column . the program while importing data gives error - "unknown data type float . add this entry is xxx.props file " even after i add the float entry in xxx.props file in the proper way just as other entries are there for other data types in xxx.props file, i still get the same error . what should i do next ? which part of the code should i study to find out the problem?
When you say it "gives error" - does it throw an exception? If so, find out where that exception is thrown from, and work backwards from there.
You haven't explained what the xxx.props file is, how you created the mysql table, or what you're then doing with it, which makes it kinda hard to give any more specific help...
Without a direct question, I can't help you much. However, I can advise you on how to solve the problem.
Learn the basics before you try to make a leap to do everything at once. Try to learn these before coming everything:
Get a working knowledge of Java
Figure out how to develop a GUI that isn't binded to your logic. (Take a look at the MVC design pattern)
Break up the big program into manageable parts
Implement your logic separately from your data access (M C part of MVC)
Use Unit Testing to verify that your components work
Implement the CSV reading, and then move it to the database
This way you have seperable components that will make your job easier to deal with, and you will learn quite a bit along the way.
You've got a lot of small questions but I think your main question is "why am I getting this 'unknown data type float' error". Without looking at any code my first suggestion would be to validate how you're inserting/updating the MySQL table. Hopefully the developer extrapolated the data tier from the UI tier meaning there should only be one or two java classes to look at to verify how the data is inserted.
Overall, it sounds like you're new to Java and application design. Try to follow how the program works from point A to point B. Hopefully the code was broken out into tiers to make it easier for you; IE the GUI is in it's own package/project and the business logic is in it's own package/project, so on and so on.

Categories

Resources