CodeConversion from byte to int [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 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.

Related

How to get the count of specific letters in a String? (Android) [closed]

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++
}
}

check regex for integers bigger than the negative -328 [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 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!");
}

Efficient algorithm for finding subset with doubles and subset is together [closed]

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 4 years ago.
Improve this question
I have two arrays with letters.
I want to know if array A is contained in array B as follows:
The letters in A must appear adjacent to each other in array B but do not have to appear in the same order as they were in array A.
Example that would be accepted
A = abcd
B = hbadcg
A = aabc
B = abcag
Examples that would not be accepted
A = aabcd
B = adcbga
A = abcd
B = abbcdg
What I could do is for every variation of A check if its a sub string in B. but I'm looking for a better way
Consider using a two-pointer approach for the given problem.
Store the count of each character in A in a hash map - HashMapA
Maintain two pointers, start and end as we iterate over the array B.
Maintain yet another hash map to store the count of characters in the range [start, end] appearing in array B - HashMapB
Sharing ideone link for the same: https://ideone.com/vLmaxL
for(char c : A) HashMapA[c]++;
start = 0
for(int end = 0; end < B.length(); end++) {
char c = B[end];
HashMapB[c]++;
while(HashMapB[c] > HashMapA[c] && start <= end) {
HashMapB[ B[start] ]--;
start++;
}
if(end - start + 1 == A.length())
return true;
}
return false;

Comparing multiple strings in 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 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;
}

How do you assign a number in an array a value? [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 9 years ago.
Improve this question
I am trying to convert the tenth digit of an array to a number (10) if it is the value 'x'.
I have tried...
if (array[9] === 'x') {
'x' === 10;
};
Thanks
Try this, assuming that array is a char[]:
if (array[9] == 'x') {
array[9] = 10;
}
By the way, the code you posted is not valid for Java. This is not an operator: ===, we must use = for assignment and the trailing ; after the last } is unnecessary.
I edited your code already.
if (array[9] == 'x') //Line 1
{
array[9] = 10; // Line 2
}
On Line 1 you said ===. Java uses == to check if two primitive values are equal in value.
On Line 2 you used three equal signs again. If you want to reassign a variable, you will need to use ONE equal sign.

Categories