How to limit input length in JFX textField? [duplicate] - regex

This question already has answers here:
JavaFX TextField Array max length of text value
(1 answer)
How to restrict TextField so that it can contain only one '.' character? JavaFX
(3 answers)
Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number
(1 answer)
Numeric TextField for Integers in JavaFX 8 with TextFormatter and/or UnaryOperator
(4 answers)
Closed 5 years ago.
I need to limit input length to 4 digits in JFX textField (field can be empty or have value 0-9999). Below solution works only partially - I can input only digits , but as many as i want - limit do not work. Even if I remove {0,4} from regex and
change IF condition to:
if(newText.matches("\d") && newText.length()>=0 && newText.length()<5)
it doesn't work too. Where is the error?
JFXtextField.textProperty().addListener((obs, oldText, newText) ->
{
if(newText.matches("\\d{0,4}"))
{
newText = newText;
}
else
{
newText = oldText;
}
});

Related

What is the internal functioning of the following code? [duplicate]

This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]
(6 answers)
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
I am trying to understand how the following lines of code work in c++.
int main(){
int i;
i = 1 + (2,3,5,3,6);
cout<<i<<endl;
return 0;
}
Output: 7
Basically, the answer is the sum of 1 and the last integer in between the parentheses.
(2,3,5,3,6) turns out to be 6.
Hence 1 + 6 = 7
You can verify with a print statement
printf("\n%d\n", (2,3,5,3,6));
It will print 6 only.

How to convert a character to an integer in Swift 3 [duplicate]

This question already has answers here:
Convert Character to Int in Swift 2.0
(5 answers)
Closed 6 years ago.
I am trying to simply convert a character or string to an integer in Swift 3. I am a long time C++ developer who is just learning Swift and finding it extremely frustrating to do some of the simplest things in Swift 3 (ie, indexing into a string, converting a character to an int).
Can someone please help me convert characters to ints, specifically in Swift 3 Xcode 8?
Thank you!
It's as simple as this.
let myString = "5"
let sum = 3 + Int(myString)! // Produces 8
print(Int(myString)!)
Indexing
let str = "My test String Index"
let index = str.index(str.startIndex, offsetBy: 4)
str[index] // Returns e from test
let endIndex = str.index(str.endIndex, offsetBy:-2)
str[Range(index ..< endIndex)] // returns String "est String Ind"
str.substring(from: index) // returns String "est String Index"
str.substring(to: index) // returns String "My t"
let string = "1"
if let integer = Int(string) {
print(integer)
}

How do I find all arrangements of a string? [duplicate]

This question already has answers here:
Algorithm to list unique permutations of string with duplicate letters
(3 answers)
Closed 6 years ago.
char ary[10] = "AAABB";
There are 6!/(3!*2!) possible ways to arrange them. How do I find them all?
First, there are 5! / (3! * 2!) = 10 possible ways, not 6! / (3! * 2!). But I think this is your typo.
For this specific string "AAABB", you can do something like this:
Take away "BB", and treat "A"s as separators of "B" slots. Let _ (underscore) be the slot we can insert "B" in.
_A_A_A_
First, treat "BB" as two separated char, and insert them in. We can insert them in slot 1 & 2 ("BABAA"), 1 & 3 ("BAABA"), 1 & 4, 2 & 3, 2 & 4, 3 & 4. (6 in total)
Then, treat "BB" as one, and insert it in. We can insert it in slot 1 ("BBAAA"), 2 ("ABBAA"), 3, 4. (4 in total)
All 10 possible ways iterated.

Efficient extraction of several numbers from a character string in R [duplicate]

This question already has answers here:
R: convert list of numbers from character to numeric
(3 answers)
Closed 7 years ago.
Suppose I have the character string
x <- " 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136"
and I want to extract the floats to end up with this vector as output:
c(1.1325, -0.9022, -0.1832, -0.5479, 0.1236, -0.6556, -1.0599, -0.8881, -0.2136)
What I managed to achieve is:
na.omit(as.numeric(strsplit(samp, split = " ")[[1]]))
My question: Is there a more efficient way?
We can use scan
scan(text=x, what=numeric(), quiet=TRUE)
#[1] 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136

Exception when comparing an (int)double and (int)int [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Exception when comparing an (int)double and (int)int
IGNORE THIS POST. ACCIDENTLY REPOSTED
Sorry
if((int)time >= 600){ time_s.insert(4, sec);
should be,
if((int)time >= 600){ time_s.insert(3, sec); // digit 3 instead of 4
From your code, I suppose the string size is 6 characters (0 to 4 and 5th character as nul). Inserting 2 digit at 4th position and 5th position would overwrite nul.
Do cross verify, as I have made a guess seeing your code.