2011-03-24 46 views
0

我想問一下我的程序有什麼問題.. 我的程序是關於堆棧的,它沒有錯誤,但是我的問題是,我們的要求是在堆棧中執行所有操作,然後即使關閉程序,下次打開它時,以前的數據仍然應該顯示。 我使用了filewriter和filereader,但是每次關閉它,然後再打開它,以前的數據就不會出現。 有什麼建議嗎? 感謝..這裏是我的節目..爲什麼不讀它?

import java.util.*; 
import java.awt.*; 
import java.io.*; 
import javax.swing.*; 
import java.awt.event.*; 
public class STACKS extends JFrame implements ActionListener 
{ 
    private JButton push,pop,peek,display,exit; 
    private JLabel Stack; 
    static int arr[],x=0,b=0,xy=0,c=0; 
    static String in="",INDEX="",d="",s=""; 
    static String id[]={}; 

    public STACKS() 
    { 
     super("STACKS MENU!^_^"); 
     Container c=getContentPane(); 
     c.setLayout(new FlowLayout()); 
     Stack=new JLabel("STACKS MENU!^_^"); c.add(Stack); 
     push=new JButton("Push!^-^"); 
     pop=new JButton("Pop!^-^"); 
     peek=new JButton("Peek!^-^"); 
     display=new JButton("Display!^-^"); 
     exit=new JButton("Exit!^-^"); 
     push.addActionListener(this);   c.add(push); 
     pop.addActionListener(this);   c.add(pop); 
     peek.addActionListener(this);   c.add(peek); 
     display.addActionListener(this);  c.add(display); 
     exit.addActionListener(this);   c.add(exit); 
     setVisible(true); 
     setSize(150,250); 
    } 
    public static void saveMe() throws IOException 
    { 
     File data1=new File("Sample.txt");  //a file was created... 
     PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(data1,true))); 
     for(int x=0;x<4;x++) 
     { 
      out.write(":"+arr[x]); xy=1;   //here in this section, I put : in every elements inside the array.. 
     } 
     out.close(); 

    } 
    public static void readMe() throws IOException 
    { 
     Scanner txtFile=(new Scanner("Sample.txt")); 

     for(int y=0;x<id.length;y++) 
     { 
      s=txtFile.nextLine(); 
      id=s.split(":"); 
      arr[y]=Integer.parseInt(id[y]); 
     } 

    } 
    public void actionPerformed(ActionEvent a) 
    { 
     try 
     { 
      readMe();        //here is where the previous data will be read.. 
     } 
     catch(Exception e) 
     { 
      JOptionPane.showMessageDialog(null,"File not found! readme !^,^"); 
     } 
     if (xy==0) 
     { 
      INDEX=JOptionPane.showInputDialog(null,"Enter LENGTH of the array!"); 
      c=Integer.parseInt(INDEX); xy=1; 
      arr=new int[c]; 
     } 
     if(a.getSource()==push) 
     { 
      in=JOptionPane.showInputDialog(null,"Enter integer to be pushed!"); 
      b=Integer.parseInt(in); 
      arr[x]=b; x+=1; 
      if(x==c) 
      { 
       JOptionPane.showMessageDialog(null,"WARNING! The stacks are full, please pop something!^-^"); 
      } 
      if(x>c) 
      { 
       JOptionPane.showMessageDialog(null,"Sorry, the stacks are full,please pop something first!^-^"); 
       x-=1; 
      } 
     } 
     else if(a.getSource()==pop) 
     { 

      arr[x-1]=0;x-=1; 
      JOptionPane.showMessageDialog(null,"The value has been popped!^-^"); 
      if(x==0) 
      { 
       JOptionPane.showMessageDialog(null,"The stacks are empty, push something!^-^"); 
      } 

     } 
     else if(a.getSource()==peek) 
     { 
      JOptionPane.showMessageDialog(null,"The value is "+arr[x-1]+"! ^-^"); 
     } 
     else if(a.getSource()==display) 
     { 
      for(int y=c-1;y>-1;y--) 
      { 
       d+="*** "+arr[y]+" ***\n"; 
      } 
      JOptionPane.showMessageDialog(null,"The value inside the stacks are:\n"+d); 
      d=""; 
     } 
     else if(a.getSource()==exit) 
     { 
      System.exit(0); 
     } 
      try 
     { 
      saveMe();     //here is where the file will be saved.. 
     } 
     catch(Exception e) 
     { 
      JOptionPane.showMessageDialog(null,"File not found!^,^"); 
     } 
    } 
    public static void main(String args[]) 
    { 
     STACKS pot=new STACKS(); 
     pot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 
} 
+1

'還有就是在it'沒有錯誤是一種相當強烈的聲明:) – 2011-03-24 00:49:28

回答

0

因爲你叫saveMe()

必須調用FileWriter.close你終止程序,這樣的緩衝輸出將被刷新到磁盤之前之前調用System.exit

+0

我不要認爲這是問題System.exit()只在動作事件源來自退出按鈕時被調用 – 2011-03-24 00:41:00

1

實際上這裏有很多問題。

就亞歷山大的回答而言,在每次按下/彈出呼叫後都會保存,所以saveMe()正在被調用。

saveMe()中,雖然在堆棧的第一個元素之前寫了:,所以讀回來總會給出一個額外的空白元素。在編寫:之前檢查x!= 0。

而且在readMe(),您呼叫nextLine()爲id.length,這兩種方式不正確:

  1. 當您啓動該程序,ID的長度是零。您不應該使用x<id.length作爲for循環的結束條件(x無論如何都是錯誤的,它應該在您的上下文中爲y)。使用txtFile.hasNext()來檢查是否有更多堆棧元素可供讀取。
  2. 您的文件完全在一行中包含堆棧,而不是多個。你應該做的是將掃描儀的分隔符設置爲:,並使用textFile.useDelimiter(":"),然後調用next()。另外請記住,除非您正確saveMe(),否則在開始時會有一個額外的元素爲空。

也在readMe(),根據Nicklamort的回答,您需要撥打new Scanner(new File("Sample.txt"))打開實際文件。

Scanner txtFile=(new Scanner("Sample.txt")); 

我認爲你試圖解析:新saveMe()

public static void saveMe() throws IOException 
{ 
    File data1=new File("Sample.txt");  //a file was created... 
    PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(data1,false))); // don't append, overwrite existing data since we are saving the entire stack each time 
    for(int x=0;x<arr.length;x++) 
    { 
     if(x != 0) { out.write(":"); }   //here in this section, I put : in every elements inside the array.. 
     out.write(arr[x]); xy=1; 
    } 
    out.flush(); // force writing to disk 
    out.close(); 
} 
+0

爲你的答案+1了:P – darvids0n 2011-03-24 01:00:18

1

當您嘗試從文件中讀取一個新readMe()

public static void readMe() throws IOException 
{ 
    Scanner txtFile=(new Scanner(new File("Sample.txt"))); 
    int y = 0; 
    txtFile.useDelimiter(":"); 
    while(txtFile.hasNext()) 
    { 
     arr[y]=txtFile.nextInt(); 
     y++; 
    } 

} 

例文字字符串:「Sample。TXT」

你要實際的文件對象傳遞給掃描儀的構造:

Scanner txtFile = new Scanner(File source); 

退房的docs