2016-10-06 6 views
3

私はここで苦労しています。テキストファイルから各単語をArrayListに追加しようとしています。もう一度同じ単語をスキップします。 (意味がありますか?)テキストファイルをArrayListに入れても、単語が存在する場合はスキップします

私はどこから始めるべきか分かりません。私は、テキストファイルをArrayListに追加するループが1つ必要であり、単語がリストにないかどうかをチェックする必要があることを知っています。何か案は?

PS:私は正しい道の上だ場合だけこれは私がこれまで何をやったかであるJavaの

を始め、さえ知らない...

public String findWord(){ 
     int text = 0; 
     int i = 0; 
     while sc.hasNextLine()){ 
      wordArray[i] = sc.nextLine(); 
     } 
      if wordArray[i].contains() { 
     } 
     i++; 
    } 
+0

はあなたが試したことを共有します – sidgate

+0

アイテムが既にArrayListに存在するかどうかをチェックする 'contains'メソッドがあります。多分あなたはこれで試すことができますか? –

+0

また、HashSetもチェックしてください。重複を許さないので、すべての単語を追加することができます(重複を自動的に破棄します)。次に、HashSetをArrayListに追加できます。 –

答えて

4

ListArrayListまたはそれ以外)は、使用するのに最適なデータ構造ではありません。 Setが良いです。擬似コードで:

  • 遭遇した(最初の時間をどうしたいか何でも、セット戻りfalseに追加する場合は、各単語
    • に設定さ
    • を定義し、他の
    • それをスキップ)ワード

add()方法は、コールの結果セットが変更された場合はtrueを返します。セットがセットに含まれていない場合にのみ発生します。セットでは重複が許可されないためです。

+0

ありがとう、これはそれを解決した:) –

+0

@jonas np。これにはO(n)[時間の複雑さ](https://en.m.wikipedia.org/wiki/Time_complexity)もあります。できるだけ速いです。 – Bohemian

0

私は一度作っ同様のプログラムでは、テキストファイルを読み取って、単語が何回出てきたかを数えます。スキャナだけでなく、ファイルシステムをインポートすると Idを開始(これは、Javaクラスの最上部にあることが必要)

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.File; 
import java.io.PrintStream; 
import java.util.Scanner; 

、あなたは、ファイルだけでなく、このファイルからの読み取りスキャナーを作ることができ、それに応じてファイルへのパスを調整してください。新しいPrintstreamは必要ではありませんが、大量のデータを扱う場合は、コンソールのオーバーフローが気になりません。

あなたはこのような何か書きますので、次の単語を取得するために)(scanner.nextを使用することができ、この後
public static void main(String[] args) throws FileNotFoundException { 
    File file=new File("E:/Youtube analytics/input/input.txt"); 
    Scanner scanner = new Scanner(file); //will read from the file above 
    PrintStream out = new PrintStream(new FileOutputStream("E:/Youtube  analytics/output/output.txt")); 
    System.setOut(out);  
} 

:あなたは、このいずれかを理解しないか、進歩へのさらなるguidenceが必要な場合は

String[] array=new String[MaxAmountOfWords];//this will make an array 
int numberOfWords=0; 
String currentWord=""; 

while(scanner.hasNext()){ 
    currentWord=scanner.next(); 

    if(isNotInArray(currentWord)) 
    { 
     array[numberOfWords]=currentWord 
    } 
    numberOfWords++; 
} 

を、 お知らせ下さい。

0

あなたはこれを試すことができます。

public List<String> getAllWords(String filePath){  
     String line; 
     List<String> allWords = new ArrayList<String>(); 
     BufferedReader reader = new BufferedReader(new FileReader(new File(filePath))); 
     //read each line of the file 
     while((line = reader.readLine()) != null) { 
      //get each word in the line 
      for(String word: line.split("(\\w)+")) 
       //validate if the current word is not empty 
       if(!word.isEmpty()) 
        if(!allWords.contains(word)) 
         allWords.add(word); 
      } 
     } 
     return allWords; 
    } 
0

最善の解決策は、設定を使用することです。あなたはまだリストを使用する場合でも、ここに行く:

は、ファイルには、以下のデータを持っているとします

Hi how are you 
I am Hardi 
Who are you 

コードは次のようになります。

List<String> list = new ArrayList<>(); 

    // Get the file. 
    FileInputStream fis = new FileInputStream("C:/Users/hdinesh/Desktop/samples.txt"); 

    //Construct BufferedReader from InputStreamReader 
    BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 

    String line = null; 
    // Loop through each line in the file 
    while ((line = br.readLine()) != null) { 
     // Regex for finding just the words 
     String[] strArray = line.split("[ ]"); 
     for (int i = 0; i< strArray.length; i++) { 
      if (!list.contains(strArray[i])) { 
       list.add(strArray[i]); 
      } 
     } 
    } 

    br.close(); 

    System.out.println(list.toString()); 

テキストファイルが持つ文章を持っている場合特殊文字は、正規表現を書く必要があります。

関連する問題