indexOf assertion issue [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
public int indexOf(X s)
{
for (int i = 0; i <= arr.length-1; i++)
{
if (arr[i].equals(s))
{
return i;
}
}
return -1;
}
.
#Test
public void testIndexOf()
{
BetterArray<String> b = new BetterArray<String>();
for (int i = 0; i < 20; i++)
b.add("str" + i);
assertEquals(0, b.indexOf("str0"));
assertEquals(19, b.indexOf("str19"));
assertEquals(-1, b.indexOf("not found"));
}
The code on top does not pass the final assertion in the test, but the first 2 seem to be going fine, to me it looks like if it loops through and does not find the string, it'll return -1, am i missing something?

Related

How can i count how many times the keywords user enter is the used in the essay. its doesn't check on the entire keyword [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
public static int keywordsChecker(String essay,String key) {
int count = 1;
String[] k=key.split(",");
for (int i = 0; i < k.length-1; i++) {
if (essay.contains(k[i])) {
count++;
}
}
return count;
}
To take into account that each keyword searched for may occur more than once, and to count such occurrences, you may use this inside your for loop:
int indexOfOccurrence = essay.indexOf(k[i]);
while (indexOfOccurrence > -1) {
count++;
indexOfOccurrence = essay.indexOf(k[i], indexOfOccurrence + 1);
}
There are a couple of other issues in your code: I believe you need to initialize count to 0 (not 1). And to count also the last keyword in key your for loop should be for (int i = 0; i < k.length; i++) (without subtracting 1 from k.length). If you want, using <= would also work: for (int i = 0; i <= k.length-1; i++), but this is non-standard, so I would not recommend it.

getting String out of bounds exception [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
the code compiles and also works for any string that is passed but it doesn't work for checking the vowels. It throws a String out of bounds error and i have no idea why. the check for consonants is working.
Here is the code:
public String catchword(String word){
int x = 0;
for(x=0; x<word.length()+1; x++){
boolean v = Vowel(word.charAt(x));
boolean c = Consonant(word.charAt(x));
if (x<word.length()-1){
v = Vowel(word.charAt(x+1));
} else{
v = true;
}
if (c == true && v == true){
word = word.substring(0,x+1) + "op" + word.substring(x+1,word.length());
x = x+3;
}
}
System.out.print(word);
return word;
}
In for(x=0; x<word.length()+1; x++)
x<word.length()+1
Should be
x<word.length()-1
The max index of word is word.length()-1
You will also have problem in word.charAt(x+1). word.length() - 1 is the max index, when x will be equal to it you will get again IndexOutOfBoundsException.

An array with the terms that are the differences of consecutive terms (adjacent) in another array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I currently have:
public double[] differences()
{
diffs = new doublae[sequence.length-1];
for (int = 0; i<sequence.length; ++i){
double diff = sequence[i+1] - sequence[i];
diffs[i] = diff;
}
return diffs;
}
However this does not work when i run a test program to check it.
You range should be different - i shouldn't pass sequence.length - 2 in order for i+1 to be a valid index.
double[] diffs = new double[sequence.length-1];
for (int = 0; i < sequence.length - 1; i++) { // changed the condition
double diff = sequence[i+1] - sequence[i];
diffs[i] = diff;
}

Code not Working For Large Permutations [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've just completed a codility test and only achieved a score of 81%. My code failed when a 'large permutation' was tested against it.
I've got no idea why this failed, as the spec says all values are integers, and my for loop uses only int values. I would really appreciate it if somebody could look at my code and tell me why it provides a value of -1 for massive permutations:-
https://codility.com/demo/results/demo4G8CJS-9YN/
class Solution {
public int solution(int X, int[] A) {
// write your code in Java SE 8
int target = X;
int[] path = new int[X];
for(int i = 0; i < A.length-1; i++) {
if(A[i] != path[A[i]-1]) {
path[(A[i]-1)] = A[i];
target--;
}
if(target==0) {
return i;
}
}
return -1;
}
}
It should be for (int i = 0; i < A.length; i++)(not i < A.length - 1). As of now, the last element of the array is just ignored. It actually fails a very simple test: an array of one element and X = 1.

For loop results reverse of expected order [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The problem is I have a for loop in android , and its seems to be running in the reverse order.
Here is the code :
for(i=0;i<strlent;i++)
{
//ch=st.charAt(i);
//disp(String.valueOf(ch));
disp(String.valueOf(i));
}
I have a string and would like to get each characters out of it, but if I feed in "babe" it runs e-b-a-b. I checked the i value and it runs as 3-2-1-0. I seriously don't understand why it behaves this way.
This is my disp function
public void disp(String st) // this function is used to check with message boxes
{
AlertDialog.Builder adb = new Builder(this);
adb.setTitle("Testing");
adb.setMessage(st);
adb.show();
}
String str = "Let Me Reverse";
System.out.println("\nIn order..");
for(int i = 0; i < str.length(); i++){
System.out.print(str.substring(i, i + 1));
}
System.out.println();
for(int i = 0; i < str.length(); i++){
System.out.print(str.charAt(i));
}
System.out.println();
for(char ch : str.toCharArray()){
System.out.print(ch);
}
System.out.println("\nIn reverse order..");
for(int i = str.length() - 1; i >= 0; i--){
System.out.print(str.charAt(i));
}
String name = "Hello";
for(int i=name.length()-1;i>=0;i--){
System.out.println(name.charAt(i));
}

Categories

Resources