This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

StringTokenizer probelm  RSS feed

 
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());
}



}


}
 
author and iconoclast
Sheriff
Posts: 24220
40
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
Sheriff
Posts: 24220
40
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 = "...";

?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!