读取由选项卡分隔的文件,并将这些单词放在ArrayList中 [英] Read a file separated by tab and put the words in an ArrayList

查看:108
本文介绍了读取由选项卡分隔的文件,并将这些单词放在ArrayList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个自学练习,以帮助我更多地了解Java,但是我陷入了这个问题。我有以下的txt文件:

$ pre $ code $名字Hobby
Susy吃鱼
安娜园艺
比利与朋友保龄球b $ b $ / code>

注意:名称和爱好由制表符分隔



读取所有行并将其放入数组列表(兴趣爱好)的最佳方法是什么?这个棘手的部分是

 与朋友一起吃鱼或打保龄球

有空格,必须放在一个数组下,显然我不能硬编码。这里是我现在的代码:

pre $ public void openFile(){
try {
FileInputStream fstream = new的FileInputStream( TextFile.txt的);
//使用DataInputStream读取二进制文本
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
ArrayList< String> names = new ArrayList< String>();
ArrayList< String> hobbies = new ArrayList< String>();
String lineJustFetched; ((lineJustFetched = br.readLine())!= null){
String [] tokens = lineJustFetched.split(\ t);

出现错误:


java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:
-1


该索引在选项卡上并不是非常有用。
有什么想法吗?

解决方案

好的,你需要做下面的配方:


  1. 创建一个 BufferedReader
  2. 开始读取数据到字符串变量名称为 lineJustFetched
  3. 通过调用 lineJustFetched.split(\t); 来拆分 String
  4. 迭代产生的 String [] 。检查你想进入 ArrayList 的令牌是不是

  5. 如果不是,添加单词到 ArrayList

你需要基于 \ t 值进行拆分,所以空格不会成为问题。

SSCCE

  import java。 io.BufferedReader; 
import java.io.FileReader;
import java.util.ArrayList;
$ b $ public class WordsInArray {
public static void main(String [] args){
try {
BufferedReader buf = new BufferedReader(new FileReader(/ home /小/下载/测试));
ArrayList< String> words = new ArrayList<>();
String lineJustFetched = null;
String [] wordsArray;

while(true){
lineJustFetched = buf.readLine();
if(lineJustFetched == null){
break;
} else {
wordsArray = lineJustFetched.split(\t);
for(String each:wordsArray){
if(!。equals(each)){
words.add(each);




$ b(String each:words){
System.out.println(each);
}

buf.close();

catch(Exception e){
e.printStackTrace();





输出

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $喜欢打网球
Sherlock
喜欢解决犯罪


I am doing a self learning exercise to help me understand more about Java, but I am stuck at this question. I have the following txt file:

Name  Hobby 
Susy  eat fish 
Anna  gardening
Billy bowling with friends

Note: name and hobby are separated by tab

What is the best way to read all the line and put it in arraylist(name,hobby). The tricky part is that

eat fish or bowling with friends

has white spaces and it must be put under one array and obviously I cannot hardcode it. Here is my current code:

 public void openFile(){
            try{
                FileInputStream fstream = new    FileInputStream("textfile.txt");
          // use DataInputStream to read binary NOT text
          BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
          ArrayList<String> names = new ArrayList<String>();
          ArrayList<String> hobbies = new ArrayList<String>();
          String lineJustFetched;
          while ((lineJustFetched = br.readLine()) != null)   {
          String[] tokens = lineJustFetched.split(" \t");

I got an error:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

I suspect counting the index is not very useful on a tab. Any idea?

解决方案

Alright, you need to do the recipe shown below:

  1. Create a BufferedReader
  2. Create an ArrayList<String>
  3. Start reading data into a String variable named lineJustFetched.
  4. Split the String by calling lineJustFetched.split("\t");
  5. Iterate over the String[] produced. Check if the token you want to enter into the ArrayList is not ""
  6. If not, add the word to the ArrayList

You specify that you need to split based on \t values so white spaces won't be an issue.

SSCCE

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

public class WordsInArray {
    public static void main(String[] args) {
        try{
            BufferedReader buf = new BufferedReader(new FileReader("/home/little/Downloads/test"));
            ArrayList<String> words = new ArrayList<>();
            String lineJustFetched = null;
            String[] wordsArray;

            while(true){
                lineJustFetched = buf.readLine();
                if(lineJustFetched == null){  
                    break; 
                }else{
                    wordsArray = lineJustFetched.split("\t");
                    for(String each : wordsArray){
                        if(!"".equals(each)){
                            words.add(each);
                        }
                    }
                }
            }

            for(String each : words){
                System.out.println(each);
            }

            buf.close();

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}    

Output

John
likes to play tennis
Sherlock
likes to solve crime

这篇关于读取由选项卡分隔的文件,并将这些单词放在ArrayList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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