Java regular expressions specify range of digits [duplicate] - java

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 7 years ago.
I'm trying to verify that a user inputs coordinates in the format: x,y
so I'm using regular expressions. x and y can be between 1 and 15, so I used
[1-15]\\d{1,2},[1-15]\\d{1,2}
but that didn't work because 15 isn't a digit obviously. I changed it to
\\d{1,2},\\d{1,2}
so at least I can confirm that its two one or two digit numbers, but it could be up to 99 on either side; not good. I've tried a few other ways like
\\d{1}|[1]\\d[0-5]\\d...
but nothing works and honestly I've been looking at all this so long it doesn't make sense anymore.
More importantly, is it even good practice to use java's regular expression feature? I could think of other ways to do this, but this is just for a personal project I'm working on and trying out different approaches to things I usually do messily.

I think you understand the [...] the wrong way: it means you specify a range of characters. So [1-15] means: 1to 1and 5. It is thus equivalent to 1|5.
You can however specify the digits 1 to 15 with [1-9]|1[0-5]. Plugging this into your regex results in:
([1-9]|1[0-5])\\d{1,2},([1-9]|1[0-5])\\d{1,2}

Related

How to format print function in Java to print equal width per different number? [duplicate]

This question already has answers here:
Java output formatting for Strings
(6 answers)
Closed 11 months ago.
Given these variables:
int a = 1, b = 123, c = 55, d= 1231;
Is there a way in Java to print them with a set width of 5, say. In case number is less than five digits - only print dashes.
1----,123--,55---,1231-
I am aware that these can be achieved with some loops and if statements, looking for something similar to setw() from C++
System.out.println(String.format("%-5.5s", s).replace(" ", "-"));
In short, you can't do it directly. Java has functionality broadly similar to that of C's "printf" formatting.
You can set a field width, you can justify left or right, but your fill characters are limited to zero and space.
Documentation
If the format uses the general "%s" directive, and the corresponding argument is of a class under your control, then you can implement a 'formatTo' method to do the conversion. So a wrapper class might be useful to you.

While debugging Java codes what does # mean in statements like {Instance#789} or "SomeThread"#321: RUNNING? [duplicate]

This question already has an answer here:
Deciphering variable information while debugging Java
(1 answer)
Closed 6 months ago.
The "#" seems to be everywhere when I debug. They are always preceded by some instance/variable name and followed by a (usually three digits) number. What does it mean? I have an image below
Taken from https://medium.com/#andrey_cheptsov/intellij-idea-pro-tips-6da48acafdb7 .
#730 means the 730th object created since the application started.
It is not the hashcode. Length of this can be more or less than 3 digits.
It's totally depends upon which IDE you are using, may eclipse will give something else instead of #730 and in different format also, so it is the way of intellij to maintaining the debugging.
This is Intellij debugger's way of displaying a "unique identifier" for an object. It consists of the short classname and a unique number. The unique number seems to be generated using a simple counter, so the "meaning" of 729 in Owner#729 is (presumably) "this is the 729th object that the debugger has allocated an identifier for". However, you probably shouldn't rely on that.
There is no overt relationship between these numbers and Java identity hashcode values, though I expect Intellij maintains a mapping behind the scenes.
The Owner#5f9d02cb in the screenshot is reminiscent of the result of Object::toString ... when it hasn't been overridden. If that it is what it is, then the 5f9d02cb will be the object's identity hashcode.

Create substring breaking on the whole words [duplicate]

This question already has answers here:
split a string in java into equal length substrings while maintaining word boundaries
(2 answers)
Closed 6 years ago.
In Java or Groovy is there a library or a simple implementation
that for a text it would create substring at some length but not breaking a word in the middle?
An example method with an input: substring("My very long text", 9 /*substring length*/, true /*break on whole words only*/)
An output because without keeping the words it would result in My very l. Since I want to break on the whole words only it will be My very.
In case there are no spaces it would cut the string at the index:
substring("MyVeryLongText", 9 /*substring length*/, true /*break on whole words only*/) --> MyVeryLon
I believe I can say that there isn’t built into Java. We wrote our own method for a similar task. There could easily be some free library out there, but I don’t think I’d introduce a new dependency for this relatively simple problem. You will want to decide what you want to happen if there is no space at which to break (substring("Beginning with a long word", 4, true)). Maybe the library you find doesn’t do what you want in this case. If writing your own, you need to take the cases into account where the original string is too short (substring("Cat", 4, true)) and where the space comes right after the 4th char (substring("Long text", 4, true)).

How do i convert a string to double in java when the string contains math functions? [duplicate]

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 string with a math function, like "35+20". i want a new double variable that takes in the result of the math function i.e, 55.0 How do i achieve this? this is actually for android, i'm making a calculator..
Manually parse the string and do a calculation at each operator symbol. It will get more complicated when dealing with brackets, however.
If you want to write it yourself, you'll probably want to implement the Shunting Yard Algorithm.
There are also some libraries that handle it for you.
https://github.com/uklimaschewski/EvalEx
Since you have mentioned you are working on a calculator I am assuming that you might not only be interested in just the + operation but on a bunch of other stuffs too.
You can look in to open source GitHub project linked below which provides the JAVA implementation for the stuff you are trying to do https://github.com/uklimaschewski/EvalEx
which can give you a good set of functionality that you desire.
This project takes in a string as an expression and the returns the result in BigDecimal format.
You can always extend it and tweek it to custom suite you needs.

How to separate long calculation string into usable calculation in Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Evaluating a math expression given in string form
Sorry about the long (and also slightly strange :)) title, I couldn't think of a better title for it, but here goes.
I have been making a calculator in Java using a JFrame which has JButtons like a real calculator would. As you click the buttons, the calculation appears in a TextArea. When the 'equals' button is pressed, the whole calculation is taken from the TextArea and calculated. The problem I'm having is how to actually calculate the answer. This may sound a little weird, but say the calculation I'm getting is 36+45/22. How would I write the numbers into variables then tell the computer which operations to perform on the variables, and in what order. Can this be done with an infinite number of variables? Is there any way to do this? Thanks for your help.
You could use ScriptEngine:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
System.out.println("result = " + engine.eval("36+45/22"));
Another option is Jep.
Try this, I use a simillar implementation. Works just great. :-D
Evaluate expression in java
If you want to evaluate the expression yourself and not depend on external APIs such as ScriptEngine, you must parse the expression first. This parsing gives you an in-memory representation of the expression which you can then evaluate.
A common way to handle arithmetic expressions is a recursive descent parser. This kind of parser has the nice feature that it evaluates the parsed expression in-place in the parsing methods. An example for a recursive descent parser that does not depend on computer science's formal language theory can be found at http://www.savarese.org/articles/1998-2006/2001-05-Recursive_Descent_Parsing/

Categories