Results 1 to 10 of 10
  1. #1
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default Out of bounds exception

    I am using a for loop to extract digits out of a string. The string in question is cdates, which = "MW 130 230". I get an out bounds exception, but I'm using cdates.length() as the limit for the loop. How can it be going out of bounds? Weirdly, the exception occurs when the for loop enters (or tries to enter) iteration # 7. The string is longer than that. What the deuce is going on?!?!

    Java Code:
    public boolean timeConflictWith(DiaryItem c) {
    		String thisdt = this.getDaysTimes();
    		String cdates = c.getDaysTimes();
    		StringBuffer filter = new StringBuffer(cdates);
    		String Cee = "";
    		String Tee = "";
    		String ctime = "";
    		boolean conflict = false;
    		for(int i = 0; i < cdates.length(); i++){
    			char ch = cdates.charAt(i);
    			if(Character.isDigit(ch)){
    				ctime = ctime + ch;
    				filter.deleteCharAt(i);
    			}
    		}
    		cdates = "" + filter;
    		cdates.replaceAll("AM","");
    		cdates.replaceAll("PM","");
    		cdates.trim();
    		for(int j = 0; j < cdates.length(); j++) {
    			if(thisdt.length() < j){
    				break;
    			}	
    			else {	
    				char C = cdates.charAt(j);
    				Cee = " " + C;
    			}
    			for(int k = 0; k < thisdt.length(); k++){
    			char T = thisdt.charAt(k);
    			Tee = "" + T;
    			int result = Tee.compareTo(Cee);
    			if(result != 0)
    				continue;
    			else{
    				String cstart = cdates.substring(0,4);
    				String cend = cdates.substring(4);
    				int cstart2 = Integer.parseInt(cstart);
    				int cend2 = Integer.parseInt(cend);
    				conflict = cstart2 >= this.starttime && cstart2 < this.endtime ||
    					 cend2 > this.starttime && cend2 <= this.endtime;
    			   return conflict;
    			}
    			}
    		}
    		return conflict;
    	}
    Last edited by MetalR0; 07-29-2011 at 07:13 AM.

  2. #2
    return is offline Member
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    Hi - Instead of creating a StringBuffer called filter, why don't you create another String called filter and set it equal to cdates.

    There is a useful method in the String class called replace, which you can read up on. All you will have to do is replace the character with an empty character.

    Let me know if that works.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The error message will tell you on which line the exception occurs. Please indicate which line this is.

  4. #4
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    The error occurs at:
    filter.deleteCharAt(i);

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You have a problem with your logic. Each time around the loop i will get bigger, yet each time you delete a char from filter it will get smaller. Eventually i will be larger than filter.length().

  6. #6
    return is offline Member
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by MetalR0 View Post
    The error occurs at:
    filter.deleteCharAt(i);
    That's what I thought too. The index of the character from cdates becomes invalid for filter when you start deleting characters. Come to think of it I think what I mentioned earlier will not work either. SOrry

  7. #7
    return is offline Member
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by return View Post
    That's what I thought too. The index of the character from cdates becomes invalid for filter when you start deleting characters. Come to think of it I think what I mentioned earlier will not work either. SOrry
    Okay this is a suggestion, why consider using an array of characters? That's how I'm thinking maybe others have a more efficient idea?

  8. #8
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    Got it. Thanks!

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If your task is remove digits from the String then you have 2 options.

    Instead of starting with the StringBuffer containing all the chars and then start deleting the digits. You can start with an empty StringBuffer and only add a char to it if it is not a digit.

    Or you can use the replaceAll method. Research how regular expressions work. You could start by looking at the Pattern class in the Java API which has plenty of information.

  10. #10
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

Similar Threads

  1. array index out of bounds exception, AGAIN!
    By samanyu in forum New To Java
    Replies: 8
    Last Post: 06-08-2011, 12:27 PM
  2. Out of Bounds Exception
    By johnwy8 in forum Java Applets
    Replies: 1
    Last Post: 12-08-2010, 09:19 PM
  3. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-19-2010, 12:25 AM
  4. Array out of bounds exception 20.
    By dropt in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 11:32 PM
  5. [SOLVED] out of bounds exception help
    By soxfan714 in forum New To Java
    Replies: 21
    Last Post: 11-11-2008, 09:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •