this program demonstrates the operations of the "stack" data structure using JOptionPane, linked list, FileWriter and FileReader. basically, when you enter an element, it should not duplicate. but in my case, it ALWAYS does. must be because of my FileReader.. when it re-read the .txt file, it also re-passes the elements of the stack to the linked list, thus making a duplicate for it. as much as i want to prevent its duplication, i don't know what to do...

btw, here's the code for my program:

import javax.swing.*;
import java.util.*;
import java.io.*;
 
class stackData extends JFrame
{
	String strSt = "";
	static LinkedList<String> lStack = new LinkedList<String>();
 
	static void readStack(LinkedList<String> ll) throws Exception //method for reading and re-enter of elements in stack
	{
		FileReader sr = new FileReader("stack.txt");
		Scanner stackLine = new Scanner(sr);
		String str;
		while (stackLine.hasNextLine())
		{
				str = stackLine.nextLine();
				ll.add(str);
				sr.close();
		} 
	}
 
	static void writeStack(LinkedList<String> l) throws Exception //method for writing
	{
		FileWriter stackW = new FileWriter("stack.txt");
		for (int w = 0;w < l.size();w++)
		{
			stackW.write(l.get(w)+"\r\n");
		}
		stackW.close();
	}
 
	static void displayS(LinkedList<String> e) throws Exception //display stack
	{
		readStack(e);
		if (e.isEmpty())
		{
			JOptionPane.showMessageDialog(null,"The stack is empty.");
			stackM();
		}
 
		else
		{
			String dispS = "";
			for (int r = (e.size()-1);r >= 0;r--)
			{
				dispS += e.get(r) + "\n";
			}
 
			JOptionPane.showMessageDialog(null,dispS);
			stackM();
		}
	}
 
	static void stackM() throws Exception //stack menu
	{
		String sM = JOptionPane.showInputDialog(null, "Stack Operations\n\n[1] Push\n[2] Pop\n[3] Peek\n[4] Display\n");
 
		if (sM.equals("1"))
			push(lStack);
 
		else if (sM.equals("2"))
			pop(lStack);
 
		else if (sM.equals("3"))
			peek(lStack);
 
		else if (sM.equals("4"))
			displayS(lStack);
	}
 
	static void push(LinkedList<String> sPu) throws Exception
	{
		String sData = JOptionPane.showInputDialog(null,"Enter any value(String, integer, etc.):");
		sPu.add(sData);
		writeStack(sPu);
		displayS(sPu);
	}
 
	static void pop(LinkedList<String> sPo) throws Exception
	{
	//	readStack(sPo);
		if (sPo.isEmpty())
		{
			JOptionPane.showMessageDialog(null, "The stack is empty.");
			stackM();
		}
 
		else
		{
			sPo.removeLast();
			String stackPopContainer = "";
 
			for (int sPop = 0;sPop < sPo.size();sPop++)
			{
				stackPopContainer += sPo.get(sPop) + " ";
			}
 
			writeStack(sPo);
			displayS(sPo);
		}
	}
 
	static void peek(LinkedList<String> sPe) throws Exception
	{
	//	readStack(sPe);
		if (sPe.isEmpty())
		{
			JOptionPane.showMessageDialog(null, "The stack is empty.");
			stackM();
		}
 
		else
		{
			sPe.getLast();
			String stackPeekContainer = "";
			for (int sPeek = 0;sPeek < sPe.size();sPeek++)
			{
				stackPeekContainer += sPe.get(sPeek) + ":";
			}
			writeStack(sPe);
			displayS(sPe);
		}
	}
 
	public static void main (String[] args) throws Exception
	{
		stackM();
    }
}


if there's any other errors aside from what i've indicated above, please correct it... thanks!!