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);
}
}
whilestatement. If execution exits the loop, what does that mean forstok?stokhas been read in full by the while loop, so callingnextToken()after the loop completes will of course throw an (end-of-data) error. Did you intend to have theadd()call inside the loop? If so, you're missing some {braces}. Otherwise, what are you trying to do? Edit question and explain.