Skip to content

Commit 670dddd

Browse files
committed
feat(StackDemo1): add interactive stack input and save contents to file
What - Added StackDemo1 class. - Used Scanner to take user input. - Created a Stack<Integer>. - Asked user how many elements to push. - Iteratively read integers and pushed them onto the stack. - Printed the stack contents. - Implemented saveStackToFile() method: - Used BufferedWriter + FileWriter to write each stack element to a file, one per line. - Wrapped file writing in try-with-resources for safe closure. - Closed Scanner after input. Why - Demonstrates interactive usage of Stack with user input. - Shows how to persist stack contents into a file. - Combines collections (Stack) with I/O (BufferedWriter) for practical example. How - User prompted for n. - For loop reads n integers via sc.nextInt() and pushes into stack. - After input, printed stack. - saveStackToFile() iterates stack, writes each value with newline. - File saved to hardcoded path. Logic - Inputs: integer count n and stack elements. - Outputs: - Printed stack contents. - Saved stack elements to text file. - Flow: 1. Prompt for size. 2. Read elements, push to stack. 3. Print stack contents. 4. Write contents to file. - Edge cases: - Non-integer input throws InputMismatchException. - File path invalid or lacking permissions → IOException caught and error message shown. - Empty stack still creates empty file. - Complexity: - Stack push O(1) per element. - File write O(n). - Concurrency: Stack is synchronized but Scanner/FileWriter are not thread-safe here. Real-life applications - Capturing runtime stack input from users. - Persisting stack state for later retrieval or debugging. - Useful template for combining data structures with file I/O. Notes - Hardcoded file path may fail across systems; should use relative paths or configurable paths. - Prefer try-with-resources for Scanner as well to ensure closure. - In modern code, ArrayDeque often replaces Stack, but demo illustrates legacy class + file persistence nicely. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 861a4d6 commit 670dddd

File tree

1 file changed

+8
-10
lines changed
  • Section 25 Collections Frameworks/List Interface/Vector/Stack/src

1 file changed

+8
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
package ListDemo.SatckDemo;
2-
31
import java.io.BufferedWriter;
42
import java.io.FileWriter;
53
import java.io.IOException;
64
import java.util.Scanner;
75
import java.util.Stack;
86

9-
public class InputStacks {
7+
public class StackDemo1 {
108
public static void main(String[] args) {
119
Scanner sc = new Scanner(System.in);
10+
11+
// Input Stacks
1212
Stack<Integer> stack = new Stack<>();
13-
System.out.print("Enter the number of elements to push: ");
13+
System.out.print("Enter the number of elements you want to store inside the stack: ");
1414

1515
int n = sc.nextInt(); // Take the number of elements as input
1616

@@ -22,20 +22,18 @@ public static void main(String[] args) {
2222
System.out.println("\nStack contents: " + stack); // Print stack contents
2323

2424
// Save stack data to a file
25-
saveStackToFile(stack, "C://Users//somes//Downloads//JAVA SE\\Section25CollectionFramework\\src\\ListDemo\\SatckDemo\\stack_data.txt");
25+
saveStackToFile(stack, "/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section 25 Collections Frameworks/List Interface/Vector/Stack/src/Output.txt");
2626
sc.close();
2727
}
2828

2929
public static void saveStackToFile(Stack<Integer> stack, String filename) {
30-
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename)))
31-
{
32-
for (int item : stack)
33-
{
30+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
31+
for (int item : stack) {
3432
writer.write(item + "\n"); // Write each stack element on a new line
3533
}
3634
System.out.println("Stack contents saved to " + filename);
3735
} catch (IOException e) {
3836
System.out.println("Error writing to file: " + e.getMessage());
3937
}
4038
}
41-
}
39+
}

0 commit comments

Comments
 (0)