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.
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 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;
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 7 years ago.
Improve this question
how can we figure-out a number is even or odd, with out using division(/) or percentile (%) symbols.
This one of the interview asked to me. Asked me to write program using java.
We should not use arithmetic symbols like division(/) , percentile (%).
Without using division or modulo, the only thing that comes to mind is checking if the last digit is in the set [ 1, 3, 5, 7, 9 ], like so:
public static boolean isEven(int testNumber) {
String strI = Integer.toString(testNumber);
String lastCharacter = strI.substring(strI.length() - 1);
return ("13579".indexOf(lastCharacter) == -1);
}
That would produce:
System.out.println ( isEven( 10) ); // true
System.out.println ( isEven( 11) ); // false
System.out.println ( isEven( 999) ); // false
Good enough?
Shift right and then shift left the number, if it remains the same number is even, otherwise it is odd.
boolean isOdd = (yourInteger&1)==1;
That is from the link provided by Christopher How do I check if an integer is even or odd?
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 7 years ago.
Improve this question
If I had a numberical idno value, how would I find the ones >=5000 and < 5000? Would i use idno.substring(7,11) and then check the conditions
if >=5000 print Male
if <5000 print Female
How would I perform this?
Check idno % 10000 to get the value of the last four digits.
Given that your ID number is stored as an integer:
int idno = ...; // some ID number
You can use this comparison:
if (idno % 10000 >= 5000) {
System.out.println("Male");
} else {
System.out.println("Female");
}
The if statement is a branching operator that allows your code to branch to one block or another based on the result of the comparison. This is different from a loop.
A loop is code that will execute repeatedly until a condition is met. For example, if you had an array of integer ID numbers:
int[] idArray = { .... , .... , .... }; // some array of IDs
You could loop through them like this:
for (int idno: idArray) {
if (idno % 10000 >= 5000) {
System.out.println(idno+" is Male");
} else {
System.out.println(idno+" is Female");
}
}
First convert your String to integer using
int idno1=Integer.parseInt(idno);
Then simple check idno1<=5000 or idno1>5000
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.