Results 1 to 2 of 2
- 05-07-2010, 09:39 PM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
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...
- 05-08-2010, 08:18 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Debug a code
By abdullahansari in forum New To JavaReplies: 2Last Post: 04-18-2010, 01:52 AM -
Is it possible to debug java code marked load-on-startup?
By Arthur Gardner in forum EclipseReplies: 1Last Post: 11-16-2009, 07:12 AM -
Debug challenge: how do you debug a Java program?
By CJSLMAN in forum New To JavaReplies: 60Last Post: 07-04-2009, 10:28 AM -
Debug gives same result when I change the code!
By RedSix in forum NetBeansReplies: 3Last Post: 04-14-2009, 04:44 PM -
how to debug java code in eclipse
By rajesh058 in forum Advanced JavaReplies: 3Last Post: 07-25-2007, 11:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks