Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Allen Thomas
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!! .
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Allen Thomas
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cindy.
Is there a way to just download the regex package by itself and place within jdk1.3?
Allen
 
Greg Ostravich
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Allen Thomas
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic