Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to store values from text view for example i am performing the following plus function like 5+6.Now i want to store 5 as value-1 and 6 as value-2.I only have one text view where i am displying like 5+6 in my project.I search a lot about my issue but could not found any proper solution. thanks
try this:
String string =text.getText().toString();
String[] separated = string.split("+");
String firstValue=separated[0];
String secondValue=separated[1];
You can try this and you will able to get the addition of two numbers at once. .
String[] getText = textview.getText().split("\\+");
int total=0;
for(String s:getText){
total+=Integer.parseInt(s);
}
in the end of iteration total will give the final result.From this method it gives the total of any numbers of numbers.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
For example, if I have a string (24/25), how would I go about being left with two double values of 24 and 25. My objective to to eventually, given a set of number of that form, divide each of them, and add to get the average. I'm fairly new to Java and I honestly am so confused as where to even begin. Thank you!
As Andreas said. First you need to take the substring between brackets. Use String.substr
Now split 24/25 to 24 and 25. You can use String.split
Then you can parse them with Double.parse
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an amount of 12.35 like this:
Double x = 12.35
And I would like to change the amount to 35.12 like this:
println x >> 35.12
I am not able to find a solution in Java, where I could do that.
What you're trying to do isn't a mathematical operation, so you really shouldn't try to treat it as one. Instead, treat it as a string manipulation.
Convert the number to a String, and split it up at the decimal point. Then recombine the string with the before and after parts swapped.
String[] xParts = Double.toString(x).split("[.]");
System.out.println(xParts[1] + "." + xParts[0]);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hii friends I am beginner in java. I am getting string from InputStream in the format like:
aD1bD2cD3dD4eD5fD6g
where D1,D2,D3... are data and small letters a,b,c,d,e,f,g are identifier so I can identify data between a and b and c and so on.
Now the problem is that I am getting more than one data pattern with spaces in between. But I need extract only first data from it. For example, consider data received as
a-674b-26c96d-662e-39f93g a74b-2c96d66e-39f86g a-84b-96c96d562e-99f93g
then I need to extract only first data from this entire data string that is
a-674b-26c96d-662e-39f93g
Please help me.
Use this
String[] params = data.split(" ");
String firstString = params[0];
split() function will split your whole string using space as delimiter, resulting in array of strings. First element of the array is the string you are looking for.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string of numbers which looks like this
["100000100685716-2","603834770-2", "604544970-3"]
can someone help me with a regular expression to match each long (first number before the "-") so i can add it to an array?
The following regex should work: (\d+)-
You do not need a regex here:
Use int pos = str.indexOf('-') to get the location of the dash
Use str.substring(0, pos) to get the initial portion of the string.
If dashes in the input strings are optional, you would need to add a check of pos to be non-negative.
(?:\"\d+)
This should do exactly what you want
String s = "[\"100000100685716-2\",\"603834770-2\", \"604544970-3\"]";
System.out.println(s.split("-")[0].substring(2));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to display the first folder name from the path.
/mnt/sdcard/Videos/lk.jpeg, I want to display mnt string. in java
/mnt/sdcard/Videos/lk.jpeg--> **mnt**
You can split on / and use [1] element from result array.
You can either use regular expressions or you can use String.split(). Note that the split() result should be checked for live usage (e.g. if it has at least two elements).
String desired = "/mnt/sdcard/foo".split("/")[1];
String str = "/mnt/sdcard/Videos/lk.jpeg";
System.out.println(str.split("/")[1]);
Try this out. This is a poor question. But maybe the asker can be a newbie.