Java 为什么赢了';它不明白吗?

Java 为什么赢了';它不明白吗?,java,Java,我想问我的程序有什么问题。。 我的程序是关于堆栈的,没有错误,但我的问题是,我们的要求是在堆栈中执行所有操作,然后,即使您关闭程序,下次打开它时,仍应显示以前的数据。。 我使用了filewriter和filereader,但每次我关闭它,然后再次打开它时,以前的数据都不会出现。。 有什么建议吗? 谢谢这是我的节目 import java.util.*; import java.awt.*; import java.io.*; import javax.swing.*; import java.a

我想问我的程序有什么问题。。 我的程序是关于堆栈的,没有错误,但我的问题是,我们的要求是在堆栈中执行所有操作,然后,即使您关闭程序,下次打开它时,仍应显示以前的数据。。 我使用了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);

    }
}
import java.util.*;
导入java.awt.*;
导入java.io.*;
导入javax.swing.*;
导入java.awt.event.*;
公共类堆栈扩展JFrame实现ActionListener
{
专用按钮推送、弹出、查看、显示、退出;
专用JLabel栈;
静态整数arr[],x=0,b=0,xy=0,c=0;
静态字符串in=“”,INDEX=“”,d=“”,s=“”;
静态字符串id[]={};
公共堆栈()
{
超级(“堆叠菜单!”;
容器c=getContentPane();
c、 setLayout(新的FlowLayout());
Stack=newjlabel(“STACKS菜单!^^”);c.add(Stack);
push=新的JButton(“push!^-^”);
pop=新按钮(“pop!^-^”);
peek=新的JButton(“peek!^-^”);
display=新的JButton(“display!^-^”);
退出=新的JButton(“退出!^-^”);
push.addActionListener(this);c.add(push);
addActionListener(this);c.add(pop);
peek.addActionListener(this);c.add(peek);
display.addActionListener(this);c.add(display);
exit.addActionListener(this);c.add(exit);
setVisible(真);
设置大小(150250);
}
public static void saveMe()引发IOException
{
File data1=新文件(“Sample.txt”);//创建了一个文件。。。
PrintWriter out=新的PrintWriter(新的BufferedWriter(新的FileWriter(data1,true));
对于(int x=0;x-1;y--)
{
d+=“***”+arr[y]+“***\n”;
}
showMessageDialog(null,“堆栈中的值为:\n”+d);
d=“”;
}
else if(a.getSource()==退出)
{
系统出口(0);
}
尝试
{
saveMe();//文件将保存在此处。。
}
捕获(例外e)
{
showMessageDialog(null,“找不到文件!^,^”);
}
}
公共静态void main(字符串参数[])
{
STACKS pot=新堆栈();
pot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

因为您调用了
系统。在调用
saveMe()之前退出


您必须调用FileWriter。在终止程序之前关闭
,这样缓冲区输出将刷新到磁盘。

实际上这里有很多问题

根据Alexander的回答,每次push/pop调用后都会进行保存,因此会调用
saveMe()

但是,在
saveMe()
中,您在堆栈的第一个元素之前写入
,因此读回它将始终提供一个额外的空白元素。检查x!=在编写
之前,请执行0

readMe()
中,您正在为id.length调用
nextLine()
,这在两方面是不正确的:


  • 启动程序时,id的长度为零。尝试从文件读取时,不应使用
    x:

    Scanner txtFile=(new Scanner("Sample.txt"));
    
    我认为您正在尝试解析文本字符串:“Sample.txt”

    要将实际文件对象传递给扫描仪构造函数:

    Scanner txtFile = new Scanner(File source);
    

    看看

    我认为这不是问题所在。System.exit()仅在操作事件源来自exit按钮时才被调用。
    其中没有错误
    是一个相当强的语句:)
    Scanner txtFile=(new Scanner("Sample.txt"));
    
    Scanner txtFile = new Scanner(File source);