Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer probelm

 
Pratibha Nandwana
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pratibha Nandwana
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = "...";

?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic