2013-04-27 139 views
-1

我試圖編寫一個程序的代碼,該程序將txt文件的內容保存到單個鏈接列表中,txt文件中的每一行代表一個對象。我用掃描儀從文件中讀取,但我不知道如何獲得列表中的單詞。如何從文件中讀取並將內容保存到鏈接列表中?

任何尖端或暗示是高度讚賞。

我嘗試:

類項目

public class Item implements Serializable { 
private String name; 
private double price; 

public Item() { 
name="" ; 
price=0.00 ; 
} 

public Item(String n, double p) { 
name = n ; 
price = p ; 
} 

public void setName(String n) { 
name = n ; 
} 

public void setPrice(double p) { price = p ; 
} 

public String getName() { 
return name ; 
} 

public double getPrice() { 
return price ; 
} 
} 

類節點

public class Node { 
public Item data ; 
public Node next ; 

public Node(Item d) { 
    data = d ; 
} 

public Node(Item d, Node n) { 
    data = d ; 
    next = n ; 
} 

public String toString() { 
    return data+""; 
} 
} 

類listItems中

public class ListItems { 

private Node first; 

public ListItems(){ 
    first=null; 
} 

public boolean isEmpty(){ 
    return first==null; 
} 

public void addAnItems(Item d, int i){ 
    Node node1=new Node(d); 
    node1.next=first; 
    first=items.get(i); 
} 

public void displayList(){ 
    Node current = first; 

     while(current != null){ 
     System.out.println(current.toString()); 
     current=current.next; 
     } 
} 
} 

類FileItems

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

public class FileItems { 
public static void main (String[]args){ 
Scanner input1=new Scanner(System.in) ; 
     try { 
     Scanner input2=new Scanner(new File("items.txt")) ; 
     } 

     catch (FileNotFoundException e) { 
     System.out.println("ERROR OPENING FILE") ; 
     System.Exit(1) ; 
     } 

     while (input2.hasNext()){ 
     String name1=input2.next() ; 
     double price1=input2.nextDouble() ; 
     } 

      ListItems items=new ListItems(); 
       System.out.println("CHOOSE ONE OF THE FOLLOWING OPTIONS :") ; 
       System.out.println("ENTER 1 TO ADD AN ITEM TO THE LIST") ; 
       System.out.println("ENTER 2 TO DELETE AN ITEM FROM THE LIST") ; 
       System.out.println("ENTER 3 TO DISPLAY ALL THE ITEMS IN THE LIST") ; 
       System.out.println("ENTER 4 TO CLOSE THE PROGRAM") ; 

       int in=input1.nextInt() ; 

         switch (in) { 
         case 1 : case1() ; 
          break ; 
         case 2 : case1() ; 
          break ; 
         case 3 : displayList() ; 
          break ; 
         case 4 : System.Exit(1) ; 
          break ; 
         } 

         public void case1() { 
          System.out.println("ENTER THE NAME OF THE ITEM, THE PRICE AND THE INDEX "); 

          try { 
          System.out.println("NAME: ") ; 
           String n2=input1.next() ; 
          System.out.println("PRICE: ") ; 
           double p2=input1.nextDouble() ; 
          System.out.println("INDEX: ") ; 
           int index=input1.nextInt() ; 
          } 

          catch (InputMismatchException e) { 
          System.out.print("INVALID INPUT") ; 
          } 

         Item I=new Item(n2, p2) ; 
         addAnItem(I, index) ; 
         } 

         public static case2() { 
          System.out.println("ENTER THE INDEX OF THE ITEM TO DELETE IT ") ; 

          try { 
          int index=input1.nextInt() ; 
          } 

          catch (InputMismatchException e) { 
          System.out.print("INVALID INPUT") ; 
          } 
         items.remove(index) ; 
         } 
     } 
     } 
+0

什麼文件包含?只有文本字符串或序列化對象。 – Mavrick 2013-04-27 02:27:51

+0

唯一的字符串,但我想它在列表保存爲項目對象 – user2252700 2013-04-27 02:35:08

+0

http://www.daniweb.com/software-development/java/threads/147590/how-to-insert-data-read-from- text-file-into-linked-list – Mavrick 2013-04-27 02:44:03

回答

2

打開文本文件並將每行讀取爲一個字符串,並將該String對象放置到LinkedList中。按照相反的順序打印LinkedList中的所有行。

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

非常感謝您的幫助,但我希望用戶在從txt文件中讀取字符串後選擇以下選項之一:1-將項目添加到列表中2-刪除項目從列表中3-顯示所有項目4-關閉程序。我怎樣才能做到這一點 ? – user2252700 2013-04-27 04:15:20

+0

如果我的回答很有幫助,那麼請注意。 – Mavrick 2013-04-27 08:10:42

+0

爲了給用戶,你需要按照下面的菜單式選擇選項: – Mavrick 2013-04-27 08:12:06

相關問題