I have an analytics program that receives many values from a HTTP GET request and maps them into a table. My question pertains to changing the shorthand names I assigned to variables in the request into more full names before I write them to a log file. What is the best way for mapping the shorthand (e.g uid: KG) to the full names (User ID: KG )?
Currently I have a Map that puts all the relations in it ("uid": "User ID") on runtime. It uses a good number of calls to put every value in the map so I was wondering what is standard practice or most efficient, many put calls or is there a way to save a standard map to a file and load it in runtime?
Load a Properties object from a file is probably your best option. Instead of hard coding them in your program you can put them in a properties file like so:
uid=User ID
bid=Billy ID
...
THen load them using this API:
http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html
Related
I have a feature file, and in one of the steps, I'm trying to pass parameters to the step definitions using the format below:
Then the user's name and password should be displayed as what we expected
|Name |Password|
|aaa |11|
|bbb |22|
Then for the steps, I was tying to store the parameters I passed in into a map using:
List <Map <String, String> expected =dataTable.asMaps(String.class, String.class);
Because in this way I can compare this with the actual result (also stored in a map).
My question is: Can I store the parameters in the feature file into a map in the way I described? I'm asking because my code failed to compile.
Without looking at your code, it would be difficult to pin point why it is failing. However, I tried the exact scenario you posted with datatable and it works for me. Please check this gif to see the implementation
https://nocodebdd.live/datatable
I have also created NoCodeBDD, which allows you to automate BDDs without code and in minutes. Would love to get some feedback from you and other BDD enthusiasts. Basic version is free and you can download it from https://www.nocodebdd.com/download
Given two strings representing locations, how can we identify if both are same or different locations.For eg. "Bangalore, Karnataka" and "Bangalore, India" are both same
location which is Bangalore.
I have a web-based application with a text-box for "location" and user can specify values as mentioned in the above example. Input values are stored into elasticsearch and later I need to find distinct locations. For that I am using Term Facet in elasticsearch but I getting two entries,"Bangalore, Karnataka" and "Bangalore, India". Somehow I need to identify that both are same locations.
I am looking forward for java based solution for this.
I would use a geo-coding Java API such GeoGoogle. It is open source. With that, you can call:
GeoUtils.distanceBetweenInKm(firstAddress.getCoordinate(), secondAddress.getCoordinate());
I have a RuneScape Private Server project coded in Java, and am trying to code a personal "tag" that players can use. I have managed to do this, but everytime there is a restart on the server, their "tag" gets reset to "null".
Their "tag" is initalized by doing a command ";;settag [name]". Their tag is then set to whatever they want. I have done this through a string:
if (command[0].equals("settag")) {
newTag = getCompleteString(command, 1);
newTag = player.yellTag
player.sendMessage("Your tag is now:" +newTag);
}
I am unsure what the most efficient way to fix this would be, I am thinking of just loading and saving through .xml/.txt files. By the way, player.yellTag is where the next command (::mytag) searches it from, which works fine, until there is a restart of the server.
it all depends on the context of your application. If you are planning on having less than a few hundreds players, then a xml file may be ok. You should look at JAXB, which is, afaict, the standard way to store your objects in Java. You can also store them as JSON files, using gson which is way simpler to use and implement than XML stuff.
But if you get to have more than thousands of players, you may want to get some more efficient way to serialize your tags by putting them in a database, and thus an ORM library like hibernate could help you do that.
You may want to make your own stuff, like a tag directory full of files named after unique ids of your players containing the players' tag... It's a lot more "hackish" but still quite efficient.
I am developing a program which has three JTextBox which my users can enter and check some text for right rule.
So I want add a ablitiy to my program that my users can add or remove their favorite text to a Favorite List and can create folder in Favorite list and put some text in it, such as Bookmark library in FireFox or other web browser.
I want use RandomAccessFile to save favorite list as a favorite source.
How do I implemet it? is there beter way to implement it? is there beter way from RandomAccessFile?
Can any one help me?
Thanks.
There could be lots of approaches. It all depends on what you want to achieve.
Consider using Java serialization mechanism. You can serialize a collection of bookmarks to a file. When your app starts, you deserialize it, and get the same collection data.
The advantages are: simple and easy implementation. The disadvantages: you can't look through stored bookmarks in a text editor or something. The same class hierarchy is to be used to load the serialized version.
XML is human-readable and provides easy interoperability. Other applications would be able to handle your list of bookmarks.
It usually takes more resources to parse the XML and load it to memory and then to create the internal object structures. Though you can use the DOM to traverse the tree all the time, it could be not as convenient as the internal data structure using specialized classes.
Random Access Files work best with fixed record sizes. It means all the fields of your bookmarks must be fixed-length. For example, the name of a bookmark is String. When you write it out to a file, you store it like an array of a fixed length, let's say 20. This automatically implies that if users give a bookmark the name which length is greater than 20, the remaining characters would be lost.
It is also easy to implement with the caveats above. Of course the records could be of variable length, but then you lose the random access to file because you cannot easily calculate the position of a specific record.
Firefox uses JSON for storing bookmarks and allows exporting to HTML. You can explore this too.
You can also store bookmarks, and things you want to keep between sessions in the Preferences,
see http://download.oracle.com/javase/6/docs/api/java/util/prefs/Preferences.html
I'm writing a game in which user can create his own level and remove them also.
The only trouble is that I want files to be saved with names as level1, level2, level3 etc without asking user name of level.
While saving a game with level5 name it might be possible that any previous level with that name already exists. Is there any way to avoid such problems.
I mean before saving the name by which i should save should be known previously...
thanx...
You can make use of the methods provided by the java.io.File API, such as File#exists(). It returns a boolean. If it is true, then you can either append to the file, or create another one with a different filename (maybe a counter suffix?). You can append to any existing file using the another constructor of FileOutputStream taking a 2nd boolean argument which you set to true. You can also just stick to using this constructor without checking if the file exists, it will just create new one if file doesn't exist and it will just append if the file exists.
What is the expected behavior in case level5 already exists and the user tries to save level 5? Should the old level5 file be overridden? If not, what else should happen? Should the new file me saved under a different name? And how is your game later on finding this level? If there are multiple level5 files for the 5th level, how shall your game know which one to pick?
Of course you could always create a UUID (that is more or less guaranteed to be unique in practice), create a directory named after that UUID and store the files into the directory as level1 to level5. Next time the user opens the level editor you create a new UUID, thus avoiding any naming conflicts.
Or you can turn it around. You crate a directory name level1, level2, etc. and within each directory you store the files using file names that contain a UUID. That way the game can always easily present a list of all level 5 levels by going into the level5 directory and looking at all files found there.
The question is rather: How will you present those levels to the user when it comes to picking one? As you don't have names, you hardly want to show UUIDs to the user. So I wonder if it is not better to let the user name levels or set of levels (directories).
It seems that your problem is not how but what. Reconsider your goal (desired functional requirements).
If user cannot provide a unique (and meaningful) name for created level, how will he refer to that level (for example in case he wants to edit it)?
I think you should keep additional data for every level like: name (unique), author, maybe date of creation, date of last modification, etc. When user saves (creates or edits) a level he should be warned in case level with provided name exists and should choose between cancel/overwrite/try_another_name. Probably user should be prohibited from overwriting a level when he is not the author.
The best (natural) way to store and process above data is to use a database.