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 8 years ago.
Improve this question
I have a code :
variables : Double meter, km ;
it has to convert Km to meters and pass the value to the text fiels in this case "tfm"
meter=km*1000;
Double.parseDouble(tfm.setText(meter));
However it show error which says:
incompatible types : double cannot be converted to string
Does anybody knows how to fix it ?
Thank you in Advance
To set any value to label :
label.setText(YourValue);
so , in your case use this line:
tfm.setText(meter+"");
meter = km * 1000;
is correct. However, in your line of
Double.parseDouble(tfm.setText(meter));
You have several problems:
setText expects String and is void, therefore you cannot use its value, as seen here. You try to use this void as a parameter at parseDouble, which expects String, seen here. You should have something like:
tfm.setText(meter + "");
instead.
Related
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 2 years ago.
Improve this question
Edit 1:
Please don't go by the example in a literal way. The key values can be of any string. Need not include "key" and "col" in them.
For example, the string can be the following
Ravi,India,Married;John,Canada,Single;Robert,Spain,Unknown
So, if I pass Ravi as the key, I should get India and Married as the result.
I am trying to use the Java regex pattern to find the details from a given string. The structure of the string is as follows:
Key1,Key1Col1,Key1Col2;Key2,Key2Col1,Key3Col2;Key3,Key3Col1,Key3Col2
where ; is the delimiter for each record.
My requirement is to accept the Key (which is Key1, Key2 etc.) and return the corresponding values like Key1Col1,Key1Col2 etc.
This would do it:
(?<=^Ravi,|;Ravi,)[^;]+
https://regex101.com/r/Tcmzhd/1
The link above shows PHP but it should work in Java no problem.
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 4 years ago.
Improve this question
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
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 4 years ago.
Improve this question
I'm building a calculator app which take up math expressions, parses it and displays the results, for that I use Javaluator.
Something like:
String expression = "(2^3-1)";
Double result = new DoubleEvaluator().evaluate(expression);
I use two textView for this: one for displaying user input (expression) and the other for displaying the results which are a Double.
Everything works fine but I would like to get rid of the float that is returned after each operation: e.g: 10 * 10 = 100.0. I tried something like finResult = result.intValue(); works but is broken for division operations. e.g: 2 / 3 = 0.
Is there a way to fix that?
For getting rid of trailing zero, you can use DecimalFormat and its method setMinimumFractionDigits(0).
DecimalFormat dc = new DecimalFormat();
dc.setMinimumFractionDigits(0);
String finResult = dc.format(result);
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 function:
ItemValue[i] = substring(toString(obj),0,toString(obj).indexOf(".") + 4);
where obj is a number. The function works for 9,999,999.99999 -> 9999999.999 but values such as 99,999,999.99999 gets converted to 9.999.
Is it a data type issue?
Thanks,
The problem is with the double values you are using. The value 99999999.99999 will be translated to 9.999999999999E7 by toString() so the results you are getting. In order to strip the digits after decimal you can use DecimalFormat class:
DecimalFormat f = new DecimalFormat();
f.setMaximumFractionDigits(3);
f.setMinimumFractionDigits(3);
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 am developing a Java program and I'm meeting cases where I get undecided whether to use the casting a string to integer method, or to use the integer.parseInt method. Is there any clear benefit for either of the two methods?
With 'casting to string method', I mean:
String.valueOf(integer);
As far as I know, it's not possible to cast from a String to an int, so using Integer.parseInt seems like the best option here.
Looking at your edits about using valueOf, perhaps this link may help: Integer.valueOf() vs. Integer.parseInt()