Results 1 to 4 of 4

Thread: String out of bound error

  1. #1
    Join Date
    Nov 2009
    Posts
    347

    String out of bound error

    Hi
    I am trying a program which takes the input from the user, the program will convert all the multiple blank spaces into a single blank space. It will also create a new line after a sentence is declared. my logic is to find where is "! . or ?" in the string, if it founds one then the sentence is declared. I want to capitalize the first word of the sentence. This is my code please check it out.
    Code:
    public String parsentence(String sent) {
        	String t1;
        	for (int x = 0; x <= sent.length(); x ++) {
        		if ((sent.indexOf(sent.charAt(x)) + 1) != sent.length()){
        			if ((sent.charAt(x) & sent.charAt(x + 1)) == ' ') {
        				t1 = sent.charAt(x + 1) + "";
        				sent = sent.replaceFirst(t1, "");
        			}
        			if (((sent.charAt(x) == '.') || (sent.charAt(x) == '!') || (sent.charAt(x) == '?')) & (sent.charAt(x + 1) == ' ')) {
        				t1 = sent.charAt(x + 1) + "";
        				sent = sent.replaceFirst(t1, "\n");
        				sent = sent.replace(sent.charAt(x + 2), Character.toUpperCase(sent.charAt(x + 2)));
        			}
        		}
        	}
        	return sent;
        }
    Any advice or suggestion on this.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: String out of bound error

    Hi
    You are trying to play with the individual characters of a string, so you must always remember that the charAt position is a zero based index. For this reason the following would not work.
    Code:
    x <= sent.length()
    for example we will consider
    Code:
    test
    0123
    As you can see the above string is of length 4 but the index count is only 3. To sort out this you will need to change the loop, while x must be lower that the length.
    Code:
    for (int x = 0; x < sent.length(); x ++)
    The following can give you StringOutofBoundsException
    Code:
    sent.charAt(x + 1)
    make the required changes and I think your program will run.

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: String out of bound error

    Hi
    Thanks for your advice. Now the problem I am going through is, when is type "exam. send." two spaces after the first period. the output what i need is
    Code:
    Test.
    Sent.
    But it is displaying
    Code:
    Test.
     sent.
    This is my modified code, please go through it and suggest me what I have to do.
    Code:
     public char[] parsentence(char[] sent) {
        	sent[0] = Character.toUpperCase(sent[0]);
        	for (int x = 0; x < sent.length; x ++) {
        		if (x != (sent.length - 1)){
        			if (((sent[x] == '.') || (sent[x] == '!') || (sent[x] == '?')) && (sent[x + 1] == ' ')) {
        				sent[x + 1] = '\n';
    					if (sent[x + 2] == ' ')                // SEE NOTE
    						sent[x + 2] = '\u0000';     // SEE NOTE
    					sent[x + 2] = Character.toUpperCase(sent[x + 2]);
        			}
        			if ((sent[x] == ' ') && (sent[x + 1] == ' ')) {
        				sent[x] = '\u0000';
        			}
        		}
        	}
        	return sent;
        }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: String out of bound error

    Hi
    I think you are making your program more complicated. I can suggest you a simple solution. Just convert to a String and after that you can use the .trim() method. I think this will make your program more efficient.

Similar Threads

  1. Replies: 5
    Last Post: 15-12-2010, 07:18 PM
  2. What is the purpose of Bound Checking in C?
    By Rajeev D in forum Software Development
    Replies: 3
    Last Post: 21-01-2010, 10:53 AM
  3. Prefix not Bound to a namespace Error with XML parser
    By Nihar Khan in forum Software Development
    Replies: 3
    Last Post: 02-03-2009, 02:25 PM
  4. Error: Argument prompt cannot be converted to type string.
    By Kiran123 in forum Software Development
    Replies: 3
    Last Post: 06-02-2009, 08:38 PM
  5. Call of Juarez: Bound in Blood
    By Gamez in forum Video Games
    Replies: 3
    Last Post: 14-01-2009, 06:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,298,600.86775 seconds with 17 queries