读取由选项卡分隔的文件,并将这些单词放在ArrayList中 [英] Read a file separated by tab and put the words in an ArrayList
问题描述
$ 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
该索引在选项卡上并不是非常有用。
有什么想法吗?
好的,你需要做下面的配方:
- 创建一个
BufferedReader
开始读取数据到 - 通过调用
lineJustFetched.split(\t); $ c>来拆分
String
- 迭代产生的
String []
。检查你想进入ArrayList
的令牌是不是
- 如果不是,添加单词到
ArrayList
- 迭代产生的
字符串
变量名称为 lineJustFetched
。 你需要基于 \ 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:
- Create a
BufferedReader
- Create an
ArrayList<String>
- Start reading data into a
String
variable namedlineJustFetched
. - Split the
String
by callinglineJustFetched.split("\t");
- Iterate over the
String[]
produced. Check if the token you want to enter into theArrayList
is not""
- 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屋!