Results 1 to 2 of 2
  1. #1
    niksatjava is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Question 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...

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Have a look at this:

    Java Code:
    String text = "this is a sample text which I want to split w.r.t space and store it in an array";
    String[] tokens= text.split("\\s+");
    kind regards,

    Jos

Similar Threads

  1. Debug a code
    By abdullahansari in forum New To Java
    Replies: 2
    Last Post: 04-18-2010, 01:52 AM
  2. Replies: 1
    Last Post: 11-16-2009, 07:12 AM
  3. Debug challenge: how do you debug a Java program?
    By CJSLMAN in forum New To Java
    Replies: 60
    Last Post: 07-04-2009, 10:28 AM
  4. Replies: 3
    Last Post: 04-14-2009, 04:44 PM
  5. how to debug java code in eclipse
    By rajesh058 in forum Advanced Java
    Replies: 3
    Last Post: 07-25-2007, 11:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •