Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-18-2006, 05:31 PM   #1
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Rep: Reputation: 30
Define a tokenizer class

I want to create a class with the following methods:

String next(); // next element
boolean hasNext(); // true if there are still elements
int type() // return 1 if it is a tag and 0 if it is test


until now i have done:

import java.util.*;
public class TagTokenizer {
	private RandomAccessFile f;
	private StringTokenizer tok;
	private String line,d;

	public TagTokenizer(String s,String des) throws IOException
		f=new RandomAccessFile(s,"r");
		tok=new StringTokenizer(line,des);

	public int type()
		return 0; //PROBLEMS !!!
	public boolean hasNext() throws IOException
		if (!tok.hasMoreTokens())
			if(line!=null) tok=new StringTokenizer(line,d);
		if(line==null) return false;
		return true;

	public String next() throws IOException
		return tok.nextToken(d);
and to be used like this:
public class pb6
	public static void main(String[] args) throws IOException
		TagTokenizer a=new TagTokenizer("test.txt","<>");
			if(a.type()==1) System.out.println("Tag";
			else System.out.println("Text";

my problems are with type() method. my ideea is to retain in a variable the next token with separator ">" and if it begins with "<" it is a tag.
The problem is that I want to have a string variable s=tok.nextToken(); but I don't want for the token string to jump to the next item, I just want to get the token without jumping.
How can I do that?

I'm really sorry if I didn't explain ok... I'm not a native English.

Thank you!
Old 10-19-2006, 11:10 AM   #2
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,199

Rep: Reputation: 1390Reputation: 1390Reputation: 1390Reputation: 1390Reputation: 1390Reputation: 1390Reputation: 1390Reputation: 1390Reputation: 1390Reputation: 1390
You will need a String variable current which needs to be updated whenever you call next(). Probably you will want to add a getCurrent() method too. Also I think you need to construct the StringTokenizer with new StringTokenizer(line,des,true) so that is doesn't throw away the "<>"s.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM
php projects: define each class in a seperate file? ldp Programming 4 09-16-2004 05:00 PM
Tokenizer for Open Firmware drsparikh Linux - Software 0 03-16-2004 04:23 AM
Java tokenizer problem Andy@DP Programming 7 03-02-2004 01:49 AM
java + string tokenizer dave bean Programming 2 12-10-2003 03:22 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:49 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration