Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By milovan
  • 1 Post By milovan

Thread: StringIndexOutOfBoundsException

  1. #1
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default StringIndexOutOfBoundsException

    Here is the description of the problem:
    Write a program that has the user enter two words. The program outputs the positions where the words
    have the same letter. The user then enters two more words. The program outputs the positions where the
    words have the same letter. This continues until the two words don't match in any positions. You should
    assume that the user enters only CAPITAL letters.

    Here is the source code I've come up with:
    Java Code:
    public static void main(String[] args)
    {
       Scanner kbd = new Scanner(System.in);
       String word0 = "", word1 = "";
       boolean moreWords = true;
    
       do
       {
          System.out.println("Enter first word: ");
          word0 = kbd.next();
          System.out.println("Enter second word: ");
          word1 = kbd.next();
    
          for (int i = 0; i < word0.length(); i++)
          {
             if (word0.charAt(i) == word1.charAt(i))  // Here is where the compiler indicates the error
             {
                System.out.print("They match at these positions: " + i + " ");
                count++;
             }
          }
          System.out.println();
          
          if (count == 0)
             moreWords = false;
       } while (moreWords);
    }
    Here is an example of what the program output should look like:
    Java Code:
    Enter first word:  MOUNTAIN
    Enter second word:  FOG
    They match in these positions:  2
    I was able to compile the program, but during runtime I encountered an error: They match in these positions: 1 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(String.java:695)
    at Words.main(Words.java:21)


    Any help on this would be much appreciated.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    270
    Rep Power
    4

    Default Re: StringIndexOutOfBoundsException

    Imagine a String as an array of chars. You reffer to each char in that array with charAt(int charPosition). In your case you have i as a position of char.
    i will have value that is less then length of MOUNTAIN means 0 - 7.
    Now, your second char array is FOG, it has only positions 0-2.

    When you try word1.charAt(3) you will get described error, because there is not position 3 in FOG.
    eviljeff likes this.

  3. #3
    jitu.ideas2it is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: StringIndexOutOfBoundsException

    First Letter Is MOUNTAIN And Second Letter is Fog


    One Thing see Ur Line No 14 U Count Upto That MOUNTAIN MEANS 8 Character But In Second only 3 Character So When if (word0.charAt(3) == word1.charAt(3)) so Ur Word1.charAt(3) Means It Showing There

    StringIndexOutOfBoundsException


    F-0
    O-1
    G-2
    So In 3 Nothing Is there So It show Error Better There is Lot Of Functionality In String Buffer U May Use That Funcationality

  4. #4
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default Re: StringIndexOutOfBoundsException

    Quote Originally Posted by milovan View Post
    Imagine a String as an array of chars. You reffer to each char in that array with charAt(int charPosition). In your case you have i as a position of char.
    i will have value that is less then length of MOUNTAIN means 0 - 7.
    Now, your second char array is FOG, it has only positions 0-2.

    When you try word1.charAt(3) you will get described error, because there is not position 3 in FOG.
    I see now. So would it be better if I put it into an IF-ELSE condition? Like this:
    Java Code:
    if (word0.length() > word1.length())
    {
       for (int i = 0; i < word1.length(); i++)  // Here I use the word with the shortest length
       {
          if (word0.charAt(i) == word1.charAt(i))
          {
             System.out.print("They match at these positions: " + i + " ");
             count++;
          }
       }
    }
    else if (word0.length() < word1.length())
    {
       for (int i = 0; i < word0.length(); i++)
       {
          if (word0.charAt(i) == word1.charAt(i))
          {
             System.out.print("They match at these positions: " + i + " ");
             count++;
          }
       }
    }
    else 
    {
       for (int i = 0; i < word0.length(); i++)
       {
          if (word0.charAt(i) == word1.charAt(i))
          {
             System.out.print("They match at these positions: " + i + " ");
             count++;
          }
       }
    }
    I am somewhat certain this would work. However, is there perhaps a simpler way to do this?

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    270
    Rep Power
    4

    Default Re: StringIndexOutOfBoundsException

    Yes, you understand the problem, only try to keep your code as simple as possible. For example you don't need so many loops to get wanted result. one if() with else would solve the problem in the way you try to do it.

    I would do it like next:

    Java Code:
    int i;
    if(word0.length() > word1.length(){
        i = word0.length();
    }
    else{
        i = word1.length();
    }
    // only one for loop here
    BR
    Last edited by milovan; 04-28-2012 at 09:24 AM.
    eviljeff likes this.

  6. #6
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default Re: StringIndexOutOfBoundsException

    Thanks milovan!

  7. #7
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    270
    Rep Power
    4

Similar Threads

  1. Array and StringIndexOutOfBoundsException(s)
    By karimoune in forum Advanced Java
    Replies: 3
    Last Post: 02-24-2012, 03:48 AM
  2. StringIndexOutOfBoundsException
    By Golfnutest in forum New To Java
    Replies: 10
    Last Post: 10-10-2011, 06:01 AM
  3. StringIndexOutOfBoundsException- Crossword Solver App.
    By numbskull20 in forum Advanced Java
    Replies: 3
    Last Post: 12-01-2010, 01:33 PM
  4. StringIndexOutOfBoundsException Error
    By Mayur in forum New To Java
    Replies: 4
    Last Post: 02-01-2009, 05:10 PM
  5. StringIndexOutOfBoundsException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 11:25 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
  •