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

Thread: StringIndexOutOfBounds Exception

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default StringIndexOutOfBounds Exception

    Hello people my program is supposed to be a tool designed to find palindromes. For those that do not know what a palindrome is... it is basically a word phrase or sentence, that reads the same backwards and forwards.. An example is the word wow. w o w. If you spell it backwards, it is still w o w. Another example is Madam I'm Adam. However, because of the way the computer thinks about things, I have decided to make spaces, symbols, numbers and apostrophes to not be part of the phrase... and so my program will first ask for an entry, which is parsed as a String. then it will check each index position of that word from 0 to length - 1. if there is an illegal character or space found, then a message is displayed and it will prompt the user to reenter a suitable word. As of this point, it only checks for spaces... my code compiles but when run gives a StringIndexOutOfBounds Exception after the word is entered.

    My code, which can be found below, consists of two classes in two files. One is the application containing the main method, the other is the actual code.
    public class againApp{
    	public static void main(String [] args){
    		again a = new again();
    		again.start();
    	}
    }
     
    public class again{
     
    	static boolean isPalindrome;
    	static boolean reEnter;
     
    	public again(){
    		isPalindrome = false;
    		reEnter = false;
     
    	}
     
    	public static void start(){
    		do{
     
    			System.out.println("Pleanse enter the word or phrase, with no spaces, to be tested ");
    			String word = Keyboard.readInput();
     
    			reEnter = checkWord(word);
    			if (reEnter){
    				System.out.println("Your word or phrase contain spaces or illegal characters. Please check and reenter");
    			}
    		}
    		while (reEnter);
     
    			if(!reEnter){
    				System.out.println("The word is ok");
    			}
     
     
    	}
     
    	public static boolean checkWord(String myString){
    		int i = 0;
    		for(i = 0; i < myString.length()||!reEnter; i++){
    			if (myString.charAt(i)==('\0')){
    				return true;
     
     
     
     
    			}
     
     
    		}
    		return false;
            }
    }


  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: StringIndexOutOfBounds Exception

    gives a StringIndexOutOfBounds Exception after the word is entered.
    Please post the full text of the error message. It has valuable information.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: StringIndexOutOfBounds Exception

    for(i = 0; i < myString.length()||!reEnter; i++) {
        if (myString.charAt(i)==('\0')){
    With an OR statement only one of the conditions needs to be true. Since you initialise reEnter to false, not reEnter is always true. Therefore your loop will continue even when you have reached the end of the String and continue trying to get the next char. Also the if statement is not checking for a space.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: StringIndexOutOfBounds Exception

    but if i put just the two single quotes to show i want to test for an empty character... instead of '0\' which is the empty character literal... i get a char cannot be dereferenced compiler error.

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: StringIndexOutOfBounds Exception

    Space is not an 'empty' character. A literal space character is two quotes separated by... you guessed it, a SPACE.

Similar Threads

  1. [SOLVED] Should I use an exception here?
    By whity in forum Java Theory & Questions
    Replies: 3
    Last Post: May 4th, 2011, 06:52 AM
  2. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  3. can someone please help me im getting exception
    By kristynrod in forum Exceptions
    Replies: 2
    Last Post: March 15th, 2011, 04:40 PM
  4. DAO exception
    By nrao in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 13th, 2010, 12:22 PM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM