Java parsing a string expression [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 7 years ago.
Improve this question
I have expression like below, and I need to parse them.
Examples:
Total volumes of A + Total volumes of B
Total sales of A / Total sales of B
Total Shipment of A - Total Shipment of B
When I get the string , I don't know which operation is present in the string. So using regex I want to parse each expression in java, and I should get this result.
expression1 = Total volumes of A
expression2 = Total volumes of B
operator = +
Can anyone help me with this. I want an efficient way to do this in java, there are 1000's of such expression

As #Pshemo stated, groups are the solution.
I did make the assumption that operator characters aren't part of the operands:
Pattern pattern = Pattern.compile("([\\w\\s]+)\\s+([+\\-/*])\\s+([\\w\\s]+)");
Matcher matcher = pattern.matcher("Total volumes of A + Total volumes of B");
if (matcher.matches()) {
System.out.println("operand 1: " + matcher.group(1));
System.out.println("operator: " + matcher.group(2));
System.out.println("operand 2: " + matcher.group(3));
}
Output:
operand 1: Total volumes of A
operator: +
operand 2: Total volumes of B

Related

Arbitrary amount of decimals while dividing [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 new to programming. I have a task to make a division of two arbitrary numbers, and to set arbitrary number of decimals. I was searching on the internet, but not really sure how to set it. If I could get some help, would much appreciate!
Here's the code so far:
int a,b, decimala;
System.out.println("first number: ");
a = unos.nextInt();
System.out.println("second number: ");
b = unos.nextInt();
System.out.println("amount of decimals: ");
decimala = unos.nextInt();
double c;
System.out.println(a);
System.out.println(b);
System.out.println("--------------");
c = (double)a/b;
System.out.println(%.decimala+ c);
If you just want to output them you could try using format
String format = "%" + decimala + "f";
System.out.format(format,a);
Here's a cheat sheet with all the stuff you can do.
https://alvinalexander.com/programming/printf-format-cheat-sheet
Thanks to #AndrewGuerra for pointing out how to format a variable amount of decimals

Advice for Math Proof Language [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Trying to figure out if I wanted to run a number 17 digits long through 50 million divisions how long would it take on a decent i7 PC and what would you recommend for language? Also I would like to scale it up over time so need a language that can be flexible when I get to say 30ish digit long numbers. For now basically I begin with a 17 digit long number and as I go I only care about the a smaller number after each calculation so it will get smaller quick. I am only doing division and subtraction and not keeping any remainders. Thoughts?
I've knocked up a quick program to test how long it takes, here I believe I have used a 20 digit long number. I know it's not exactly the parameters that you have asked for, however it gives a good illustration to what kind of speed you can be expecting.
This was run on a i5-6200U # 2.30GHz
If you are interested on the code that I used here it is. It will not be able to divide 30 digits but a little tweaking will allow it to. This is written in C#.
using System;
namespace _50_Million
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Number ");
string number = Console.ReadLine();
Console.Write("What to divide by ");
string divide = Console.ReadLine();
Console.Write("How many times ");
string d = Console.ReadLine();
decimal previousNumber = Convert.ToDecimal(number);
decimal times = Convert.ToDecimal(d);
decimal divideDecimal = Convert.ToDecimal(divide);
var watch = System.Diagnostics.Stopwatch.StartNew();
decimal newNumber = 0;
for (int i = 0; i < times; i++)
{
newNumber = previousNumber / divideDecimal;
previousNumber = newNumber;
}
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("It has taken " + elapsedMs + " millisecounds to divide " + number + " by " + divide + ", " + d + " times.");
Console.WriteLine("The Answer is " + newNumber);
Console.ReadLine();
}
}
}

Put point in string value (android 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 6 years ago.
Improve this question
I have BMI calculator app that takes some input and puts it in "EditText". Here is what I am trying to do:
If the input is 170, it will become 1.70.
If the input is 1.70, it will not change.
This is the code I have:
String weight = editText.getText().toString();
Cant you convert the string to an integer and take modulus 100 to the cms and divide by 100 to get in meters?
You can convert the String weight to int like this
int wgt = Integer.parseInt(weight);
Then separating meters and centimetres.
int mtrs = wgt / 100;
int cms = wgt % 100;
Then combining both
String result = mtrs + "." + cms;
try something like this
float value = Float.parse("170");
editText.setText(String.Format(Locale.ENGLISH,"%,02f", value/100f))
I found this way works: I take the number that the user put, and then "170/100" + "0" gives me 1.70

Checking number positions in bankaccount [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
So I have the following problem of a task that I need to make. It's about checking if a bankaccount number is correct or not this is the task:
The first group consists of 3 digits and determines which bank it is .
The second group consists of 7 digits and establishes the customer goes to that bank.
The third group consists of two numbers and a check digit which the validity of the bank account is determined .
The verification is done as follows:
The first and second group together form a number consisting of 10 digits. If you divide this number by 97 then the remainder after division by 97 must be equal to the third group of the bankaccount.
Maybe this is what you are looking for:
private boolean checkNumber(String number) {
//number consists of 12 digits
String firstGroup = number.substring(0, 3);
String secondGroup = number.substring(3, 10);
String thirdGroup = number.substring(10, 12);
int firstSecond = Integer.parseInt(firstGroup + secondGroup);
int third = Integer.parseInt(thirdGroup);
int remainderAfterDevision = firstSecond % 97;
return (remainderAfterDevision == third);
}

Required boolean, found int? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm still learning Java....
My task is to write a program which divides two doubles, but before it displays result of dividing, it has to say if they are dividible or not (without a rest). I HAVE TO use ternary operator.
code:
public class exercise {
public static void main(String[] args){
double x = 8.4;
double y = 4.2;
double z = (int)(x % y);
String result = (z>0) ? "not dividible" : "dividible";
System.out.println("This operation is: " + result);
System.out.println("The result of dividing is: " + (x/y));
}
}
Compiler says for the line "String result = (z>0) ?....." that it requires boolean, and that it found int. Of course, compilation failed.
You've missed the last closing bracket ()) of this statement.
System.out.println("The result of dividing is: " + (x/y);
It should be:
System.out.println("The result of dividing is: " + (x/y));
↑
All the other stuff compiles for me.
My compiler says that you dont have last parenthesis ) in this line:
System.out.println("The result of dividing is: " + (x/y);
Try this:
System.out.println("The result of dividing is: " + (x/y));
It compiles for me.
The problem comes from the syntax error, due you have forgotten the last closing bracket () of the last system.out line.
System.out.println("The result of dividing is: " + (x/y);
Should be:
System.out.println("The result of dividing is: " + (x/y));
If you solve this, then there is no problem.

Categories

Resources