在多行文本文件上使用StringTokenizer时出错 [英] Error whilst using StringTokenizer on text file with multiple lines

查看:513
本文介绍了在多行文本文件上使用StringTokenizer时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



文本文件看起来像这样;



  a 2000 

4
b 3000
c 4000
d 5000

现在,我要做的是从文本文件中获取每个单独的字符并将其存储到数组列表中。

这是我的代码;

  import java.io.BufferedReader; 
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public static void main(String [] args){

String fileSpecified = args [0];

fileSpecified = fileSpecified.concat(。txt);
字符串行;
System.out.println(file Specified =+ fileSpecified);

ArrayList< String> words = new ArrayList< String> ();


尝试{
FileReader fr = new FileReader(fileSpecified);
BufferedReader br = new BufferedReader(fr);
line = br.readLine();

StringTokenizer标记; $()
while((line = br.readLine())!= null){
token = new StringTokenizer(line);
words.add(token.nextToken());
}
} catch(IOException e){
System.out.println(e.getMessage());


for(int i = 0; i< words.size(); i ++){
System.out.println(words =+ words.get (一世));
}



}

我得到的错误信息是这样的;

 线程main中的异常java.util.NoSuchElementException $ b $ java .util.StringTokenizer.nextToken<未知来源> getWords.main中的
< getWords.java:32>

其中'getWords'是我的java文件的名称。



谢谢。

解决方案

你总是要检查 StringTokenizer.hasMoreTokens()第一。如果没有更多的标记可用,则抛出 NoSuchElementException 是记录的行为:

  token = new StringTokenizer(line); 
while(token.hasMoreTokens())
words.add(token.nextToken());



b)不要为每行创建一个新的Tokenizer,除非您的文件太大进入记忆。将整个文件读取到一个字符串,让标记器工作在

I'm trying to read a text file and split the words individually using string tokenizer utility in java.

The text file looks like this;

a 2000

4  
b 3000  
c 4000  
d 5000

Now, what I'm trying to do is get each individual character from the text file and store it into an array list. I then try and print every element in the arraylist in the end.

Here is my code;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public static void main(String[] args) {

    String fileSpecified = args[0];

    fileSpecified = fileSpecified.concat(".txt");
    String line;
    System.out.println ("file Specified = " + fileSpecified);

    ArrayList <String> words = new ArrayList<String> ();


    try {
        FileReader fr = new FileReader (fileSpecified);
        BufferedReader br = new BufferedReader (fr);
        line = br.readLine();

        StringTokenizer token;
        while ((line  = br.readLine()) != null) {
            token = new StringTokenizer (line);
            words.add(token.nextToken());
        }
    } catch (IOException e) {
        System.out.println (e.getMessage());
    }

    for (int i = 0; i < words.size(); i++) {
        System.out.println ("words = " + words.get(i));
    }



}

The error message I get is this;

Exception in thread "main" java.util.NoSuchElementException   
                at java.util.StringTokenizer.nextToken<Unknown Source>  
                at getWords.main<getWords.java:32>  

Where 'getWords' is the name of my java file.

Thankyou.

解决方案

a) You always have to check StringTokenizer.hasMoreTokens() first. Throwing NoSuchElementException is the documented behaviour if no more tokens are available:

token = new StringTokenizer (line);
while(token.hasMoreTokens())
    words.add(token.nextToken());

b) don't create a new Tokenizer for every line, unless your file is too large to fit into memory. Read the entire file to a String and let the tokenizer work on that

这篇关于在多行文本文件上使用StringTokenizer时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆