gpt4 book ai didi

java - 为什么它不读取?

转载 作者:行者123 更新时间:2023-12-01 15:54:11 25 4
gpt4 key购买 nike

我想问一下我的程序有什么问题..我的程序是关于堆栈的,没有错误,但我的问题是,我们的要求是在堆栈中进行所有操作,然后,即使您关闭程序,下次打开它时,仍应显示以前的数据..我使用了文件写入器和文件读取器,但是每次我关闭它,然后再次打开它,以前的数据都不会出现。有什么建议吗?谢谢..这是我的程序..

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

}
}

最佳答案

实际上这里存在很多问题。

就亚历山大的回答而言,您在每次推送/弹出调用后都会保存,所以 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"))打开实际文件。

新的 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++;
}

}

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

关于java - 为什么它不读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5413275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com