jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes using methods and counting uppercase characters 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 » Beginning Java
Bookmark "using methods and counting uppercase characters" Watch "using methods and counting uppercase characters" New topic
Author

using methods and counting uppercase characters

mark donner
Greenhorn

Joined: Oct 08, 2011
Posts: 17
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

Joined: Oct 02, 2003
Posts: 11150
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
mark donner
Greenhorn

Joined: Oct 08, 2011
Posts: 17
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

Joined: May 29, 2005
Posts: 479
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

Joined: Oct 08, 2011
Posts: 17
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

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Oct 08, 2011
Posts: 17
I figured it out with that trick!! Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Oct 08, 2011
Posts: 17
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

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Oct 08, 2011
Posts: 17
brilliant. Thanks for going the extra mile
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using methods and counting uppercase characters
 
Similar Threads
Count the number of vowels, words, and sentences.
Letter counter program. having trouble understanding it.
Help with Character count from a file.
Checking whether a string is a substring of another string without using indexOf method
Scanner