Results 1 to 5 of 5
- 11-30-2008, 12:16 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
[SOLVED] String out of bound error
Hi,
Everytime I try to input a string longer than one char I get a string out of bound error... and I'm trying to figure out why.
here my code (basically it a method that checks if a sting has char placed in order)
_________________________
import java.io.*;
import java.util.*;
public class Alpha{
public static boolean charEnOrdreAlpha (String chaine) {
int test = 0;
if (chaine.length() <= 1){//1
return true;
}//1
else {//2
chaine = chaine.toLowerCase();
for (int i=0; i< chaine.length() ; i++){//3
char lettre1 = chaine.charAt (i);
char lettre2 = chaine.charAt (i+1);
if (lettre1 < lettre2) {//4
test = 0;
} else {//5
test = 1;
}//5
}//3
}
if (test == 0) {
return false;
}
else {
return true;
}
}//2
}
___________________
thanks in advance
- 11-30-2008, 03:42 AM #2
Some observations...
- Need to change the following if statement:
It works as is, but anything less than 1 is zero, which means that it doesn't exist... better make it:Java Code:if (chaine.length() <= 1)
Java Code:if (chaine.length() == 1)
- This is probably were your problem is:
To fix it, either:Java Code:for (int i=0; i< chaine.length() ; i++){//3 char lettre1 = chaine.charAt (i); char lettre2 = chaine.charAt (i+1); . . .
- drop the for loop and assign the lettre1/lettre2 variable directly like you are now or...
- keep the for loop and put the chars in a two element array.
Why are you receiving the out of bound error? Because on the the second loop, chaine.charAt (i+1) is trying to get the third char of a two char string... no can do.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-30-2008, 05:27 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
I don't understand what you mean. First I want to compare the first char with the second one. Like compare a to b in "abc". After I need to do the same with b and c. Why (i+1) which at the beginning is supposed to be (0+1) than second position char is returning an error???
- 11-30-2008, 05:54 AM #4
Jacky ol' boy
ok... like Jack The Ripper said "Let's do this piece by piece"...
Let's say chaine = "my"
The for statement is going to do two loops (i=0, i=1):
Loop #1:
i=0
lettre1 = 'm', because charAt (i) = 'm'
lettre2 = 'y', because charAt (i+1) = 'y'
Loop #2:
i=1
lettre1 = 'y', because charAt (i) = 'y'
lettre2 = '???', because charAt (i+1) = '???' <-out of bounds
Looking at this again, another solution might be to change the for comparation from "i<chaine.length()" to "i<chaine.length()-1".
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-01-2008, 02:32 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
I finally managed to solve the problem. Thanks for your help
public class Alpha{
public static boolean charEnOrdreAlpha (String chaine) {
int test = 0;//initialisé à 0 de façon à ce que le test passe par défault
int test2 = 0;//initialisé à 0 de façon à ce que le test passe par défault
if (chaine.length() <= 1){//1 cas ou la chaine serait de 1 ou 0 caractère.
return true;
}//1
else {//2
chaine = chaine.toLowerCase(); //transforme le tout en minuscules
for (int i=0; i< chaine.length()-1 ; i++){//3 boucle qui incrémente le i afin de comparaison
char lettre1 = chaine.charAt (i);
char lettre2 = chaine.charAt (i+1);
if (lettre1 <= lettre2) {//4
test2 = 0;
} else {//5
test = 1;// on met test2 à 1 dès que l'ordre alpha n'est pas respectée
}//5
}//3
}//2
if (test == 0 && test2 == 0) {//si dans aucun cas l'ordre alpha n'est pas respectée
return true;
}
else {
return false;
}
}//fin méthode
}//fin classe
Similar Threads
-
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String)
By baltimore in forum New To JavaReplies: 2Last Post: 09-18-2008, 07:30 AM -
ArrayIndexout of Bound exception
By Preethi in forum New To JavaReplies: 2Last Post: 02-14-2008, 09:40 PM -
Cannot convert from char to String error
By sondratheloser in forum New To JavaReplies: 1Last Post: 12-13-2007, 09:28 PM -
Error: cannot be applied to (java.lang.String)
By carl in forum New To JavaReplies: 1Last Post: 08-05-2007, 06:33 AM -
Error: convert from String to long
By bbq in forum New To JavaReplies: 1Last Post: 06-29-2007, 07:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks