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 4 of 4

Thread: Help with substring

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with substring

    I'm supposed to convert Hypertext for ex. :

    <li><a href=”(insert url)”>Url Name</a></li>

    into a string:

    Url: (Insert Url)

    but I don't know what's wrong with my code. Can't even get a word. This is what the console says: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

    public void start()
        {
            String hyperLink = null;
            int position1;
            int position2;
     
            do
            {
                System.out.print("Hyperlink: ");
                hyperLink = input.next();
                hyperLink = hyperLink.replace(" ", "");
     
     
                position1 = hyperLink.indexOf("/\">");
                position2 = hyperLink.indexOf("</a");
     
                String name = hyperLink.substring(position1, position2);
     
                System.out.println(name + ":    ");
            } while (continue());

    What am I doing wrong?
    Last edited by KaFr29; December 11th, 2021 at 10:48 AM. Reason: Code formatting

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with substring

    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    The -1 looks like what is returned when the indexOf method fails to find a match. The code needs to test for that value and handle it as an unexpected value instead of using it in a substring method call.

    Which String is not being found?
    Check the String being searched for is part of the String being searched.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with substring

    I guess it has something to do quotation marks and whitespace for the input.

    I get this error message java.lang.StringIndexOutOfBoundsException: begin -1, end -1, length 6

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with substring

    Do some debugging to see what the code is doing.
    Print out the value of hyperlink so you can see what the computer sees when the indexOf method statements are executed.
    Also print out the values of the Strings that are being searched for with the indexOf method.
    For example:
       String searchFor = "something";
       int loc = stringToSearch.indexOf(searchFor);
       System.out.println("seachFor="+searchFor + ", stringToSearch="+stringToSearch);
       System.out.println("loc="+loc);
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Substring
    By javastart in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 17th, 2020, 12:15 PM
  2. substring
    By frozen java in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2012, 10:10 PM
  3. Need some help with substring please
    By iijakeh in forum Java SE APIs
    Replies: 18
    Last Post: April 7th, 2012, 08:08 AM
  4. SUBSTRING
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 1
    Last Post: November 12th, 2010, 03:57 AM
  5. Substring help
    By Roach in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2010, 07:57 AM

Tags for this Thread