【问题标题】:Java - Read characters from file to ArrayListJava - 从文件中读取字符到 ArrayList
【发布时间】:2016-10-31 22:23:14
【问题描述】:

我尝试创建可以: 1.从文件中读取字符 2.将这些字符添加到ArrayList 3. 检查是否只有字符 a,b,c (没有其他/没有空格)

如果 3 为真 - 1.比较ArrayList中的第一个和最后一个字符,如果它们不同打印“OK”

示例文件: abbcb - 好的 abbca - 不好 一个英国广播公司 - 不好 abdcb - 不好 bbbca - 好的

此刻我得到:

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Projekt3 
{
    public static void main(String[] args) throws IOException 
    {
        List<String> Lista = new ArrayList<String>();
        Scanner sc = new Scanner(System.in).useDelimiter("\\s*");
        while (!sc.hasNext("z")) 
        {
            char ch = sc.next().charAt(0);
            Lista.add(ch);

            //System.out.print("[" + ch + "] ");

        }
    }

}

我在向列表中添加字符时遇到问题。我将不胜感激。

【问题讨论】:

  • 为什么您认为您可以将char 添加到String 列表中?
  • 您的List 需要String 而不是char。将类型参数更改为Character,即List&lt;Character&gt;,如果你想要char中的List

标签: java arraylist character


【解决方案1】:
import java.io.*;
import java.util.ArrayList;

public class Project3 {

public static void main(String[] args) throws FileNotFoundException, IOException {

    BufferedReader reader = new BufferedReader(new FileReader("//home//azeez//Documents//sample")); //replace with your file path
    ArrayList<String> wordList = new ArrayList<>();
    String line = null;
    while ((line = reader.readLine()) != null) {
        wordList.add(line);
    }

    for (String word : wordList) {
        if (word.matches("^[abc]+$")) {
            if (word.charAt(0) == word.charAt(word.length() - 1)) {
                System.out.print(word + "-NOT OK" + " ");
            } else {
                System.out.print(word + "-OK" + " ");
               }
           }
       }
   }
}

【讨论】:

    【解决方案2】:

    我认为这对你来说是一个好的开始:

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Project3 {
    public static void main(String[] args) {
        String path = "/Users/David/sandbox/java/test.txt";
    
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)))) {
    
            String currentLine = null;
    
            // Array list for your words
            List<String> arrayList = new ArrayList<>();
    
            while ((currentLine = br.readLine()) != null) {
                // only a, b and c
                if (currentLine.contains("a") && currentLine.contains("b") && currentLine.contains("c")) {
                    // start character equal end character
                    if (currentLine.substring(0, 1)
                            .equals(currentLine.substring(currentLine.length()-1, currentLine.length()))) {
                        arrayList.add(currentLine);
                        System.out.println(currentLine);
                    }
                }
            }
        } catch (Throwable e) {
            System.err.println("error on read file " + e.getMessage());
            e.printStackTrace();
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 2016-04-14
      • 2013-05-10
      • 1970-01-01
      相关资源
      最近更新 更多