Results 1 to 5 of 5

Thread: Tokenizer help!

  1. #1
    joeyalf is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Tokenizer help!

    I am having trouble, after i enter the numbers seperated by commas, the program goes no further but keeps running. I am not sure what the error is. I am trying to get the total number of tokens, so that I can create and array from that. Then I would like to convert these tokens into ints and store them in an array. Please let me know if you see any other problems, I just can't get past the second while loop. Thanks

    edit: I also tried while (strTokenizer.hasMoreTokens() )

    System.out.print("Do you want to sort (yes/no)? ");
    input = keyboard.nextLine();


    while(input.equals("yes"))
    {


    System.out.println("Enter your numbers separated by commas below:");
    allNumbers = keyboard.nextLine();


    String in;
    StringTokenizer strTokenizer = new StringTokenizer(allNumbers, "," , false);
    int counter = 0;


    while (strTokenizer.hasMoreTokens() == true)
    {
    counter++;
    }


    int [] numbers = new int [counter];



    while (strTokenizer.hasMoreTokens() == true)
    {

    in = strTokenizer.nextToken();
    int token = Integer.parseInt(in);
    for(int i = 0; i <= counter; i++)
    numbers[i]=token;

    }
    Last edited by joeyalf; 12-07-2011 at 01:25 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Tokenizer help!

    Easy solution use String.split instead.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Tokenizer help!

    The solution to your current code, the first loop exits when there are no more tokens left. Therefore it is impossible to enter the second loop.

  4. #4
    joeyalf is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Tokenizer help!

    oh i see that makes sence, thank you, how would i go about using the split method

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Tokenizer help!

    Same way you use any other method. Try reading the API for more info.

Similar Threads

  1. String Tokenizer HELP!
    By alloftheglamour in forum Java Software
    Replies: 1
    Last Post: 11-10-2011, 09:01 AM
  2. Tokenizer
    By skaterboy987 in forum New To Java
    Replies: 7
    Last Post: 10-21-2011, 06:07 AM
  3. String Tokenizer help
    By GreenTea in forum New To Java
    Replies: 4
    Last Post: 10-30-2010, 03:44 AM
  4. String Tokenizer
    By hussainian in forum Advanced Java
    Replies: 1
    Last Post: 03-16-2010, 09:58 AM
  5. String Tokenizer
    By viperlasson in forum New To Java
    Replies: 1
    Last Post: 03-09-2010, 02:14 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
  •