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;
}
Related
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!");
}
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 5 years ago.
Improve this question
I use java and I have the following string:
points="335,234 285,320 185,320 135,234 186,147 285,147 335,233 ";
How it is possible to add 2 to each number?...for example:
points="337,236 287,322 187,322 137,236 188,149 287,149 337,235 ";
You can use String#split to get all the separate numbers in an array, then use a for to iterate through them:
String points = "335,234,285,320,185,320,135,234,186,147,285,147,335,233";
String[] indvPoints = points.split(",");
for(int i = 0; i < indvPoints.length; i++) {
indvPoints[i] = String.valueOf(Integer.parseInt(indvPoints[i]) + 2);
}
points = Arrays.toString(indvPoints).replaceAll("[\\[\\] ]", "");
System.out.println(points);
Although I suggest you just use an int array to begin with, it would be much more efficient and less likely to encounter errors:
int[] points = {335,234,285,320,185,320,135,234,186,147,285,147,335,233};
for(int i = 0; i < points.length; i++) {
points[i] += 2;
}
System.out.println(Arrays.toString(points));
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 5 years ago.
Improve this question
I've got a 6 char long string, and I would like to check, if there is just one from each character. How to?
You could compare the number of distinct characters to the length of the string:
boolean charMoreThanOnce = s.chars().distinct().count() < s.length();
You can do it using a Set. You need unique elements and Set gurantees you containing the unique elements. HashSet is implementation of Set, you can use it to implement this idea.
public boolean ifAllCharsUnique(String input){
char[] arr = input.toCharArray();
int length = arr.length;
Set<Character> checker = new HashSet<>();
for(int i =0 ; i < length; i++){
if(checker.contains(arr[i]){
return false;
}
checker.add(arr[i]);
}
return true;
}
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
I need help with a implementation of a function that gives me all combinations of ArrayList<ArrayList<String>> (not List<List<String>>) List in fix order.
An Example for what I need:
From:
List1:[1,2]
List2:[3,4]
List3:[5,6]
To:
String 1 "1,3,5"
String 2 "1,3,6"
String 3 "1,4,5"
String 4 "1,4,6"
String 5 "2,3,5"
String 6 "2,3,6"
String 7 "2,4,5"
String 8 "2,4,6"
All Strings in ArrayList<String> Object
Please help me, I have no idea how to implement it.
PS: The size of ArrayList<ArrayList<String>> is variable.
PSS: ...and the size from the ArrayList lists are variable.
After little changes from the old rev it works now (ArrayIndexOutOfBoundsException and deleting to much on result):
void generate(ArrayList<ArrayList<String>> lists,
ArrayList<String> result, int index) {
if (index >= lists.size()) {
System.out.println(String.valueOf(result));
return;
}
ArrayList<String> list = lists.get(index);
for (int i = 0; i < list.size(); i++) {
result.add(list.get(i));
generate(lists, result, index + 1);
result.remove(result.size() - 1);
}
}
}
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.