【问题标题】:String tokenizer to array java字符串标记器到数组java
【发布时间】:2013-05-25 13:04:30
【问题描述】:

我正在尝试使用字符串标记器将数据从文件导入数组。

文件中的数据格式是

AA,BB,CC
AA,BB,CC

但我不断收到错误

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++;
             }

         }


}

}

因此,我们将不胜感激。我不能使用数组列表,以便脱离上下文

【问题讨论】:

  • 如果所有行都有这种格式,为什么不直接使用.split()
  • 你不应该在调用每个nextElement() 之前检查hasMoreElements() 吗?
  • @fge 我是 java 新手,可以告诉我该怎么做吗?
  • @我检查更多元素的新白痴“strings.hasMoreElements()”
  • 您可以随时尝试调试...

标签: java arrays file stringtokenizer


【解决方案1】:

你不能在while语句中多次调用.nextElement(); 在它们中的每一个之前,必须调用 .hasNextLine()

【讨论】:

  • 是的。我现在明白了.. 谢谢:)
  • 你的意思是hasMoreElements()
【解决方案2】:

我建议你使用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++;
         }
    }

}

【讨论】:

  • 快速问题:String [] cName = new String [100];说我不知道​​我是否需要 100 个数组大小,让它动态的最好方法是什么,所以如果我有 105 个元素,我仍然可以将它们保存在数组中
  • @user2291480 如果您需要动态数组,请使用ArrayList
  • 没有 ArrayList 有什么办法吗?
  • 嘿,我如何转换“year[z] = line.split(",")[0];"到int?
  • @user2291480 试试Integer.parseInt(line.split(",")[0])
【解决方案3】:

您也可以使用以下代码:

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);
        }
    }

}

【讨论】:

  • 嘿,谢谢,但字符串必须是数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 2018-08-10
  • 2012-04-20
相关资源
最近更新 更多