How to take only numbers in a string? [closed] - java

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
If you prompt the user for a price and they input $25.50, how would I take out the numbers and store them as a Double so that I can use them in equations.
I have tried,
System.out.println("What is the price of the product(e.g. 35.21)?");
price = scanner.nextLine();
price_2 = price.replaceAll("[^0-9]", "");
price_3 = Double.parseDouble(price_2);
System.out.println(price_3);
But it does not maintain the decimal.

You can use a regex to strip out all non-numeric characters from the String, while leaving the decimal point.
Then, just parse the string to a double:
String str = "$25.50";
double dbl = Double.parseDouble(str.replaceAll("[^0-9,.]",""));
System.out.println(dbl);
In your code, you were removing the decimal point as well. Simply add the ,. to your regex.

Related

Is there any method of converting math formula in java [closed]

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
I need to convert that formula in such a way that i can make the calculation by taking the input of radix and number from user
〖log〗_radix (number)= (〖log〗_e (number))/(〖log〗_e (radix))
Well, you could simply read the inputs similar to here. Then, just calculate the result.
Scanner input = new Scanner(System.in);
double number = input.nextDouble();
double radix= input.nextDouble();
double result = Math.log(number) / Math.log(radix);

How to generate a random mobile number starts with zero and have 10 digits, that starts with zero? [closed]

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 3 years ago.
Improve this question
My Friend ask me this question and i am posting it here, if you have better approach please share
String getRandomMobileNumber() {
double random = Math.random();
Double randomten = random*1000000000.0;
return "0"+Math.round(randomten);
}
You can use this, see javadoc:
String phoneNumber = "0" + ThreadLocalRandom.current().nextInt(10000000, 99999999);

How do I get rid of trailing zero? [closed]

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);

How to alter Java strings contain only certain alphabetical characters [closed]

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
how can I alter text string fields in java to only contain certain alphabetical characters (f,-)
in this format: 2f5-4, 2f6, 8f9
Only numbers f numbers and
numbers f number - numbers
You could use a regular expression to check, if the String in your text field is valid:
\d+f\d+(?:-\d+)?
Java code sample
Pattern pattern = Pattern.compile("\\d+f\\d+(?:-\\d+)?");
for (String s : new String[] {
"2f6", "2f9", "6f10", "5f9-2", "3f4-9"
}) {
System.out.println("String: \""+s+"\" match: "+pattern.matcher(s).matches());
}
Regex expression =>
(([0-9]+)([f])([0-9]*)|(([-])([0-9]*)))\w+

Substring/toString/IndexOf Function not working past a certain value [closed]

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);

Categories

Resources