aspose file tools*
The moose likes Beginning Java and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Allen Thomas
Greenhorn

Joined: Jul 02, 2002
Posts: 29
Hi,
I am traversing through a string, character by character, how would I know the end of the string?
Example code:
...
...
String testStr = "Java is awesome!";
String testStr2 = "e";
int index = 0;
while (index != str.length()){
char ch = str.charAt(index);
if (ch == 'e'){
char ch1 = str.charAt(index+1);
if (ch1 == '!'){
doSomething();
}
}
}
index++; //index is a counter to go traverse char //by char in the string.
...
...
The problem I am having is that testStr passes but testStr2 doesn't. I get error message:
java.lang.StringIndexOutOfBoundsException:
String index out of range: 16. The problem is in this line: char ch1 = str.charAt(index+1); I am looking ahead by indexing by one. Is there a better way of looking ahead a character when traversing through a string? I am not using j2se1.4 that has regular expressions, which would have been easier.
Thanks in advance in helping a newbie.
Allen
[ August 20, 2002: Message edited by: Allen Thomas ]
[ August 20, 2002: Message edited by: Allen Thomas ]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Apparently there is no reason for you to actually look at the last character in this if section - since you can't capture the char+1 for it.
if (ch == 'e' & index < str.length() ){
Yeah - not only that but the regex stuff is more FUN!! .


"JavaRanch, where the deer and the Certified play" - David O'Meara
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I am not using j2se1.4 that has regular expressions
You may have a reason not to use them, but there are decent & free third party packages for handling regular expressions in Java that can be quickly and easily integrated in to a project. Stevesoft's Pat is one of them.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Allen Thomas
Greenhorn

Joined: Jul 02, 2002
Posts: 29
Thanks Cindy.
Is there a way to just download the regex package by itself and place within jdk1.3?
Allen
Greg Ostravich
Ranch Hand

Joined: Jul 11, 2002
Posts: 112
The Jakarta project has a regular expression package too. I haven't looked at regular expressions much for 1.4 but from what I've heard they sound similar.
Here's a link to the Jakarta Regular Expression Package
[ August 20, 2002: Message edited by: Greg Ostravich ]

Greg Ostravich - SCPJ2
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Is there a way to just download the regex package by itself and place within jdk1.3?
I doubt it. Sun probably thinks that if you want the function of Java 1.4, then you'd download Java 1.4.
If you don't want to use the Java 1.4 SDK and API, you could download it and take a look at the source code to try and create your own regex package, but just downloading and using one of the free regular expression packages would be a bit less work.
Good Luck.
Allen Thomas
Greenhorn

Joined: Jul 02, 2002
Posts: 29
Hi,
If I download the regex package, do I decompile it using one of those de-compilers? If so, do you know any good ones?
Thanks.
Greg Ostravich
Ranch Hand

Joined: Jul 11, 2002
Posts: 112
If you download Jakarta's Regular Expression package why would you have to decompile it?
Just download it, include the jar in your classpath, use the package in your code and have fun.
Here's the Jakarta Regexp Package JavaDoc URL to see how you need to use it. There's even a REDemo class test harness to show you how to use it.
The source code comes bundled with the download for the package.
By the way - Jakarta is a part of the Apache Project - part of the Apache Software Foundation.
The source code and the jar are free downloads.
[ August 21, 2002: Message edited by: Greg Ostravich ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Unfortuantely, the Jakarta people seem to have removed the FAQ which should be at
http://jakarta.apache.org/jyve-faq/Turbine/screen/DisplayTopics/action/SetAll/project_id/2/faq_id/27 , but isn't.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Allen Thomas
Greenhorn

Joined: Jul 02, 2002
Posts: 29
Greg,
I was asking Dirk the question about downloading the J2SE 1.4 and just using the regex package and decompiling it because he told that I could see what they did in their regex.
However, I will try your suggestion as well. Thanks.
Allen
[ August 21, 2002: Message edited by: Allen Thomas ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Allen,
The source code for the J2SE API is included with the J2SDK download. If I recall correctly, installing the jar file called "src.jar" is one of the options when installing the SDK. You can use a program like WinZip to unzip the jar file, then browse to the code you'd like to look at..
I wouldn't be too hopeful about being able to extract the java.util.regex code to create a regex package of your own. You might be able to do it. Changes would have to be made. It'd be quite a bit easier to use an existing regex package as mentioned.
Good Luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings