2016-10-31 3 views
0

Ich versuche, Programm zu erstellen, das: 1. Zeichen aus Datei lesen kann 2. diese Zeichen zu ArrayList hinzufügen 3. Überprüfen Sie, ob in Zeile nur Zeichen a, b, c (keine andere/keine Leerzeichen)Java - Lesen von Zeichen aus Datei zu ArrayList

wenn 3 wahr ist - 1. vergleichen erstes & letztes Zeichen in Arraylist, wenn sie unterschiedlich sind print "OK"

Beispieldatei: abbcb - OK abbca - NICHT OK ein BBC - NICHT OK abdcb - NICHT OK bbbca - OK

Im Moment habe ich:

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 + "] "); 

     } 
    } 

} 

Ich habe Probleme mit dem Hinzufügen von Zeichen aufzulisten. Ich werde dankbar sein für Hilfe.

+0

Warum denken Sie, Sie 'char' auf eine Liste von' String' hinzufügen würde? – shmosel

+2

Ihre 'Liste' erwartet einen' String' nicht ein 'char'. Ändern Sie den Typparameter in 'Character', d. H.' List ', wenn Sie eine' List' von 'char' möchten –

Antwort

-1

Ich denke, das für Sie guten Start ist:

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