Results 1 to 6 of 6
  1. #1
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default error at object to int

    Hi, plz help. i have no idea why it's error.
    this my code:
    Java Code:
    while ((str = br.readLine()) != null){
    	                    StringTokenizer tokenizer = new StringTokenizer(str," ");
    			    System.out.println("test1: "+tokenizer.nextToken());
    			
    			    while(tokenizer.hasMoreTokens()){
    			    	  System.out.println("test:"+tokenizer.nextElement());
    			    	    int token1 = new Integer((tokenizer.nextElement()).toString()); //this line i figure out that error. is it wrong i convert? 
    			    	     	vec. add(token1);
    			    	    
    			    }
    			   
    			}

  2. #2
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    it is not error when compile, but when run i get error Caughtjava.util.NoSuchElementException

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What is the exact error message?

    edit: so you've run out of tokens. You're calling it twice inside the loop, but checking next token only once. That first call, the test call wastes the token by the way.

  4. #4
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    sorry, i dont really understand what u means? it is my loop was wrong? but why when i remove that line, it was ok?
    int token1 = new Integer((tokenizer.nextElement()).toString());
    vec. add(token1);

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This line:
    Java Code:
     System.out.println("test:"+tokenizer.nextElement());
    gets a token from the StringTokenizer but you do nothing with it. The StringTokenizer then moves to the next token which you then get and use. So inside the loop you are getting two tokens and discarding the first. Any time you call nextElement() the Tokenizer grabs a token and moves on to the next one.

    If you want to check the token, get it and put it into a String variable, then print out the String variable, and then use the same String variable to parse to an int. The key is to call nextElement only once in the loop.

  6. #6
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  2. object new and pointers error
    By hydride in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 07:15 PM
  3. Error showing "Object Required"
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2009, 03:23 PM
  4. Replies: 1
    Last Post: 03-04-2009, 07:14 PM
  5. Replies: 2
    Last Post: 12-03-2008, 12: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
  •