【问题标题】:How to get words from a text file to a singly linked list如何从文本文件中获取单词到单链表
【发布时间】:2016-02-28 20:02:45
【问题描述】:

这个赋值的目的是将文件中的单词列表输出到单链表中,然后按字母顺序对它们进行排序。但是我无法弄清楚如何将单个单词放入链接列表中。我对此很陌生,任何提示或提示将不胜感激。

这是我认为是正确的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;
   }
}

我知道如何在单链表中插入元素,但是尝试将单词放入列表中对我来说很困难。任何事情都会对我很有帮助。

【问题讨论】:

    标签: java linked-list text-files


    【解决方案1】:

    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();
    

    循环到文件末尾,缓存当前单词,并插入到当前Node中。然后移动到链接中的下一个节点,并重复。

    完成后,您必须对节点进行某种排序和交换。

    【讨论】:

    • 对不起,我忘了说我需要输入一个由句子组成的文件,我不能使用扫描仪
    【解决方案2】:

    试试这个,希望对你有用...

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

    【讨论】:

      猜你喜欢
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      相关资源
      最近更新 更多