File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes StringTokenizer probelm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer probelm" Watch "StringTokenizer probelm" New topic
Author

StringTokenizer probelm

Pratibha Nandwana
Greenhorn

Joined: Mar 03, 2005
Posts: 5
Hi
Im trying to get the two tokens mode and online but this pgm i wrote is returning me the output o,o. Why?

import java.util.*;

public class Test
{

public static void main (String[] args)
{
StringBuffer sb = new StringBuffer("<fieldname>mode</fieldname><fieldvalue>online</fieldvalue>");
String s = sb.toString();
StringTokenizer st = new StringTokenizer(s,"<fieldname></fieldname><fieldvalue></fieldvalue>");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}



}


}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

The second argument to StringTokenizer's constructor is a list of characters to consider as separators. Your very long argument includes every character in your actual tokens except "o", so that's all you get back!

You can't use StringTokenizer this way -- this isn't what it's meant for. For simple cases, you could use the java.util.regex package; but a complete HTML parser is a reasonably complex undertaking.


[Jess in Action][AskingGoodQuestions]
Pratibha Nandwana
Greenhorn

Joined: Mar 03, 2005
Posts: 5
Hi
Thnaks .. I finally did it like this.. Dot kno ifits gud enough ut it works...


import java.util.HashMap;

public class Test
{

public static void main(String[] args)
{

StringBuffer sb = new StringBuffer("<fieldname>serviceId</fieldname><fieldvalue>Eligibility</fieldvalue><fieldname>serviceId3</fieldname><fieldvalue>Eligibility3</fieldvalue><fieldname>serviceId1</fieldname><fieldvalue>Eligibility1</fieldvalue>");
String message = sb.toString();


String namestartTag = "<fieldname>";
String nameendTag = "</fieldname>";
String valuestartTag = "<fieldvalue>";
String valueendTag = "</fieldvalue>";

int startTagLocation = 0;
int endTagLocation = 0;
int valuestartTagLocation = 0;
int valueendTagLocation = 0;

startTagLocation = message.indexOf(namestartTag);
endTagLocation = message.indexOf(nameendTag);
HashMap h = new HashMap();
while(startTagLocation != -1)
{

String str1 = message.substring(startTagLocation+namestartTag.length(),endTagLocation);
System.out.println("str1-----"+str1);
String str_remain = message.substring(endTagLocation+nameendTag.length());

valuestartTagLocation = str_remain.indexOf(valuestartTag);
valueendTagLocation = str_remain.indexOf(valueendTag);
String str_value = str_remain.substring(valuestartTagLocation+valuestartTag.length(),valueendTagLocation );
message = str_remain.substring( valueendTagLocation+valueendTag.length());
startTagLocation = message.indexOf(namestartTag);
endTagLocation = message.indexOf(nameendTag);


h.put(str1,str_value);

} //while
System.out.println("serviceid" + h.get("serviceId"));

}//metod

}//clas
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Well, it's obviously going to be a lot of work to change if you find the XML changes at some point.

By the way, why are you saying

StringBuffer sb = new StringBuffer("...");
String message = sb.toString();

When you could equivalently just say

String message = "...";

?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer probelm