简体   繁体   English

如何在Java中将StringTokenizer结果带到ArrayList?

[英]How to take StringTokenizer result to ArrayList in Java?

I want to take StringTokenizer result to ArrayList . 我想将StringTokenizer结果带到ArrayList I used following code and in 1st print statement, stok.nextToken() print the correct values. 我使用了以下代码,并在第一个打印语句中, stok.nextToken()打印了正确的值。 But, in second print statement for ArrayList give error as java.util.NoSuchElementException . 但是,在ArrayList的第二个打印语句中,给出错误为java.util.NoSuchElementException How I take these results to an ArrayList? 我如何将这些结果带到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);

        }
    }

Quoting javadoc of StringTokenizer : 引用StringTokenizer javadoc:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code . StringTokenizer是一个遗留类,出于兼容性原因而保留,尽管在新代码中不鼓励使用它 It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. 建议任何寻求此功能的人改用Stringsplit方法或java.util.regex包。

"New code" meaning anything written for Java 1.4 or later, ie ancient times. “新代码”表示为Java 1.4或更高版本(即古代)编写的任何内容。


The while loop will extract all values from the tokenizer. while循环将从令牌生成器中提取所有值。 When you then call nextToken() after already having extracted all the tokens, why are you surprised that you get an exception? 当你再调用nextToken()已经有提取的所有令牌之后 ,为什么你惊讶,你会得到一个异常?

Especially given this quote from the javadoc of nextToken() : 特别是考虑到nextToken()的javadoc中的这个引用:

Throws NoSuchElementException if there are no more tokens in this tokenizer's string. 如果此令牌生成器的字符串中没有更多令牌,则抛出NoSuchElementException

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
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. 不要在while循环外部调用stock.nextToken,这会导致异常,并且在System.out.println中打印出arraylist不会帮助您使用for循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM