473,513 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

substring problems for java noobie

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
7 6381
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8615
by: Tim Smith | last post by:
I am looking to extract form element values from html, more generally I have a substring that identifies the beginning of a value and a string that identifies the end of value and I need to extract...
2
2315
by: osgnamah | last post by:
Hi All; I am getting ready to launch a shareware program in the next few weeks and the last step is going to be creating a website. So the last few days I've been doing some web surfing in...
1
1647
by: Clarker | last post by:
Hi there! When I use the substring function of the string class in the command window, I get the error: "Parameter is incorrect" The same code works perfectly fine when executed in the code...
6
1933
by: Aristotelis E. Charalampakis | last post by:
Hi all, this is a newbie question :-) I was wondering if there was a way to use the switch statement in a manner that each case statement includes more that a simple value. i.e.: switch (...
15
6058
by: Duncan Allen | last post by:
Hi, Using C# I'm trying to use the substring method of a string variable but it just generates an "error: 'variable.Substring' does not exist " exception - how do I fix this ? code example: ...
1
3053
by: supergrover1981 | last post by:
Gidday gang, I've been teaching myself CSS over the past 2 days and for the most part I thought I had it all working. All the problems I've had have been in IE...until now. If anyone could offer...
4
3250
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic">...
2
2409
by: Daniel Reardon | last post by:
All, I'm sure that this is an old question, but I'm having problems getting a substring of a passed in variable. I can do other string manipulation with it such as concatonation, but I'm really...
53
3360
by: yinglcs | last post by:
Hi, In java, there is an 'indexOf' function in String which does this: indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. is there...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.