如何使用 Java 的 StringTokenizer 访问特定令牌?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19162989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:38:12  来源:igfitidea点击:

How do access specific tokens with Java's StringTokenizer?

javavariable-assignmentstringtokenizer

提问by dwwilson66

I'm using Buffered Reader to pass individual lines of a file to Java's StringTokenizer. The file is structurd as follows:

我正在使用 Buffered Reader 将文件的各个行传递给 Java 的StringTokenizer. 该文件的结构如下:

"2,0";"12345";"foo";"foo.doc"
"2,4";"23456";"foo";"foo.doc";"34567";"foo7";"foo7.doc";"45678";"foo6";"foo6.doc";"56789";"foo5";"foo5.doc";"67890";"foo4";"foo4.doc"   
"3,0";"34567";"foo7";"foo7.doc"
"3,0";"45678";"foo6";"foo6.doc"
"3,0";"56789";"foo5";"foo5.doc"
"3,0";"67890";"foo4";"foo4.doc"

Here's the code I'm using--so far.

这是我正在使用的代码 - 到目前为止。

public class parse {
  public static void main(String args[]) {
    FileInputStream inputStream = new FileInputStream("whidata0.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 
    while((scrubbedInput=br.readLine())!=null) {
      StringTokenizer strTok = new StringTokenizer(scrubbedInput, ";", false);
      int tokens = strTok.countTokens();
      while (strTok.hasMoreTokens()) {
        tok01 = strTok.nextToken();
      }
      System.out.println("  scrubbed: " + scrubbedInput);
      System.out.println("    tokens: " + tokens);
      System.out.println("     tok01: " + tok01);
    }
  }
}

I need to be able to assign each token in a string to a variable to do additional manipulation. However, if I assign those variable in my whileloop, the iteration will overwrite my variables, and they will all return with the same value.

我需要能够将字符串中的每个标记分配给一个变量以进行额外的操作。但是,如果我在while循环中分配这些变量,迭代将覆盖我的变量,并且它们都将返回相同的值。

I'm trying to devide a way to do the following:

我正在尝试制定一种方法来执行以下操作:

String token01 = strTok.tokenNumber(0);
String token02 = strTok.tokenNumber(1);
String token03 = strTok.tokenNumber(2);
String token04 = strTok.tokenNumber(3);
etc.

but cannot find any methods in the String Tokenizer documentation that will allow that. I can certainly write each line to a String array of thisLineOfTokens[]and use a for loop to create String tokenN = thisLineOfTokens[n], but is there a more direct way to access specific tokens?

但在 String Tokenizer 文档中找不到任何允许这样做的方法。我当然可以将每一行写入一个 String 数组thisLineOfTokens[]并使用 for 循环来 create String tokenN = thisLineOfTokens[n],但是有没有更直接的方法来访问特定令牌?

I'm kinda lost about the best way to reference a SPECIFIC token from my string.

我有点迷失了从我的字符串中引用特定令牌的最佳方法。

采纳答案by Gangaraju

You can use String.splitfor that instead of a StringTokenizer.

您可以使用String.split它代替StringTokenizer.

String[] split = scrubbedInput.split(";");

split[2]; //index=2

回答by Max Gabderakhmanov

Info from here:

来自这里的信息:

It is recommended that anyone seeking this functionality use the splitmethod of String or the java.util.regex package instead.

建议任何寻求此功能的人改用String的split方法或 java.util.regex 包。

So, you can use something like this:

所以,你可以使用这样的东西:

String testLine = "your;test;data;"

String[] result = testLine.split(";");
for (int x=0; x<result.length; x++){
    System.out.println(result[x]);
}

Output:

输出:

your
test
data