Array that doesnt gets clear from memory when closing application - java

I need to have an int array which holds several Integer values. This array should not be clear when the user terminates the application. What I am going to use this for:
Check the last item of this array
if the last item in this array is 6, add 7 to the next item in the array
The array cannot be deleted after the user exits the application, I need this for generating a "subset" of a Unique ID.
So far I've tried using a SQLite database for holding this information, but I think this is to complex way to do it.
Any suggestions?

Well, you can use shared preference which is preferably a less complexion. But the problem is that you can't store a array object. Only primitive types can be stored.
But still here is a example which uses JsonArray to do the same. But it is not a advisory one to do though,.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
JSONArray arr = new JSONArray();
arr.put(12);
arr.put(-6);
prefs.edit().putString("key", arr.toString());
prefs.edit().commit();
// read
try {
arr = new JSONArray(prefs.getString("key", "{}"));
arr.getInt(1); // -6
} catch (JSONException e) {
e.printStackTrace();
}
Taken form here.

Try serializing the array and storing it in a File. (you can use your own logic or use some other custom library like GSON for this purpose)
Next time when the application is launched you can take the file content
and reconstruct the array back.

Related

MongoDB (Java): efficient update of multiple documents to different(!) values

I have a MongoDB database and the program I'm writing is meant to change the values of a single field for all documents in a collection. Now if I want them all to change to a single value, like the string value "mask", then I know that updateMany does the trick and it's quite efficient.
However, what I want is an efficient solution for updating to different new values, in fact I want to pick the new value for the field in question for each document from a list, e.g. an ArrayList. But then something like this
collection.updateMany(new BasicDBObject(),
new BasicDBObject("$set",new BasicDBObject(fieldName,
listOfMasks.get(random.nextInt(size)))));
wouldn't work since updateMany doesn't recompute the value that the field should be set to, it just computes what the argument
listOfMasks.get(random.nextInt(size))
would be once and then it uses that for all the documents. So I don't think there's a solution to this problem that can actually employ updateMany since it's simply not versatile enough.
But I was wondering if anyone has any ideas for at least making it faster than simply iterating through all the documents and each time do updateOne where it updates to a new value from the ArrayList (in a random order but that's just a detail), like below?
// Loop until the MongoCursor is empty (until the search is complete)
try {
while (cursor.hasNext()) {
// Pick a random mask
String mask = listOfMasks.get(random.nextInt(size));
// Update this document
collection.updateOne(cursor.next(), Updates.set("test_field", mask));
}
} finally {
cursor.close();
}```
MongoDB provides the bulk write API to batch updates. This would be appropriate for your example of setting the value of a field to a random value (determined on the client) for each document.
Alternatively if there is a pattern to the changes needed you could potentially use find and modify operation with the available update operators.

How to parse unpredictable JSON objects?

My app parse JSON from an API based on user's selection. Since each user's search might be different I can't predict what the user will search and have a established parsing structure waiting for them every time I finish a API call.
The problem is how can I have a JSON parsing structure in my app that will handle this undefined/unpredictable JSON object from the different API call?
Basically what I need is a way to handle unpredictable and unknown JSON object like they were known and I could parse their keys and the data inside them.
Thank you.
You can use ObjectMapper to convert your JSON to some key-value structure object like Map or HashMap:
Map<String,Object> result = new ObjectMapper().readValue(jsonData, Map.class);
jsonData can be file, stream, byte, string,....
More detail here: https://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/ObjectMapper.html#readValue(byte[],%20java.lang.Class)
I would help more if I had your Code but hope my explanation will work for you. I recently made an app that picks stuff entered by users on the server-side and display on phone. For instance you choose rice and fish while another person chooses rice, fish, coffee and baked beans. These are different stuff but I made an array to hold the stuff, the length of the array depends on the number of stuff you pick. then I build a json object to parse. Note: you can count the number of items the user put and safe in the variable then use that variable as the length.
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString(KEY_SUCCESS).equals("true")) {
JSONArray dataArray = jsonObject.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i++) {
JSONObject dataobj = dataArray.getJSONObject(i);
I had saved all user input with "data". One thing my answer may not fit your description because I am not sure if you are getting a list input from user or something like a long paragraph/article. I assumed you are getting a list so I gave you an option to use arrays

Fill arrays with data from block of string in java

I want to fill 4 arrays with specific data from block string
I got blocks like this
00:0035:0063:1705211023:00:
11::7027661000300976376:
99:59:07027661000300976376:::::
05:11:10000:::00 09:11:8510 07:::::1490:::
99:65:00:00:00:00:00:01000000000002140331410062269000000126300000000
99:64:00:00:00:00:00:00000355600200000000022700000000000000000001
99:01:227:1490:30:0:0:0:0:0:1:0:324
****Segundo Ticket PANGUI**** 99:00:35:63:1705211023:0:1:19353:63895896:1490:0:::::
99:150:0|1|H014|35|63|210517102100|
and I want to check if 00:.. , 05:11:.. , 99:65.. , 99:64... and 99:01...headers exists and stores data for specific field from each row, for example in line or row 99:65.. I will store the last field. If no exists one or more, I must be store zero, something like this
if exist 99:11 then Arr11 =specificfieldfrom9911, else arr11 = "0"
So that for each block have a structure or set of arrays that identifies the fields of each block
Arr00
Arr0511
Arr9965
Arr9964
Arr9901
how can I achieve this? any help would be great.
after you get the individual string you can use startsWith("") method on the strings. Eg. Assuming the string that came is on a variable called inputString you can use
if(inputString.startsWith("99:65")){
//do what you want to do
}
i hope this helps

How to save an array or object to be used in other runs

I'm working with a sensor that taking data and giving it to me whenever I call on it. I want to collect 1 minute of data in an arraylist and then save it to a file or something so that I can analyze it at a different time.
So I have something like this:
ArrayList<DataObject> data = new ArrayList<DataObject>();
public void onUpdate(DataObject d) { //being called by my sensor every second
data.add(d);
}
I want to save the ArrayList data to a file to my computer so that I can later feed it into a different program and use it there.
How do I go about doing this?
If you want to save these as CSV files, they'll be easily exportable and importable, even to Excel (which may be of value for doing further work or passing on results).
Check out OpenCSV and in particular this entry in the FAQ relating to writing the data out.
e.g.
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), ',');
// feed in your array (or convert your data to an array)
String[] entries = "first#second#third".split("#");
writer.writeNext(entries);
writer.close();
I think you should just output the values to a file with something as a delimiter between the values, then read the files into an array in a new program. To get the array into a file, loop through the array while appending each number to a file when looped through until you reach the end.
If the other program is also based on Java, you could leverage the Java Serializable inferface. Here is a tutorial, Java - Serialization.
it would be best to use ObjectOutputStream for the purpose, since the output of the sensor is a integer or double. using writeObject method method your task can be done.
see the link for a detailed reading:
http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html

how do i create an array with the name passed on to it in a method arg (in java)

I'm writing a personal program that will help my Dnd group and at the same time expand my java knowledge a little :) now part of that involves some arrays, and loading text into them from a text file. Now I have succeeded in that and with them all set statically it's all fine, since the end result will have lots of arrays I thought rather than making each array do all the leg work itself I would create an array handler method.
So I would do filetest(filename,arrayName) (ie filetest(table1,table1Array)
and it would make the array, but I'm stuck on one thing: How do I make the array using the name from arrayName?
It's pretty much got me stumped my so far failed code is:
public class arrayFileHandler {
public static void fileTest(String fileName,String arrayName) throws FileNotFoundException{
int a = 0;
System.out.println("test");
System.out.println(System.getProperty("user.dir")); //this is the folder where the file SHOULD be
Scanner testTable1 = new Scanner(new File("data/"+fileName+".txt")); //with luck this will load the file ! if i understand + correct!
//Scanner testTable1 = new Scanner(new File("C:/Dev/newjava/dnd/src/dnd/test.txt")); //this works but is no good for our needs
ArrayList<String> testTable = new ArrayList<String>(); //create the array list since at this stage we dont know how long the array will be
while(testTable1.hasNextLine()){ //see if the file we are using has a next line (could cause me issues if the txt has blank lines...hmmm)
String line = testTable1.nextLine(); //put that line into the string "line"
testTable.add(line); //add that line to the array list
System.out.println(line); //lets see what that line says
a++; //to help count the lines(not needed now)
speechHandler.speechSynth(2, 1, 0, 60, line); //a debug line
}
System.out.println("there are "+ a +" lines"); //print how many lines there are
String arrayName[] = new String [testTable.size()]; //create an array with the number of "slots" equal to the number of slots in the arraylist and named with the String in arrayName
arrayName = testTable.toArray(arrayName);//copy the arraylist to the array
System.out.println(arrayName[0]);
//System.out.println(tableList[2]);
speechHandler.speechSynth(2, 1, 0, 60, arrayName[2]); //also a debug line
}
Now the important line is String arrayName[] = new String [testTable.size()]; it's trying to create an array called arrayName, but I need it to be created with whatever name is in the string called arrayName, so in my example in the second paragraph it would be called table1Array.
Googling hasn't helped me much and I'm wondering if what I want to do is actually possible.
It's not possible and it also doesn't make any sense in your case. What's your goal? You're just creating a local variable, it doesn't matter which name did you choose. It'd only get complicated, because you'd need to access it using another variable (containing the name). I really don't see any usage in this.
If you were using some dynamic interpreted language, it could be done by using something like "eval", which would create variable with defined name at runtime. But in the Java, all code (including all variables etc) is compiled into bytecode and then executed. Do you see the problem? At compile time, the variable might not be recognized (because of missing name), and therefore it's not possible. Truth is, that bytecode doesn't contain local variable's names, but why JVM would have to solve issues like "isn't there already a variable with such a name" and so on? Bytecode would only get bigger, without actually bringing some new functionality.
If you really need this for some reason (and I just can't imagine which is that), I'd suggest you to use some sort of associative array, e.g. java.lang.Hashtable<>. It allows you to change names runtime.
From you last comment, if I understand right, you want to create arrays of strings based on the contents of text files.
You started with reading the lines into an ArrayList of Strings. I suggest you stick with ArrayLists, and not bother with arrays.
If you are going to read lots of files, I suggest you put your code which creates the ArrayLists into a method so you can call it as many times as you want, once for each file.
Below is such a function, copied from your code and modified where necessary (I removed the comments):
Here goes:
public List<String> getLinesFromFile(String filename) {
Scanner testTable1 = new Scanner(new File(filename));
List<String> testTable = new ArrayList<String>();
while(testTable1.hasNextLine()){
String line = testTable1.nextLine();
testTable.add(line);
}
return testTable;
}
You can call that as many times as you like. If you have lots of files, you can 'name' them the same as your filename by storing them in a Map:
First, create a Map whose keys are Strings (so we can look up by filename) and whose values are List (lists of strings - the content of the files)
Map<String, List<String>> fileContentMap = new HashMap<String, List<String>>();
Next, every time we read a file, add it to the Map:
String filename = // ... whatever file to read next
fileContentMap.put(filename, getLinesFromFile(filename));
Finally, when you want to retrieve them:
//retrieve lines from a file I read earlier:
List<String> lines = fileContentMap.get(filename);

Categories

Resources