Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: please help with out of bounds exception

  1. #1
    Junior Member
    Join Date
    Apr 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default please help with out of bounds exception

    so I have a task for an online course (in which the forums are no longer active) where i have to find the ratio of letters (C and G) in a string.
    I've marked where i'm getting an out of bounds error -1 with ***
    although i'm stuck as to why

    pls help

    p.s. the error is all i need help with at the moment, I would prefer to figure the rest out on my own :)

    public String cgRatio (String dna){
    int currIndex = 0;
    float cCount = 0;
    float gCount = 0;
    int startIndex = 0;
    while (true){
    currIndex = dna.indexOf(startIndex);
    if ( dna.substring(currIndex, currIndex +1) == "C"){ ***
    cCount ++;
    }
    if (dna.substring(currIndex, currIndex +1) == "G"){
    gCount ++;
    }
    if (currIndex >= dna.length()){
    break;
    }
    startIndex = currIndex +1;
    }
    return "c to g ratio is " + (cCount/gCount);
    }


    //being called to:
    public void test(){
    cgRatio ("CCATGCCCCTATAGTAGATGTTAAAAGAAATATGAACTTAGATTAAGA AACCCC"); ***
    cgRatio ("");
    cgRatio ("ATGTATAGATATAAATATAGAGSsaegTGATAGoiajhegoijaTGA" );
    }

  2. #2
    Junior Member
    Join Date
    Mar 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help with out of bounds exception

    The exception you are getting is a StringIndexOutOfBoundsException which can occur when no such character is in bounds of a string. The problem you had is that with dna.indexOf(startIndex), you are looking for a character that has a value of 0, not updating the current index.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: please help with out of bounds exception

    Another problem is using == to compare String objects. You need to use the equals method to compare object contents.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Index out of bounds exception. Need help.
    By chernorizec in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 13th, 2014, 05:03 PM
  2. Airline having with array index out of bounds exception
    By dubs4sam in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 22nd, 2014, 02:57 PM
  3. Array Out Of Bounds Exception Question
    By OllieDee in forum Exceptions
    Replies: 2
    Last Post: August 8th, 2013, 05:54 AM
  4. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 03:03 PM
  5. Array exception out of bounds
    By gabberlt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 17th, 2011, 08:05 AM

Tags for this Thread