I'm very new to Android development and Java. This is probably a simple answer but I cant seem to find the info anywhere. I know I can pull data from my XML by using:
Double.parseDouble(example.getText().toString());
but what I need to to is then extract the data and add it together.
Example: I pull the data and its 1234 then I need to add 1+2+3+4. Thanks for the help.
I'm not sure I understand the question, but if you want to add all the digits in a string then you would do this:
(pseudocode)
length = number of characters in string
runningtotal = 0;
loop through all charcaters in string, 0 to length-1
extract character at each loop position
runningtotal += Integer.getString(character);
But I suspect this is not actually what you're trying to do. More info?
Related
My programm needs to read a file that has different data structures with a variable separator.
In my properties-file you can set the separator and put coordinates for values of different variables:
separator = ;
variable1 = 1,7
variable2 = 2,42
I would like to have a way where I can access a column and a line with some kind of coordinates.
I'm thinking of a syntax like this:
file.get(1,7,";")
(Which would give you the value of the 1st line and 7th column with the specific separator)
Does someone know a library or a code snippet that does exactly this?
Using String.split() :
public String get(File file, int lineNumber, int column, String separator ) {
//getting to the lineNumber of the file ommitted
// suppose you got it in a String named "line"
return line.split(separator)[column - 1];
}
You can use OpenCSV or SuperCSV for example. I'm not aware of any library that does your 'coordinates' gettings, but it's as simple as reading the CSV with the given separator as List-of-Lists and then call
csv.get(1).get(7)
Seems to be a simple file processing, You should first process the file -
create ArrayList<ArrayList<String>> processedFile
Read every line, split using "line".split(separator)
Store the array above in the ArrayList processedFile at current index
increase the index with every line
Once processedFile is ready, you can simply use processedFile.get(row).get(column). Also once the file is processed, all the other queries will be O(1). Hints are enough, try writing the code yourself, you will learn more.
PS: Take care of NullPointerExceptions wherever required.
Below is the code.\ I imagine you use a for loop and then another but I cannot seem to make it work. I attempted research however most topics were too complex since I am a novice. I'm trying to find a way to get the fifth character out of each string within the variable. I'll use the information given to me so i can then solve the rest of my program. I have more to do
public static void main (String[]args)
{
String[] decoder = {"Nexa2f5", "Z52Bizlm" , "Diskapr" , "emkem9sD", "LaWYrUs", "dAStn78L", "mPTuriye", "aaeeiuUu", "IL8Ctmpn"};
int character = 4;
for(int i=0; i<=decoder.length-1; i++)
{
}
}
I am trying to get the third and fourth characters of the odd numbered Strings. I am trying to put the letters into an array and decode the message. I am also trying to print the 5th character of all other words. I'm having issues commenting right and I've tried to reply a couple times but no dice.
Within your for loop use the array indexing notation. For example, String current = decoder[0] results in current having the value Nexa2f5. Once you have the String Object (in my example, I named it current) you can use the charAt() method shown in the String class documentation to get the 4th and 5th characters. If you need more help than that, read my comments then update your code and ask another question.
I know that we can do this by following ways
StringBuilder
Use substring
But i am looking a way where i have a compressed String say a5b4c2 etc which means a is 5 times b is 4 times etc so String is actually aaaaabbbbcc something like that.
So char at index 2 should return a and char at index 6 should return b.
What can be the best approach for this?
My question is more about what is the best approach to decompress String ?
My question is more about handling this compressed string rather than finding the character at specific index.
Decompress the string until you get the index you want to know. Or you could decompress the whole string and cache it.
What can be the best approach for this?
Without any more specific requirements, I believe the best approach is the simplest approach you can think of.
I would, parse each pair of the letter and the number in turn, reduce the index by that number and if the remaining index is < 0 you have the letter you want.
Check what index you are searching for, and start adding up the numbers of characters. Every time you add, check if the index falls within the previous interval and the current one. If it does, you've found what your character is, otherwise add again.
For example, the workflow given your string a5b4c2, if you want the character at index 7, could be like this:
current position: 0
index we are looking for: 7
add first character's count: 0+5 = 5
does 7 fall within 0 and 5? no, add again
current position: 5
add second character's count: 5+4 = 9
does 7 fall within 5 and 9? yes, so our character must be 'b'.
I'm not sure if this is more efficient or faster than decompressing the string and just using charAt() or something, it's just a different way of approaching it.
EDIT: Since the question is more about how to decompress the string, you could use a StringBuilder and use a for loop to append the correct number of the character to your string... sounds like the simplest way to me.
I want to read this file:-
http://www.somehost.com/products/, A0,D1,L0,T0
http://www.somehost.com/news/rel, A1,D0,L1,T0
http://istor.somehost.com, A0, D1, L0, T0
I have a list of urls and I want to compare those url's with the url's that are there in this file. And Suppose the url that I wanted to compare starts with the url that is there in these file.. Then it will move forward in that url line and it will check for A and D. If A is 0 then we will not crawl that url and vice versa and If A is 1 then we will move forward and see whether L is 0 or 1 means if L is 1 then we will extract link only and vice versa and same with T is 0 or 1, we will extract text only if T is 0.
Any suggestion how can I do this.. ??
I've used Java CSV and it's pretty easy. See the code examples as well. However (summarizing what #Hovercraft Full Of Eels said), if your data are not overly complicated, Java's String.split() should work fine.
After parsing your data you can, you know, read the values and determine what to do from there. Your description of what you need to do is practically an outline of a method with an if ... else if ... else structure, so start from that.
my question is related to my previous question (How to display data from txt file in specific format).
I was wondering if it is possible at the first place, to store data on txt file in specific format rather than store it first and then retreived it again and display it in specific format?
e.g. instead of store data on txt file like this
Jessica
Walking
20 minutes
Matthew
Run
10 minutes
I wanted to store it in txt file in this format
Jessica Walking 20 minutes
Matthew Run 10 minutes
Regarding your comment to adeel's answer:
Thanks adeel825, however I dont know where to put the "\t".. so far I use this method: new PrintStream(fout).println (name); new PrintStream(fout).println (exercise); new PrintStream(fout).println("10 minutes");
First, don't call "new PrintStream(fout)" everytime you print something. Do this:
PrintStream ps = new PrintStream(fout);
ps.print(name);
ps.print('\t');
ps.print(exercise);
ps.print('\t');
ps.print(time);
ps.println();
Or simply:
PrintStream ps = new PrintStream(fout);
ps.println(name + '\t' + exercise + '\t' + time);
Edit
In response to your comment:
one more question...some of the name are too long and it requires more tab..I have put ps.print('\t','\t'); but it seems not working..
If this is a problem, it sounds like you are trying to store them in the manner you want to display them. I had assumed you were trying to store them in a way that would be easy to parse programmatically. If you want to store them displayed in columns, I'd suggest padding with spaces rather than tabs.
If you know that all the columns are going to be less than, say, 30 characters wide, you could do something like this with printf:
ps.printf("%30s%30s%30s%n", name, exercise, time);
That syntax can look quite byzantine if you're not used to it.. basiclly each "%30s" means pad the string argument so that it is at least 30 characters wide. Your result won't look right if any of the values are 30 or more characters wide. If you can't know ahead of time, you'll have to loop through the values in each column to determine how wide the columns need to be.
There is no problem with storing data this way. All you need to do is write out the values and delimit them with a tab character "\t"
You need to "handcode" your formatting. Best way to do this would be to wrap your file-accessing code somewhere, and create something like:
OpenFile()
CreateEntry(name, type, time)
If you want to write records of fixed length you can use the String.[format][1] method
ps.println(String.format("%20s%20s%20s",name,exercise,time))
This will create a table with 20 characters in each field. You can readup on the syntax here
[1]: http://java.sun.com/javase/6/docs/api/java/lang/String.html#format(java.lang.String, java.lang.Object...)