久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

讀取由制表符分隔的文件并將單詞放入 ArrayList

Read a file separated by tab and put the words in an ArrayList(讀取由制表符分隔的文件并將單詞放入 ArrayList)
本文介紹了讀取由制表符分隔的文件并將單詞放入 ArrayList的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在做一個自學練習,以幫助我更多地了解 Java,但我被這個問題困住了.我有以下 txt 文件:

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

注意:姓名和愛好用制表符隔開

閱讀所有行并將其放入 arraylist(name,hobby) 的最佳方法是什么.棘手的部分是

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(" 	");

我遇到了一個錯誤:

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

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. 創建一個BufferedReader
  2. 創建一個ArrayList
  3. 開始將數據讀取到名為 lineJustFetchedString 變量中.
  4. 調用lineJustFetched.split(" ");
  5. 分割String
  6. 迭代生成的 String[].檢查你要進入ArrayList的token是否不是""
  7. 如果沒有,則將單詞添加到 ArrayList
  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(" ");
  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 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("	");
                    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();
        }
    }
}    

輸出

John
likes to play tennis
Sherlock
likes to solve crime

這篇關于讀取由制表符分隔的文件并將單詞放入 ArrayList的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 午夜精品一区二区三区免费视频 | 国产一区二区三区久久久久久久久 | 国产一区二区精品在线 | 午夜影院在线观看 | 亚洲一区精品在线 | 日本a视频 | 天堂视频免费 | 国产三级一区二区 | 蜜臀久久99精品久久久久野外 | 国产精品亚洲一区二区三区在线 | 日日夜夜精品视频 | 久久久久中文字幕 | 精品国产乱码久久久久久a丨 | 中文字幕亚洲一区二区三区 | 欧美xxxx性xxxxx高清 | 四虎影音 | 午夜视频在线观看一区二区 | 精品久久久久久久 | 蜜桃视频在线观看免费视频网站www | 欧美一级淫片免费视频黄 | 亚洲欧美中文日韩在线v日本 | 久久亚洲国产 | www.99re5.com | 亚洲高清视频在线观看 | 免费看片国产 | 国产精久久久久久久 | jav成人av免费播放 | 亚洲国产一 | 中文字幕一区二区三区四区五区 | 四虎影音 | 九九久久久 | 视频一二三区 | 狠狠躁18三区二区一区 | 日韩综合 | 久久99精品久久久久久国产越南 | 五月天国产视频 | 亚洲国产精品久久 | 午夜视频一区二区三区 | 国产丝袜一区二区三区免费视频 | 国产精品二区三区 | 91精品国产91久久久久久三级 |