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'm looking for a regex that is able to match numbers bigger than -328, and if it is possible to provide another solution to match the same pattern but without the zero. I tried many things but still not sure about how it works, for example, ^\-?[0-9]\d{3,}$
I'm using it with the com.jfoenix.validation.RegexValidator in order to check the pattern in a textfield.
Thanks
Try this.
String pat = "^-(32[0-7]|3[0-1]\\d|[1-2]\\d\\d|\\d{1,2})|\\d+$";
for (int i = -1000; i <= 1000; ++i) {
String s = Integer.toString(i);
boolean result = s.matches(pat);
if (result != (i > -328))
System.out.println(i + " fail!");
}
Related
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 months ago.
Improve this question
I need to count the number of times a letter's present in a String.
For example:
str = "/data/name/data/name"
How do we get the number of / in this string?
val count = str.count { it == '/' }
To be honest, I am not sure whether you need an answer in java or kotlin (your tags include both), so if you need an answer in java:
String input = "/data/name/data/name";
char search = '/';
long count = input.chars().filter(ch -> ch == search).count();
(and if you need a kotlin version, just take a look at #Ivo's answer)
I think you can count with this way,
val str = "/data/name/data/name"
var count = 0
str.forEach {
if(it == '/'){
count++
}
}
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 9 months ago.
Improve this question
creditTIDstatusArray=[93312263-1-09722612223, 99802001-1-09102842369, 99802002-1-09102842369];
creditTIDstatusList.addAll(Arrays.asList(creditTIDstatusArry));
searchValue="99802002-1".
int retval=creditTIDstatusList.indexOf("99802002-1");
System.out.println("The element at index is:" retval);
Output: 2
Please let me know how I can find the index of the given above(searchValue)element.
As you never have posted the reproducible code.
Assumptions
creditTIDstatusArray is a String type array.
Your search query always stays in front of each String value in the array.
Multiple indexes may start with the same search value.
String[] creditTIDstatusArray=new String[]{"93312263-1-09722612223", "99802001-1-09102842369", "99802002-1-09102842369"};
String searchValue="99802002-1";
for (int i = 0; i < creditTIDstatusArray.length; i++) {
if(creditTIDstatusArray[i].startsWith(searchValue)){
System.out.println("Index :" + I); // this will print all the indexes that starts with searchvalue
}
}
I am not sure why you have added the array into a list and then searched index of because it will never work as you are searching only part of a String rather than the whole value or object.
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
The underscore is not visible in integer data types:
int intHex = 0x0041;
System.out.println("intHex: " + intHex);
int intBinary = 0b01000001;
System.out.println("intBinary: " + intBinary);
int intUnderscore = 1_23_456;
System.out.println("intUnderscore: " + intUnderscore);
If you expect that
int intUnderscore = 1_23_456;
System.out.println("intUnderscore: " + intUnderscore);
prints
intUnderscore: 1_23_456
you misunderstod the purpose of the underscore. It is just syntactic sugare meant to make source code easier to read.
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
chr[k]=byte(chp[i]-chq[j]); //problem
The problem comes in type conversion. I have seen various answers to this and when implementing this it could not be solved.
How to remove Syntex error in the above code
Use ((byte)some expression) instead of byte(some expression).
Are you trying to compare the numerical difference (char by char) between two equal length strings? I'm sorry but your explanation of the problem left much to be desired.
If I were trying to solve the issue of comparing the numerical difference between two strings, I would likely come up with something like this:
char[] a = "cd".toCharArray();
char[] b = "aa".toCharArray();
int i = 0;
int sum = 0;
while (a.length == b.length && i < a.length)
{
if (a[i] > b[i])
sum += (a[i]-b[i]);
else
sum += (b[i])-a[i];
i++;
}
System.out.println("Total character difference: " + sum);
This should cover all cases... Assuming I correctly understood what you were asking.
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 5 distinct strings called, say, string1 through string5.
I want to write a simple if statement that runs if any two of the five strings contain the same string. How would I do that?
Thanks in advance!
Comparison is a binary operation, therefore you can always compare only two objects at a time. I would suggest using a cycle and comparing each string to the remaining ones.
public boolean multipleStringEquals(String[] strings) {
for (int i = 0; i < strings.length; i++) {
for (int j = i + 1; j < strings.length; j++) {
if (strings[i].equals(strings[j])) {
return true;
}
}
}
return false;
}