Results 1 to 5 of 5
  1. #1
    maxb is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default [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

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    10

    Default Some observations...

    • Need to change the following if statement:

    Java Code:
    if (chaine.length() <= 1)
    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)
    • This is probably were your problem is:

    Java Code:
    for (int i=0; i< chaine.length() ; i++){//3
    char lettre1 = chaine.charAt (i);
    char lettre2 = chaine.charAt (i+1);
    .
    .
    .
    To fix it, either:
    • 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    maxb is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    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,
    CJSL
    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???

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    10

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    maxb is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    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

  1. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  2. ArrayIndexout of Bound exception
    By Preethi in forum New To Java
    Replies: 2
    Last Post: 02-14-2008, 10:40 PM
  3. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 10:28 PM
  4. Error: cannot be applied to (java.lang.String)
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:33 AM
  5. Error: convert from String to long
    By bbq in forum New To Java
    Replies: 1
    Last Post: 06-29-2007, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •