Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using methods and counting uppercase characters

 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a code that will count the uppercase characters. Java says that I am missing a return statement. I have no idea why?? Thank you for all your help


 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your method is declared as:


that 'int' part says "this method will return an int primitive".

But you never do. Nowhere in the method do you have a return statement. I'm guessing you are missing a "return upperCaseCount ;" statement somewhere after the close of the outer for-loop - probably between your lines 27 and 28.
 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you

I put the return statement in and my code compiled but I am still having a problem....

Once I entered a string into the compiler to test it, it gave me the following error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 1
at java.lang.String.charAt(String.java:695)
at test.countUppercase(test.java:24)
at test.main(test.java:10)

What does this mean?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the great things about Java is that its error messages almost always give you a good idea of what the problem is.

"StringIndexOutOfBounds" means some line of code has tried to index into a string beyond its length.

I see "ex out of range: 1"; I would suspect an empty string somewhere.

You might also look at java.lang.Character.isUpperCase() to replace your test for upper, unless that is part of your exercise.

rc
 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am not sure how to tell where my string is out of index? Can you help me?

thank you
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the best things you can learn right now is to debug your program. Start putting "System.out.println()" statements in your code, to try and figure out what it is doing.

If you are getting a "StringIndexOutOfBounds" error, then I would suggest looking at what might be looking at specific spots in your string. The best bet in your code is this line:


I would probably stick in just before this line something like

you can even get more specific, and do something like:


 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out with that trick!! Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark donner wrote:I figured it out with that trick!! Thanks

Good Job!!!

Can you post your solution here, so that other may benefit from it?
 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of course

The problem was that in my for loop, I had a "less than or equal", and was starting it at 0, so there was one extra character it was trying to check when it wasn't really there.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark donner wrote:of course

The problem was that in my for loop, I had a "less than or equal", and was starting it at 0, so there was one extra character it was trying to check when it wasn't really there.


That is an EXTREMELY common error, especially for beginners. That was the first thing I looked for when I saw your code, and sure enough, there it was. But just TELLING you that wouldn't have been nearly as educational for you. Now you will be more likely to not make that same mistake again (although everyone does at some point), but more importantly, you have an excellent debugging tool on your belt now.

Way to go!!!
 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brilliant. Thanks for going the extra mile
 
Campbell Ritchie
Sheriff
Pie
Posts: 49450
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought you don’t usually get an out of bounds exception at 1 by iterating an empty String; I would have thought you suffer that Exception at index 0 on an empty String. An empty String doesn’t even have a charAt(0).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic