내 프로그램에 어떤 문제가 있는지 물어보고 싶습니다. 내 프로그램은 스택에 관한 것이지만 오류는 없습니다.하지만 제 문제는 스택의 모든 작업을 처리해야한다는 것입니다. 프로그램을 닫더라도, 다음 번에 열면 이전 데이터가 계속 표시됩니다. 필자와 파일 판독기를 사용했지만 닫을 때마다 다시 열면 이전 데이터가 나타나지 않습니다. 조언이 있으십니까? 당신이 프로그램 버퍼 출력은 디스크로 플러시됩니다 그런 식으로 종료하기 전에 FileWriter.close
를 호출해야합니다 saveMe()
왜 읽지 않을까요?
를 호출하기 전에 System.exit
를 호출하기 때문에 덕분에 .. 여기
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);
}
}
'it'에 오류가 오히려 강한 문이 없다 : –