简体   繁体   English

字符串标记生成器到数组java

[英]String tokenizer to array java

I'm trying to import data from a file to array using string tokenizer. 我正在尝试使用字符串标记器将数据从文件导入到数组。

Data format in file is 文件中的数据格式是

AA,BB,CC
AA,BB,CC

But i keep getting error 但我不断收到错误

Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
    at java.util.StringTokenizer.nextElement(StringTokenizer.java:407)
    at main.main(main.java:36)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;


public class main {
public static void main(String[] args) throws FileNotFoundException {

    Scanner input;
    String line;
    int x=0;
    String [] cName = new String [100];
    String [] cMascot = new String [100];
    String [] cAlias = new String [100];





         input = new Scanner(new File("test.txt"));

         while(input.hasNextLine()) {

             line = input.nextLine();
             StringTokenizer strings = new StringTokenizer(line,",");

             while (strings.hasMoreElements()) {
                 cName[x] = strings.nextElement().toString();
                 cMascot[x] = strings.nextElement().toString();
                 cAlias[x] = strings.nextElement().toString();
                 x++;
             }

         }


}

}

So any help would be appreciated. 所以任何帮助将不胜感激。 I cant use array list so that out of the context 我不能使用数组列表,以便在上下文之外

you can't call .nextElement() many times in while statement; 你不能在while语句中多次调用.nextElement(); befor each of them .hasNextLine() must be called 因为每个人都必须调用.hasNextLine()

I suggest you use readLine and split ... 我建议你使用readLinesplit ...

public static void main(String[] args) throws FileNotFoundException {

    String line;
    int x=0;
    String [] cName = new String [100];
    String [] cMascot = new String [100];
    String [] cAlias = new String [100];

    try (BufferedReader input = new BufferedReader(new FileStreamReader("test.txt"))) {

         while ((line = input.readLine())!=null) {

             cName[x] = line.split(",")[0];
             cMascot[x] = line.split(",")[1];
             cAlias[x] = line.split(",")[2];
             x++;
         }
    }

}

You can have below code useful too : 您也可以使用以下代码:

public static void main(String[] args) throws FileNotFoundException {

    Scanner input;
    String line;

    String cMascot = null;
    String cAlias = null;
    String cName = null;

    input = new Scanner(new File("test.txt"));
    while (input.hasNextLine()) {
        line = input.nextLine();
        StringTokenizer strings = new StringTokenizer(line, ",");

        while (strings.hasMoreElements()) {
            cName = strings.nextToken();
            cMascot = strings.nextToken();
            cAlias = strings.nextToken();
            System.out.println("cName :" + cName);
            System.out.println("cMascot :" + cMascot);
            System.out.println("cAlias :" + cAlias);
        }
    }

}

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

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