Results 1 to 9 of 9
  1. #1
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Array prints nulls instead of tokens

    Hey guys, I have a problem with arrays. Basically, I have used a stringtokenizer to split the strings, and got them to store in an array. When I print the array, it prints something like "[null, null, null]" it has been bugging me. I just want it to print the tokens that are in the array instead of nulls. But when I use .split it works fine, but not with tokens.

    Here's a snippet of my code:

    Java Code:
    Scanner input = new Scanner(System.in); 	 
    String str = input.nextLine();
    StringTokenizer st = new StringTokenizer(str, "(), ");
    String[] tokens = new String[st.countTokens()];
    System.out.println(Arrays.toString(tokens));
    I think the problem is something to do with the last two lines, or am I missing something? Does anyone know the problem?

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Array prints nulls instead of tokens

    So, you declar an array, did you populated it?

  3. #3
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Array prints nulls instead of tokens

    Quote Originally Posted by milovan View Post
    So, you declar an array, did you populated it?
    I might have not, but when I did...

    Java Code:
    String[] tokens = new String[30];
    Still print nulls

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default Re: Array prints nulls instead of tokens

    That's not populating the array. That's just allocating storage. Basically, you just obtained 30 buckets which hold strings. But the buckets are
    all empty. So you need to put some strings in there.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Array prints nulls instead of tokens

    Ohh sorry yeah you're right. I misread it. But in my code:

    Java Code:
    Scanner input = new Scanner(System.in);      
    String str = input.nextLine();
    StringTokenizer st = new StringTokenizer(str, "(), ");
    String[] tokens = new String[st.countTokens()];
    System.out.println(Arrays.toString(tokens));
    On the second line, the user inputs strings and they get split using a tokenizer and stored into an array. Even when I initialise the size of the array, it still outputs nulls instead of the tokens.

    Or do I need to convert tokens into strings?

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

    Default Re: Array prints nulls instead of tokens

    No.

    Once again you are misinterpreting things. You read in a String and you create a StringTokenizer but you never use it to tokenise the String.

  7. #7
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Array prints nulls instead of tokens

    Quote Originally Posted by Junky View Post
    No.

    Once again you are misinterpreting things. You read in a String and you create a StringTokenizer but you never use it to tokenise the String.
    I did a bit more research about this, and I have got it to work now.

    Java Code:
    int i = 0;
    while (st.hasMoreElements()) {
    tokens[i] = st.nextToken();
    i++;
    }
    System.out.println(Arrays.toString(tokens));
    However, I feel I could have used a for loop, but ahh well it works now which is good.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Array prints nulls instead of tokens

    Quote Originally Posted by juxta View Post
    I feel I could have used a for loop
    So what? While loops are cool guys too, don't try to avoid them.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Array prints nulls instead of tokens

    Note what the API for StringTokenizer says, though:
    "
    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
    "
    The advantage of the split() method is that you get your String[] as the output, without having to write the looping code yourself.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 23
    Last Post: 08-30-2013, 04:31 PM
  2. Array prints line 2x
    By Cinnic in forum New To Java
    Replies: 6
    Last Post: 07-14-2013, 03:22 PM
  3. persistence NULLs
    By flanjl2 in forum Advanced Java
    Replies: 1
    Last Post: 09-21-2011, 04:59 AM
  4. tokens
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 12-02-2007, 11:30 PM
  5. tokens
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-25-2007, 02:39 AM

Posting Permissions

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