This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Problem with strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with strings" Watch "Problem with strings" New topic
Author

Problem with strings

Sagarika Seetaraman
Greenhorn

Joined: Jan 05, 2006
Posts: 6
Hi,

I am facing a small problem while using the substring method.

I am using a simple statement as
if(url.substring(url.lastIndexOf("/")+1,url.indexOf(".")).equals("Some Value"))
{
// do something
}

I tested this statement by assigning different values for url. It works fine. But when i tested it on Prod, it gave me this exception.


Exception in thread "main" java.lang.StringIndexOutOfBoundsException
at 0x00362c70: java.lang.Throwable.Throwable() (/usr/lib/./libgcj.so.3)
at 0x00355b3f: java.lang.Exception.Exception() (/usr/lib/./libgcj.so.3)
at 0x003593f7: java.lang.RuntimeException.RuntimeException() (/usr/lib/./libgcj.so.3)
at 0x0035665b: java.lang.IndexOutOfBoundsException.IndexOutOfBoundsException() (/usr/lib/./libgcj.so.3)
at 0x0036018b: java.lang.StringIndexOutOfBoundsException.StringIndexOutOfBoundsException() (/usr/lib/./libgcj.so.3)
at 0x0034766f: java.lang.String.substring(int, int) (/usr/lib/./libgcj.so.3)
at 0x0047cff7: ffi_call_SYSV (/usr/lib/./libgcj.so.3)
at 0x0047cfb7: ffi_raw_call (/usr/lib/./libgcj.so.3)
at 0x0032a658: _Jv_InterpMethod.continue1(_Jv_InterpMethodInvocation) (/usr/lib/./libgcj.so.3)
at 0x0032af64: _Jv_InterpMethod.run(ffi_cif, void, ffi_raw, _Jv_InterpMethodInvocation) (/usr/lib/./libgcj.so.3)
at 0x00328474: _Jv_InterpMethod.run_normal(ffi_cif, void, ffi_raw, void) (/usr/lib/./libgcj.so.3)
at 0x0047ce6c: ?? (??:0)
at 0x0033cd68: gnu.gcj.runtime.FirstThread.call_main() (/usr/lib/./libgcj.so.3)
at 0x003a6e99: gnu.gcj.runtime.FirstThread.run() (/usr/lib/./libgcj.so.3)
at 0x00349aec: _Jv_ThreadRun(java.lang.Thread) (/usr/lib/./libgcj.so.3)
at 0x0031682c: _Jv_RunMain(java.lang.Class, byte const, int, byte const, boolean) (/usr/lib/./libgcj.so.3)
at 0x08048930: ?? (??:0)
at 0x006fa79d: __libc_start_main (/lib/tls/libc.so.6)
at 0x080486e1: ?? (??:0)



Has anybody faced a similar problem?? Would appreciate quick response on this matter.

Thanks
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

What happens if you test it with the URL ending with a "/" character? Take a look at the error (StringIndexOutOfBoundsException) and look at your code -- you're adding 1 to the index without checking to see if you exceed the length of the string.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Sagarika Seetaraman
Greenhorn

Joined: Jan 05, 2006
Posts: 6
Thanks for ur reply...

Actually I am testing with the right url... The url is of a standard form.. So the case that u cited may not occur.
Sagarika Seetaraman
Greenhorn

Joined: Jan 05, 2006
Posts: 6
Joel,

I feel that in the case that u mentioned, the stack trace would have been different... and would have pointed out the error location...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18492
    
  40

Originally posted by Sagarika Seetaraman:
Thanks for ur reply...

Actually I am testing with the right url... The url is of a standard form.. So the case that u cited may not occur.


It only takes 10 seconds to add a println() to your program just prior to the substring call. Better to be sure that something "will not occur" than "may not occur".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with strings
 
Similar Threads
IOException while executing runtime.exec
datasource lookup
SQLException: Driver not found for URL
Java.rmi.RemoteException
rmic NullPointerException