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: substring StringIndexOutOfBoundsException

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default substring StringIndexOutOfBoundsException

    Hi guys,

    I am trying to count the number of words in a sentence. My method of doing this is as follows;
    1.Accept a sentence from the user using System.in
    2.Scan the user input and store as a String.
    3.Scan the String.
    4.Enter while loop that is entered as long as the sentence has another String.
    Within while loop;
    5.Next word in sentence is stored as a String.
    6.Get length of that word.
    7.Create a substring minus the previous word.
    8.Increment an int variable to count the words.

    Hers's my code;

    import java.util.Scanner;//Imports Scanner class
    public class Test//Start of public class
    {
    public static void main(String[] args)//Start of main method
    {
    Scanner input = new Scanner(System.in);//Sets up new Scanner object. Reads from system
    System.out.print("user: Tell me about yourself"); //Creates input from system
    String Sentence = input.nextLine();//Store input as a String
    Scanner sentence = new Scanner(Sentence);//Scan that String
    int wordcount = 0;
    int length;
    int fullLength;
    while(sentence.hasNext())
    {
    String word = sentence.next();//Store next word in sentence as a String
    length = word.length();//Get length of that word
    fullLength = Sentence.length();//Length of full sentence
    Sentence = Sentence.substring(length+1, fullLength);//Create new sentence minus the previous word
    wordcount++;//Increment int variable
    }
    System.out.println("This sentence has "+wordcount+" words");
    System.exit(0);
    }
    }

    My problem is I get an outOfBounds exception when creating the substring. I can't see how this is as the begin and end index are within the length of the String.
    Any help would be much appreciated.
    Kind Regards,
    DD.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: substring StringIndexOutOfBoundsException

    problem solved.
    When i changed length+1 where the substring is created to just length it works. Not too sure why. If anyone could enlighten, it would be helpful

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: substring StringIndexOutOfBoundsException

    If the index goes past the end of the String, the method will throw the exception. If the index stays within the bounds of the characters in the String, there is no exception thrown.

    Remember that indexes start at 0 and go to the length-1

Similar Threads

  1. [SOLVED] Recursive Methods ; StringIndexOutOfBoundsException
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 18th, 2011, 04:33 AM
  2. [SOLVED] StringIndexOutOfBoundsException
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 14th, 2011, 08:54 AM
  3. SUBSTRING
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 1
    Last Post: November 12th, 2010, 02:57 AM
  4. Substring help
    By Roach in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2010, 07:57 AM
  5. First string is a substring of another
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 21st, 2009, 10:35 PM