2012-03-13 80 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 2012-03-13 23:48:32

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()); 
} 
} 
} 
相關問題