forked from ahmarajeel/Stack-Array-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMohammedA_Project1_Java.java
More file actions
78 lines (58 loc) · 1.24 KB
/
Copy pathMohammedA_Project1_Java.java
File metadata and controls
78 lines (58 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.io.*;
import java.util.Scanner;
public class Stack
{
int[] ary;
int top;
//constructor
Stack(int n)
{
ary = new int[n];
top = -1;
}
int pop()
{
return ary[top--];
}
void push(int n)
{
top++;
ary[top] = n;
}
boolean isEmpty()
{
if(top < 0)
return true;
else
return false;
}
public static void main(String[] argv) throws FileNotFoundException
{
int intItem, counter = 0;
Scanner inFile = new Scanner(new FileReader(argv[0]));
System.out.println("Reading integers from the file: \n\n");
while(inFile.hasNext())
{
intItem = inFile.nextInt();
System.out.println(intItem);
counter++;
}
inFile.close();
Scanner inFile_1 = new Scanner(new FileReader(argv[0]));
Stack myStack = new Stack(counter);
int intItem_1;
System.out.println("\n\nPushing integers to the Stack: \n\n");
while (inFile_1.hasNext())
{
intItem_1 = inFile_1.nextInt();
myStack.push(intItem_1);
System.out.println(intItem_1);
}
System.out.println("\n\nPopping integers out from the Stack: \n\n");
while (!myStack.isEmpty())
{
System.out.println(myStack.pop());
}
inFile_1.close();
}
}