This question already has answers here:
Converting double to string
(17 answers)
Closed 1 year ago.
so I got a Problem coding in Java.
I have some doubles declared and I need to calculate with them. But I dont know who I convert the doubles to a result that can be posted in a JLabel as a String. It always says something like: cannot convert double to String. I have tried toString method but I am either doing it wrong or it just doesnt work. Any ideas for a working code?
You can use String#valueOf.
Demo:
public class Main {
public static void main(String[] args) {
double val = 12.34;
String strVal = String.valueOf(val);
System.out.println(strVal);
}
}
Output:
12.34
Related
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 5 years ago.
I want convert a string to an integer but it doesn't work. I think the problem is that the string isn't "clean", see this example:
public class Test{
public static void main(String[] args){
String str = "(2+2)";
int conv = Integer.parseInt(str);
System.out.println(conv);
}
}
Why doesn't it work? I could also define int x = (2+2); without any problems.
What exactly is the problem here and is there an easy way to solve it?
*Purpose: I have just finished a code that will detect if a math expression is correct (brackets, signs, .. arithmetic stuff). As example the string input is ((8+7)*2) and the program will return true.
But now I need to find a way to calculate this and return the solution of it, 30. (If you want I can post my code too but I didn't want make this question seemingly long.)
Integer.parseInt expects a string, however it needs a number in a string format.
The (2+2) is a string, and it cannot compute 2+2. So trying to convert non numeric values won't work. (), and + are non numeric values
UPDATE:
From my research you can use built-in Javascript engine.
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
public class Main {
public static void main(String[] args) throws ScriptException {
String str = "2+2";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
System.out.println(engine.eval(str));
}
}
Why doesn't it work?
The String cannot be parsed to an integer due to some of the invalid characters within here:
"(2+2)"
You simply cannot parse invalid characters such as ( ) and + using Integer.parseInt. However, when + is the only character used and is a leading sign it's valid i.e
String strOne = "+2";
String strTwo = "+2345";
String strThree = "+237645";
etc...
is there an easy way to solve it?
There is no built-in method to do this for you. However, you can have a look at other posts relating to your question:
Evaluating a math expression given in string form
How to parse a mathematical expression given as a string and return a number?
This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 6 years ago.
How can I convert a String to an int in Java?
My String contains only numbers and I want to return the number it represents.
For example, given the string "1234" the result should be the number 1234
String Availability = json.getString("Availability");before parsing value ="4"
int x = Integer.parseInt(Availability);here after parsing it gives me 2 i don't know why
You can use the foll function Integer.valueOf here is an example
public static void main(String args[]){
int num = Integer.valueOf("22");
num+=2;
System.out.println(num); // output 24
}
This question already has answers here:
Parsing a currency String in java
(2 answers)
Closed 6 years ago.
Note: Java
I have a String[] called moneyEntered.
moneyEntered[0] has the value "$5.00"
I wish to create a new double (called amountPendingDeduction) which extracts the value "5.00" from moneyEntered[0] (i.e. removing the dollar sign and making the rest a double).
How do I do this?
If you are working with currency, double is just fine:
String money = moneyEntered[0].substring(1);
double moneyValue = Double.parseDouble(money);
Try:
Double amountPendingDeduction = Double.parseDouble(moneyEntered[0].replace("\\$", ""));
Since String is immutable, nothing will happen with the dollar sign in the moneyEntered[0] string.
This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 8 years ago.
I am converting double number into string but i am not getting as it is.
following is my code and result:
double d = 123456789123.0;
String test = String.valueOf(d);
System.out.println("InString"+test);
Result:
InString :1.23456789123E11
expected result: 123456789123.0
Try this:
NumberFormat nf = new DecimalFormat("000000000000.0");
String test = nf.format(d);
Consider NumberFormat class from standard java library.
Use it's methods to format a double value.
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
I want to evaluate the expression from a string..
public static void main(String[] args) {
String test = "2+3";
System.out.println(Integer.parseInt(test));
}
It returns me a NumberFormatException error..How do I fix this?
That won't work with basic java (AFAIK), maybe with some expression evaluator library.
You have to parse the string. E.g.:
String[] nums = string.split("+");
List<Integer> numbers = new ArrayList<Integer>();
for(String num : nums) {
numbers.add(Integer.parseInt(num));
int result = addNumbers(numbers);
Where addNumbers is a method you wrote to add numbers in a list.
If you have more operations you have to parse the operators as well, building an expression tree then traversing it.