3

I want to take StringTokenizer result to ArrayList. I used following code and in 1st print statement, stok.nextToken() print the correct values. But, in second print statement for ArrayList give error as java.util.NoSuchElementException . How I take these results to an ArrayList?

 import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.StringTokenizer;

        public class Test {
        public static void main(String[] args) throws java.io.IOException {

            ArrayList<String> myArray = new ArrayList<String>();
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter : ");
            String s = br.readLine();
            StringTokenizer stok = new StringTokenizer(s, "><");
            while (stok.hasMoreTokens())
                System.out.println(stok.nextToken()); 
            // -------until now ok

            myArray.add(stok.nextToken()); //------------???????????
            System.out.println(myArray);

        }
    }
3
  • 1
    Look at your while statement. If execution exits the loop, what does that mean for stok? Commented Apr 27, 2016 at 15:55
  • Thank you very much for your reply. So, How I will take this result in to an ArrayList? Commented Apr 27, 2016 at 15:57
  • 1
    stok has been read in full by the while loop, so calling nextToken() after the loop completes will of course throw an (end-of-data) error. Did you intend to have the add() call inside the loop? If so, you're missing some {braces}. Otherwise, what are you trying to do? Edit question and explain. Commented Apr 27, 2016 at 16:10

2 Answers 2

7

Quoting javadoc of StringTokenizer:

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.

"New code" meaning anything written for Java 1.4 or later, i.e. ancient times.


The while loop will extract all values from the tokenizer. When you then call nextToken() after already having extracted all the tokens, why are you surprised that you get an exception?

Especially given this quote from the javadoc of nextToken():

Throws NoSuchElementException if there are no more tokens in this tokenizer's string.

Did you perhaps mean to do this?

ArrayList<String> myArray = new ArrayList<>();
StringTokenizer stok = new StringTokenizer(s, "><");
while (stok.hasMoreTokens()) {
    String token = stok.nextToken(); // get and save in variable so it can be used more than once
    System.out.println(token); // print already extracted value
    // more code here if needed
    myArray.add(token); // use already extracted value
}
System.out.println(myArray); // prints list
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply. I have missed {braces} of while.
You didn't just miss the braces. The add() call was not indented, so you didn't even show the intent of the code. Indentation is very important for human readers of your code. As the code is written in the question, it wasn't clear what you even wanted the code to do. Missing braces was the least of the problem.
1
ArrayList<String> myArray = new ArrayList<String>();        
while (stok.hasMoreTokens()){
myArray.add(stok.nextToken());
}

dont call stock.nextToken outside the while loop that results in exceptions and printing out arraylist in System.out.println wont help you have to use a for loop.

for(String s : myArray){
System.out.Println(s);
}

4 Comments

Don't know where my comment went, but printing an ArrayList will not give you the memory location. It will print all the elements of the list. Even if it didn't, it wouldn't print a memory location, because output like List@5ad6521c is a hash code, not a memory location!!
you never mention that in the comments. appreciate your reply thanks.
I did, and I will do it again: It will print all the strings in the list. It will not print hash codes.
yes the out put will be printed within []. thank you again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.