By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,573 Members | 3,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,573 IT Pros & Developers. It's quick & easy.

substring problems for java noobie

P: n/a
Seems like whatever value I use for the first int field (starting position)
the substring procedure negates it and triggers a String index out of range
error.

I've tried all sorts of work arounds to no avail, am I defining the two
string variable incorrectly?

code snippet:

String lastChar = new String();
String imageDir = new String();
<...>
System.out.println(imageDir); //debug = C:\images\
//ok so far but:
lastChar = imageDir.substring(2,1);
//triggers: String index out of range: -1: -- this value is always the
negated value +1 of the of the starting position value.

I'm starting to lose hair over this so any help will also help keep my
barber in business.

Thanks,
Don Freeman
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
The 2 arguments to substring are:

- first: the starting position
- second: up to but not including the specified end position
The 1 argument to substring is:

- the specified start position to the end of the string
--
Regards,
Casey
Jul 17 '05 #2

P: n/a
Casey Hawthorne wrote:
The 2 arguments to substring are:

- first: the starting position
- second: up to but not including the specified end position
The 1 argument to substring is:

- the specified start position to the end of the string

I understand that and am programming for it. But I keep getting
an error during the program run. For debugging purposes I am printing
out the value of the starting postition for the substring just before
the substring function call:

System.out.println(imageDir); // = C:\images\ OK!
System.out.println(strLength); // = 10 OK!

//then the function call:

lastChar = imageDir.substring(imageDir,1);
// this triggers a String index out of range: -9: -- this value is

// always the negated value +1 of the of the starting position value.

//Then just for fun I put a literal value as the starting postition
// with the same results. the value after "out
of range:" is one less //then the value I placed there and negatated.

ie:
lastChar = imageDir.substring(5,1);
//gets String index out of range: -4
//and so forth
Jul 17 '05 #3

P: n/a
Reread my posting again!
lastChar = imageDir.substring(5,1);


And you should ask for help in:

comp.lang.java.help

--
Regards,
Casey
Jul 17 '05 #4

P: n/a
Casey Hawthorne wrote:
Reread my posting again!

lastChar = imageDir.substring(5,1);


Oh crap! I'm so used to programming languages where the second parm of
the substring function is the number of characters to retrieve counting
from the position represented by the first parm that I completely
overlooked the specs that said it was also index.
What I needed to do was:

strLength = imageDir.length();
lastChar = imageDir.substring(strLength-1,strLength);

Which I did and it worked.

Jul 17 '05 #5

P: n/a
Don Freeman wrote:
[..]
What I needed to do was:

strLength = imageDir.length();
lastChar = imageDir.substring(strLength-1,strLength);

Not really,

String lastChar = imageDir.substring( imageDir.length() - 1);

would be enough and does IMHO reflect better on what you are trying to do,
the two line version requires me to _think more_ -- and I'm lazy :)

just my $0.02
kind regards,
Tom
--
Dievision GmbH | Kriegerstrasse 44 | 30161 Hannover
Telefon: (0511) 288791-0 | Telefax: (0511) 288791-99
http://www.dievision.de
Jul 17 '05 #6

P: n/a
Tom Regner wrote:
Don Freeman wrote:
[..]
What I needed to do was:

strLength = imageDir.length();
lastChar = imageDir.substring(strLength-1,strLength);


Not really,

String lastChar = imageDir.substring( imageDir.length() - 1);

would be enough and does IMHO reflect better on what you are trying to do,
the two line version requires me to _think more_ -- and I'm lazy :)

Your right, the last argument is superflous as I only want the one
character returned anyway.

Take Care,
Don Freeman
(RPG Programmer struggling to learn JAVA)

BTW: in my opinion "laziness" is a sign of a good programmer.
Jul 17 '05 #7

P: n/a
In addition, most students/newbies don't seem to realize that most
working programs are many^3 lines of code -- so only passing one
argument lets the compiler parse faster and the interpreter run
faster.

Which would make a difference on using many substring operations.

String lastChar = imageDir.substring( imageDir.length() - 1);


--
Regards,
Casey
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.