This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Associate Certification (OCAJP 7) and the fly likes Substring method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7)
Bookmark "Substring method" Watch "Substring method" New topic

Substring method

Shane Timlin

Joined: Aug 15, 2013
Posts: 1
Hi, I came across the following question in my revision -


What will be printed by the above code if it is run with command line: java TestClass --0.50 (There are two minuses before 0.)

The explanation says
str.substring(1, str.length()-1) => "--0.50".substring(1, (6-1) ) => -0.5
which still seems too brief for me.
Could somebody explain how I tgets to -0.5?
I see this then gets rounded down to 0 and case 0 & case 1 are applied.


Roel De Nijs

Joined: Jul 19, 2004
Posts: 5139

Please QuoteYourSources and use code-tags for better readibility of the code snippets.

Strings are 0-based and with substring the begin-index is included, the end-index is not.

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
I agree. Here's the link:
subject: Substring method
Similar Threads
How to validate Text field in java.It should accept only numric value.
how to find all possible combinations of charcters in string
output is not coming please help
Get middle initial
Enthuware alpha SCJP 5 mock exam