LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-18-2006, 05:31 PM   #1
spank
Member
 
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

ex:
<tag>text</tag>

until now i have done:

Code:
import java.io.*;
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");
		line=f.readLine();
		tok=new StringTokenizer(line,des);
		d=des;
	}

	public int type()
	{
		return 0; //PROBLEMS !!!
	}
	
	public boolean hasNext() throws IOException
	{
		if (!tok.hasMoreTokens())
		{
			line=f.readLine();
			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:
Code:
import java.io.*;
public class pb6
{
	public static void main(String[] args) throws IOException
	{
		TagTokenizer a=new TagTokenizer("test.txt","<>");
		while(a.hasNext())
			if(a.type()==1) System.out.println("Tag"+a.next());
			else System.out.println("Text"+a.next());
	}

}
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
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842
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.
 
  


Reply


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


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

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