getting String out of bounds exception [closed] - java

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.

Related

Wanting to understand why this for loop will not work in Java [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 5 years ago.
Improve this question
Was doing my assignment and tried doing a multi variable for loop where both value have the same increment. However, compiler is giving me an error saying im missing ) ; etc. I dont understand and hope someone can clarify for me. Thanks
for(int i = arr.length - 1; i >0; i--){
String temp = "";
for (int j =0 && int m = i; m < arr.length; j++, m ++){
temp = temp + arr[m][j];
}
diagonalArray.add(temp);
}
Tried changing the && to , but i get error: <identifier> expected
as my error.
int j =0 && int m = i
should be
int j = 0, m = i

x cannot be resolved as a variable [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 7 years ago.
Improve this question
What is wrong with this loop?
int index = 0;
for(int x = 0; x < winDate.length;x++);
{
if(userDate == winDate[x])
{
index = x;
break;
}
}
I've used x several times before in comparing values.
You have got a ";" behind the loop!
In fact this loop does nothing instead of counting.
The part below the loop is getting to initialized statically.
int index = 0;
for(int x = 0; x < winDate.length;x++)-->;<---
{
if(userDate == winDate[x])
{
index = x;
break;
}
}

Netbeans telling me that a value is boolean when it is an int [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 8 years ago.
Improve this question
my while loop :
while (j =>0&& (courseArray[j].compareByCourse(value)) >=0 ){
}
is giving me this error in netbeans: int cannot be converted to boolean
method:
public static void insertionSort(Course[] courseArray){
Course value ; // the next value from the unsorted list to be inserted into the sorted list
int i; // i is a pointer to an item in the unsorted list
int j; // j is a pointer to an item in the sorted list; originally the sorted list is just a[0]
int compare;
for (i=1; i<courseArray.length; i++){
value = courseArray[i];
j = i -1;
compare = courseArray[j].compareByCourse(value);
while (j =>0&& (courseArray[j].compareByCourse(value)) >=0 ){
}
}
}
compareByCourse method:
//method to compare Courses by course name
int compareByCourse(Course other){
return this.course.compareTo(other.getCourse());
}
j is an int, the return value is an int, 0 is an int, so where is the boolean?
It seems like you have confused the operator >= with => (which is not a valid operator in Java). Try changing the while condition to:
while (j >= 0 && (courseArray[j].compareByCourse(value)) >= 0 )
Also if you are assigning j to:
compare = courseArray[j].compareByCourse(value);
use it in the while loop (you can also use it directly).

Java loop can't iterate down string length [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 8 years ago.
Improve this question
Here is some basic code:
public static void main(String[] args) {
String string = "Hello!";
System.out.println("First loop.");
for (int i = 0; i < string.length(); i++) {
System.out.println("g");
}
System.out.println("Second loop.");
for (int i = (string.length() - 1); i <= 0; i--) {
System.out.println("g");
}
}
For some reason, the program won't go through the second loop at all. This is somewhat strange. Can you explain this, and how to fix it?
Your second loop should be looping backwards, while the index is still greater than or equal to zero, not less than or equal to zero. With <= 0, i is greater than zero on the first evaluation and the loop never runs.
Try:
for (int i = (string.length() - 1); i >= 0; i--) {
Change the for condition,the i is initial with value greater than 0 (length-1) and there is condition i <= 0 which is true in case length is equal to 1.But the length of string is 6 so change the condition as below :
for (int i = (string.length() - 1); i >= 0; i--) {
System.out.println("g");
}
Your problem is the condition in the second for loop
i <= 0
never happens. I don't understand why you would want to check that.

I am seeing a suprice result from Integer comparing [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 9 years ago.
Improve this question
I am seeing different results in checking Integers against another and i cant figure out why i am reciving these results?
For example:
Integer j = new Integer(2);
Integer i = new Integer(2);
boolean test1 = false;
if(i >= j){
test1 = true;
}else{
test1 = false;
}
boolean test2 = false;
if(i <= j){
test1 = true;
}else{
test2 = false;
}
System.out.println("i >= j: " + test1); //gives True
System.out.println("i <= j: " + test2); //gives False
System.out.println(i >= j); //gives True
System.out.println(i <= j); //gives True
I was curius as to why i am seeing that i is greater then j so i also tested i > j and still seeing a True as result.
I wonder if someone can explain this to me, or tell me where i can read up on this. I have tried to figure it out and have been reading into wrapper classes but not sure that is the culprit. Stack placement maybe?
You had a typo: in the second block, you wrote test1 = true, but it should be test2 = true.

Categories

Resources