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

String operations

Ashutosh Deo
Greenhorn

Joined: Jul 12, 2006
Posts: 10
I have this condition statement.

if(firstval != secondval )
then(result1)
else if(secondValue == null)
then(result2)
else(result3)


I want to store the sub parts of each condition in separate strings.

For eg. in the above string,

For the first if Condition, I need the sub parts to be saved in Strings as follows ->

leftCondition = "firstval"
operator = "!="
rightCondition = "secondval"
result = "result1"

Similarly I need 4 sub parts for the else if condition and 1 sub part(result3) for the last else condition.


Can someone give me the java code for the same?

[ July 12, 2008: Message edited by: Ashutosh Deo ]
[ July 12, 2008: Message edited by: Ashutosh Deo ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
You want to design a parser?

Buy a copy of the classic books about compilers, by Aho. The 2nd (2007) edition uses Java code.
Download JFlex and look at its manual. JFlex is written in (rather old-fashioned) Java.

You are suggesting a task of that sort of magnitude. Nobody will have code available to pass on to you, I am afraid.

BTW: In Java you can't reliably use the == operator or != on object references, which includes Strings.
Ashutosh Deo
Greenhorn

Joined: Jul 12, 2006
Posts: 10
How about using StringTokenizer?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
Slightly simpler than what I thought you wanted . . . thank Goodness!

I thought you actually wanted to take the conditions from the Excel file into Java code.

StringTokenizer would work, but it is regarded as legacy code. This is what it says in the API documentation
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
More about regular expressions here.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
RFC - split vs StringTokenizer
I wrote a test program to see which was faster in a particular application.
StringTokenizer took 1/2 the time of split.


// Test speed of split vs StringTokenizer

import java.util.*;

public class TestSplit {
static int LP_CNT = 5000;
static String data = "This is the string to be split. let's see how long it takes to split it lots"
+ " of times (1000) or more.";

public static void main(String[] args) {
long start = 0;

start = System.nanoTime();
String[] dArray2 = doST();
System.out.println(" ST took " + (System.nanoTime() - start) + " nbr words=" + dArray2.length);

start = System.nanoTime();
String[] dArray1 = doSplit();
System.out.println("split took " + (System.nanoTime() - start) + " nbr words=" + dArray1.length);

// Check same results
if(dArray1.length != dArray2.length)
System.out.println("diff lengths " +dArray1.length + " " +dArray2.length);
for(int i=0; i < dArray1.length; i++) {
if(!dArray1[i].equals(dArray2[i]))
System.out.println("diff words at " + i + " 1=" + dArray1[i] + ", 2=" + dArray2[i]);
}
} // end main()

public static String[] doSplit() {
String[] split = null;
for (int i=0; i < LP_CNT; i++) {
split = data.split(" ");
}
return split;
}

public static String[] doST() {
String[] split = null;
for (int i=0; i < LP_CNT; i++) {
StringTokenizer st = new StringTokenizer(data);
split = new String[st.countTokens()];
int j = 0;
while(st.hasMoreTokens()) {
split[j++] = st.nextToken();
}
}
return split;
}
} // end class

/* NOTE: split takes 3* as long as StringTokenizer!!!
// Nbr of loops = 1000
split took 61428198 nbr words=22
ST took 25317464 nbr words=22

split took 61650014 nbr words=22
ST took 23223343 nbr words=22

split took 65775957 nbr words=22
ST took 23929298 nbr words=22
// Changed # loops to 5000
split took 129536296 nbr words=22
ST took 72610345 nbr words=22

// reversed order >>>>>> ST takes longer, split takes less???
ST took 27886785 nbr words=22
split took 57321251 nbr words=22

ST took 27479191 nbr words=22
split took 57208667 nbr words=22

// Changed nbr loops tp 5000
ST took 77422131 nbr words=22
split took 128255686 nbr words=22
*/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String operations