Automatic adding of elements to an array - java

Hi I'm not particular good at Java so please bear with me. I'm trying to write a very simple android app now and I need help with some coding.
Thing is, I have a server that automatically generates .png files and saves them to a public directory in a numerical order. The update occurs daily and is non-exhaustive.
Is there anyway in which I can assign the dynamic values to an array within my app?
private String[] myRemoteImages = {
"http://hypotheticalurl1.png",
"http://hypotheticalurl2.png",
"http://hypotheticalurl3.png",
"http://hypotheticalurl4.png",
"http://hypotheticalurl5.png",
"http://hypotheticalurl6.png",
"http://hypotheticalurl7.png",
"http://hypotheticalurl8.png",
"http://hypotheticalurl9.png",
"http://hypotheticalurl10.png",
"http://hypotheticalurl11.png",
"http://hypotheticalurl12.png",
//...blah blah blah
// these are all dynamically created so I won't know what is the last number on the list
};
This array will eventually be used to get the images from my server using the app. It works so far but that's only with hardcoded URLs. I would like the URLs to be dynamic, as the number of images will change from day to day.
I'm doubting that regex will work well in Java but then again I'm no expert. Was thinking of perhaps writing a script on the server end that generates a list of existing values and somehow parsing that with the android app.
Can anyone point me in the right direction? Thanks in advance.
Clarification:
The array doesn't have to be dynamically sized while the app is running.
I need a way to read the list of existing images in a remote directory and pass that information to populate the array automatically at runtime.
Resolved
Guys, thanks for the help. Sorry if I wasn't clear enough.
I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.
After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.

thanks for the help. Sorry if I wasn't clear enough.
I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.
After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.

With an array you can :
change the elements of the array
but you can't :
add or remove elements. The number of elements if fixed in an array. Some workaround can be found like putting null values and discarding theem when using the values in the array. But that's more troublesome than really useful.
On the other hand, if you want a full dynamic "array" : use a list (java.util.List). An ArrayList would be interesting here, or even a Vector as you will probably need some multihtreading around this array. With a list you can add and remove elements, size can vary and elements can be replaced.

I'd use an ArrayList in this case. You don't have to know the number of elements you want to add then and it's very simple to append elements at the end.
private List<String> list = new ArrayList<String>();
Then simply add elements by
list.add("http://hypotheticalurl1.png");
Regards,
Patrick

instead of using Array of String
use ArrayList<String> It will gives you more flexibility on adding and removing item on runtime refer this link...http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html
hear you can find example on arraylist...http://www.java2s.com/Tutorial/Java/0140_Collections/0160_ArrayList.htm
hope that helps

According to your scenario you need to have the followings:
1- a Web Service which has a method to get you the list of the available image names.
2- You need a web service client for your android application, I suggest you to use KSOAP 2 because it is widely known and easy to implement.
(If you can't figure out how to use the ksoap in your program, I can provide you some example codes)
3- You need to use ArrayList(java.util) to hold your dynamically sized array.

Hey ytou can do it via
ArrayList stringList = new ArrayList();
stringList.add("Item");

Related

Notify when web content change

Im new to java and working on a simple application that monitor an url and notify me when a table is updated whit new items. Looking at the entire page will not work as there are commercials that change all the time and they would give false positives.
My thought was to fetch the url line by line looking for the elements. For each element I will check to see if the element is already in an arraylist. If not the element is added to the arraylist and a notification is send.
What I need support with is not the exact code but advice if this would be a good approach and if I should store the elements in an array list or if I should use a file instead as there are 2 lines of text in each element.
Also It would be good to get recomandation on what methods and libs there would be good to look at.
Thanks in advance
Sebastian
To check the site it'd probably be more stable to parse the HTML and work with an object representation of the DOM. I've never had to do this but in a question regarding how to do this another user suggested using JTidy, maybe you could have a look at that.
As for storing the information (what you currently do in your ArrayList): this really depends on what you use your application for. If you only want to be notified of changes that occur during the runtime of your program this is perfectly fine. If you want to have the information persist you should find a way to store the information in the file system or database.

Android Intents and Lists

I plan on reading several files when my app/game is created and using the information from them for the entirety of the app. I also have to write to the file at one point.
I have two files. One is a 2-column text file that I'll turn into a dictionary for fast searching. The other is a text file that has 11 columns. I'll make a dictionary out of two of the columns, and the other data I need kept as is so I can write to the columns to count the amount of times something happens in different circumstances for datamining.
Currently, I've turned the second file into a list of a list of strings, or List>. I can't figure out how to pass that around in intents. ".putStringArrayListExtra" only works for a list of strings.
Am I going about this the wrong way entirely? This is my first real Android app.
In order to store a data structure into an Intent, it has to be either serializable or parcelable. If your data structure is neither of them, you might create a class that would implement Serializable and manage it. A good example might be found here.
Once done, you then might use Intent.putSerializable(...) to store your data structure. See this:
Using putSerializable in Android
Additionally to this, if you could convert your structure into a JSON structure, you'd already have it done since it would be treated as a String. If not, the above solution should be easy to do.

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.

Java saving strings

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.

Create a method to get integers from a https web page and store it in a array

Can someone please help me create a method to get the integers (listed below) from this link and store them in an array using Java? I'm new to Java and I've searched around and I can't any info or working examples of how to do it.
The integers are:
17,04,2011,1,2,7,10,13,23,24,25,26,27,38,39,41,43,45,48,49,55,59,62
All integers will change each time the method is called.
You need to do this:
retrieve the web page. Use URL and URLConnection for this.
Parse the HTML. It looks like this is an XHTML page, so you could use any XML API for this. As an alternative, for this simple structure something like a regular expression that throws away anything which is in <...> could work, too.
put the numbers from the HTML into a new array. You might want to count them first, to know the right size, or put them in while you are parsing, and later cut off the array. (Or they are always the same number.)

Categories

Resources