File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Hate to drag up old woes but.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hate to drag up old woes but.." Watch "Hate to drag up old woes but.." New topic
Author

Hate to drag up old woes but..

Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Im working through a program which was suggested by course on Java ranch, where a number is passed to main during runtime and the number is returned in printed form (e.g. ) java Say 10 would return "ten";

I managed to write and successfully run all numbers between 0 and 99, with no runtime errors or exceptions. I moved on to redesign the program to accept numbers between 0 and 999. It all compiles fine and even runs, but after execution I get the correct output but then I get the

xception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3


Following the exception, it looks like an issue with my method dealing with numbers over 100 (those which do not leave 0 as a modulo of 100, so 101 throws the exception not 100, I have a seperate block dealing with the Modulos of 100 (100 200 300 etc), anyways I banter..

The issue appears to boil down to my substring method, where for any number over 99 (apart from the n % 100 = 0 ) I turn the input number of args[] in main into a string, I then substring out the 1st and last two characters and parse them as ints. So for example, 111 becomes String "111" and then substrings "1" and "11" and then ints 1 and 11 respectivley. The 1 gets outputted as "One hundred and " and the 11 is returned back to its originally called method to output "11" so I get one hundred and 11 printed to the console. This is all fine and dandy, but then at runtime I get the error saying my substring is out of bounds.

So I checked sudo code)

"111" substring (0,1) >> 1
"111" substring (1,3) >>11

if i had substringed (1,2) I would get "1" (did this in debugging) as this is what javadocs reccomended (substrind(start and end index are inclusive) so start would be 0 >>1 and end would be 2 >>1 but I needed 11 so 1,3 gives me 11. see my code:



I have read that if the end index is a number larger than the string then such an exception is thrown. This seems to be the case as I am calling index 3 on a zero index string 111 (012) but if i use 2, i dont get the right substring I only get 1 instead of 11.

Any help is appreciated!

Steve







[edit]Remove excess whitespace to reduce horizontal scrolling. CR[/edit]
[ August 11, 2008: Message edited by: Campbell Ritchie ]

be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
A suggestion: when testing your substring code, use different chars (eg 123) in the String. If they are all the same(111), you don't know which ones you are getting.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Well, yes I do know what I'm getting as I can see the variable assignments within the Eclipse debugger, stepping through the breakpoints I get:

n 111 (the input integer)
tripD "111" (the input integer parsed as String)
hundred "1" the first substring (0,1) of tripD
method returns ("one hundred and")
toBrtnd "11" the second substring of tripD: (1,3) Note (1,2) renders "1"
qed 11 (the integer value of the toBrtnd substring) which is sent back to the calling method in order to be processed as an output "eleven"


running the same debug with input(n) as 123: (actually no error thrown here)

n 123
tripD "123"
hundred "1"
toBrtrnd "123"
qed 23

so all works ok, 111 works and throws the exception after the output
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
I might as well, chuck up the code for scrutiny, its not pretty, but its part of the learning process for me!



[edit]Remove excess whitespace to reduce horizontal scrolling. CR[/edit]
[ August 11, 2008: Message edited by: Campbell Ritchie ]
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
I think I may have found the issue. I only seem to get the error with any number over 100 which creates the second substring as either 0n or n0. In the former issue the number being returned to the caller as a new parameter is the int 01, I suppose this isn't too smart as an int. In this case, perhaps I should run a method to check if the leading number is 0, if it is then get rid of it and pass the number as 1.

However, it still doesn't solve the issue of n11 (e.g. 111) as the second substring = 11 and related int is 11 so it should work fine. If I pass 11 as the argument to main from the command prompt it works no bother, the issue is if it is passed back to the caller, as a newly converted int from a substrin

..the mystery continues
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
If you are doing the Cattle Drive, are you supposed to be posting up here? Have you tried the Cattle Drive forum?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

your code has no main, so it's hard to say exactly what it's doing.

I would suggest you add the second line:


and see if that give you a clue... but that's a guess since I figure out exactly how you are running your code.
[ August 10, 2008: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Originally posted by pete stein:
If you are doing the Cattle Drive, are you supposed to be posting up here? Have you tried the Cattle Drive forum?


Yes and No, Im attempting a few tasks of the Cattle Drive, but not registered or anything like that, I'm not submitting any of my work, just taking parts I want.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Originally posted by fred rosenberger:
your code has no main, so it's hard to say exactly what it's doing.

I would suggest you add the second line:


and see if that give you a clue... but that's a guess since I figure out exactly how you are running your code.

[ August 10, 2008: Message edited by: fred rosenberger ]


Sorry it does have a main I just forgot to paste that bit so here is the whole thing again with main and adding the ouput to check n you suggested. (Also an attmepted hack to cut off the O if a 3 digit number is substringed to two numbers where the second number is 0 eg 101 into 1 and 01 as 01 is not a good integer to submit.




[edit]Remove excess whitespace to reduce horizontal scrolling. CR[/edit]
[ August 11, 2008: Message edited by: Campbell Ritchie ]
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Woops forgot the n check you requested, but feel free to add it if you want to, ill do it in eclipse
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Another approach we might want to take is this... we know that for any String, the code


will throw a StringIndexOutOfBoundsException if x > aString.length(). So the maximum value of x can only be aString.length() - 1.

The code you show has this:


which is getting called on three digit "numbers", or Strings with a length of 3. So this code will throw a StringIndexOutOfBoundsException any time it is called when "tripD" is a three digit number.

So instead of asking yourself "Why isn't this working for '111'?", try asking yourself "Why is this working for 123?" "Is this code snippet run each time?" "If not, what causes it to be run or not run?"

Here's a little test code you can use to test your code. It shows for what values the code is working, and for which it is not. For values that are not, it shows you the exception, and where that exception is originating in your code. Run it. Notice any patterns? You can use that to help track down the issues.



Hope that helps.
[ August 10, 2008: Message edited by: Mark Vedder ]
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 375
Moderators,
This post forces me to scroll horizontally. Pretty tedious to do that.
Just thought I'd let you know...
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Mark, thanks for the advice, I' will give it a whizz and see whats what. Aas for the Index out of bounds exception on three digits, its strange because if I set the substring indexes to(1,2) respectivley from the String 111, that particular instance returns only a string with the value "1" when I actually need "11" (which is what I recieve when I pass .substring(1,3) on 111!. Its indeed a niggly issue, ans its even more compounded when it appears that the only time I get the exception is when I pass values of three digits which contain the second two digits (in any order) of 0 or 1!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Please set up your text editor to convert tabs to 4 spaces each automatically.

Check carefully what String#substring(int, int) does. If you call substring(i, j) you get a String j - i characters long.
Shikhar Madhok
Ranch Hand

Joined: Dec 18, 2006
Posts: 95
Also you might want to have a look at String.html#substring(int)

HTH
Shikhar


He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever - Chinese proverb
 
Consider Paul's rocket mass heater.
 
subject: Hate to drag up old woes but..