2012-03-13 23 views
0

この割り当ての目的は、ファイルから単一リンクリストに単語のリストを出力し、アルファベット順に並べ替えることです。しかし、私はリンクされたリストに個々の単語を取得する方法を把握することはできません。私はこれで非常に新しいと任意のヒントやヒントは非常に感謝される。ここでテキストファイルから単一リンクリストへの単語の取得方法

は私が正しいと信じて私のNodeクラスである:ここでは

//Node of a singly linked list of strings 
public class Node { 
    private String element; 
    private Node next; 

    //creates a node with the given element and next 

    public Node(String s, Node n){ 
     element = s; 
     next = n; 
    } 

    //Returns the elements of this 
    public String getElement(){ 
     return element; 
    } 
    public Node getNext(){ 
     return next; 
    } 

    //Modifier 
    //Sets the element of this 
    public void setElement(String newElement){ 
     element = newElement; 
    } 

    //Sets the next node of this 
    public void setNext(Node newNext){ 
     next = newNext; 
    } 
} 

ファイルから文章を取り、個々の単語にそれを破壊する私のメインクラスです。それはだった私は、リンクされたリストに、個人の単語を取得する方法を見つけ出すことができないという問題があるさ:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.*; 

public class DictionaryTester{ 
    public static void main(String[] args) { 
    try { 
     BufferedReader br = new BufferedReader(new FileReader("input1")); 
     String file; 
     int lineNum = 1; 
     while ((file = br.readLine()) != null) { 
     System.out.print("(" + lineNum++ + ") "); 
     System.out.println(file.toLowerCase()); 

     String line = br.readLine(); 
     //String is split or removes the spaces and places into the array words 
     String[] words = line.split(" "); 

     //for loop to keep running on the length of the array 
     for(int i =0; i< words.length; i++){ 
      //word is equal to a particular indexed spot of the word array and gets rid of all non-alphabet letters 
      String word = words[i]; 
      word = word.replaceAll("[^a-z]", ""); 
     } 
     } 
    } 
    catch (IOException e){ 
     System.out.println("Error: " + e.getMessage()); 
    } 
    } 
} 

は、私が持っている他のクラスは、リストに単語を保持することになるSLinkedListですが、私は私が言ったようにリストに個々の単語を取得する方法を見つけ出すことはできません。

//Singly linked list 
public class SLinkedList { 
    //head node of the list 
    protected Node head; 

    //number of nodes in the list 
    protected long size; 

    //Default constructor that creates an empty list 
    public SLinkedList(){ 
     head = null; 
     size = 0; 
    } 
} 

私は単独でリンクされたリストの要素を挿入する方法を知っているが、リストに単語を取得しようとすると、私のために困難であることが証明されました。何かが私にとって非常に役立つだろう。

答えて

0

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

スキャナはまた、空白文字以外の区切り文字を使用することができます。この の例では、文字列からのいくつかの項目を読み取ります

String input = "1 fish 2 fish red fish blue fish"; 
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*"); 
System.out.println(s.nextInt()); 
System.out.println(s.nextInt()); 
System.out.println(s.next()); 
System.out.println(s.next()); 
s.close(); 

ループファイルの終わりまで、現在の単語をキャッシュし、現在のノードに挿入します。その後、リンク内の次のノードに移動し、繰り返します。

終了したら、ソートやノードの交換をしなければなりません。

+0

...これはあなたのために役に立つことを願って、これを試してみてください – user1267401

0

私は入力に文章からなるファイルが必要であることを言及するのを忘れて申し訳ありませんが、私はスキャナを使用することはできません

import java.util.*; 
import java.io.*; 

public class FileLinkList 
{ 
public static void main(String args[])throws IOException{ 
String content = new String(); 
int count=1; 
File file = new File("abc.txt"); 
LinkedList<String> list = new LinkedList<String>(); 

try { 
Scanner sc = new Scanner(new FileInputStream(file)); 
while (sc.hasNextLine()){ 
content = sc.nextLine(); 
list.add(content); 
} 
sc.close(); 
}catch(FileNotFoundException fnf){ 
fnf.printStackTrace(); 
} 
catch (Exception e) { 
e.printStackTrace(); 
System.out.println("\nProgram terminated Safely..."); 
} 

Collections.reverse(list); 
Iterator i = list.iterator(); 
while (i.hasNext()) { 
System.out.print("Node " + (count++) + " : "); 
System.out.println(i.next()); 
} 
} 
} 
関連する問題