pls help me debug this code... pls..
I want to tokenize string w.r.t. whitespace and store it in an array..
below is the code for tokenizer.
import rece.Tokeni;
public class et {
String text = "this is a sample text which I want to split w.r.t space and store it in an array";
private void processText() {
String newtext[] = cutter(text,' ');
System.out.println(newtext);
}
public String[] cutter(String text,char delim) {
int index;
String curWord = null;
String s;
String list[] = new String[1000];
Tokeni tok = new Tokeni(text , delim);
while(tok.hasMoreTokens()) {
for(index = 0 ; index < text.length()-1 ; index++) {
s = tok.nextToken();
if(isnotspace(s)) {
curWord = curWord + s;
}
else break;
}
list[index] = curWord;
}
return list;
}
boolean isnotspace(String str) {
String space = " \t\r\n";
if(str.equals(space)) {
return false;
}
else return true;
}
public static void main(String args[]) {
et e = new et();
e.processText();
}
}
following is stored in a package rece.Tokeni.java.... I have imported it correctly in the above code.
package rece;
import java.util.NoSuchElementException;
public class Tokeni
{
private int currentPosition;
private int newPosition;
private int maxPosition;
private String str;
private char delimiter;
public Tokeni(String str, char delim)
{
currentPosition = 0;
newPosition = -1;
this.str = str;
maxPosition = str.length();
delimiter = delim;
}
public int skipDelimiters(int startPos)
{
int position = startPos;
do{
if(position >= maxPosition)
break;
char c = str.charAt(position);
if(c != delimiter)
break;
position++;
} while(true);
return position;
}
public int scanToken(int startPos)
{
int position = startPos;
do{
if(position >= maxPosition)
break;
char c = str.charAt(position);
if(c == delimiter)
break;
position++;
} while(true);
return position;
}
public boolean hasMoreTokens()
{
newPosition = skipDelimiters(currentPosition);
return newPosition < maxPosition;
}
public String nextToken()
{
currentPosition = newPosition < 0 ? skipDelimiters(currentPosition) : newPosition;
newPosition = -1;
if(currentPosition >= maxPosition)
throw new NoSuchElementException();
else{
int start = currentPosition;
currentPosition = scanToken(currentPosition);
return str.substring(start, currentPosition);
}
}
public int countTokens()
{
int count = 0;
int currpos = currentPosition;
do{
if(currpos >= maxPosition)
break;
currpos = skipDelimiters(currpos);
if(currpos >= maxPosition)
break;
currpos = scanToken(currpos);
count++;
} while(true);
return count;
}
}
it is getting compiled correctly but throwing a runtime error..
Exception in thread "main" java.util.NoSuchElementException
at rece.Tokeni.nextToken(Tokeni.java:65)
at sam.et.cutter(et.java:30)
at sam.et.processText(et.java:16)
at sam.et.main(et.java:51)
Java Result: 1
BUILD SUCCESSFUL
since i am new to java i am not able to fix the error.. please do help me out.
thanks in advance...