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 do calculator that do sums but just in one index like this:
args[0] = "2+2"
and the output should be 4.
I won't do your homework for you, but I'll split it in few steps:
You should get String before the "+" and after the "+":
Have a look at String.indexOf and String.substring for that. Make sure you check for -1 for indexOf.
Parse String to int:
int foo = Integer.parseInt(String s);
Now you can add two ints.
Good luck!
Related
This question already has answers here:
split string at index
(5 answers)
Closed 2 years ago.
I get a number generated as a string that always has the same length, for example:
0107612733631449211907028445
Now I wan't to get two separate strings with the last 10 digits:
010761273363144921**1907028445**
And another string with these digits
0107612733**63144**9211907028445
The positions of these needed numbers are always the same, the numbers at the beginning are not important for me and can be omitted.
How can I get these two strings?
Java docs .substring method
String number = "0107612733631449211907028445";
String last10 = number.substring(number.length()-10);
for middle numbers use substring(int beginIndex, int endIndex)
This question already has answers here:
How to remove the last character from a string?
(37 answers)
Closed 4 years ago.
I have a string oCustomerOrderNumber which I am passing a value into iCustomerOrderNumber. I would like to remove the last 3 characters from this iCustomerOrderNumber but unable to get this to work. Error I get is that it does not like the -.
Code:
oCustomerOrderNumber = iCustomerOrderNumber.trim() -
(iCustomerOrderNumber.length() -3);
Example. ICustomerOrderNumber is 1234567 I want oCustomerOrderNumber to be 1234.
For getting a portion of a string you can use substring method and
here you can define from where until where you want to fetch from any
String:
iCustomerOrderNumber.substring(0, iCustomerOrderNumber.length()-3);
oCustomerOrderNumber = iCustomerOrderNumber.substring(0, (iCustomerOrderNumber.length() -3));
This question already has answers here:
Java String split removed empty values
(5 answers)
Closed 6 years ago.
In Java:
String base = "a|a||";
String[] stri= .split("\\|");
produces a string array with length 2.
On the other hand in python:
base = "a|a||"
base.split("|")
produces an array with length 4.
What do I have to do to get the same result in Java?
Use split with limit set to negative value:
String base = "a|a||";
String[] stri= .split("\\|", -1);
From the docs (the number at the and is n):
If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.
This question already has answers here:
How to convert number to words in java
(31 answers)
Int to English words in Java
(4 answers)
Closed 9 years ago.
I search but cannot find on the web.
Is there a function that can represent any integer into its String litteral representation ?
Example :
Integer i1 = new Integer(4);
Integer i2 = new Integer(30);
i1.callSomeFunction();
i2.callSomeFunction();
Output :
"Four"
"Thirty"
If you know another function which isn't in the Integer class, it's fine too.
There is no standard method to do this but take a look at this. There is an example for a number to words in the English language converter.
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
I have a equation in string format like "45+5*4-6" which I have to solve in Java.
Is there any way to solve equation which is in string format?
Check Beanshell
Something like this should work -
Interpreter ip = new Interpreter();
ip.eval("res = 45+5*4-6");
System.out.print(ip.get("res"));