-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataHandlerStack.java
More file actions
48 lines (34 loc) · 1.3 KB
/
DataHandlerStack.java
File metadata and controls
48 lines (34 loc) · 1.3 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
// 2
// The purpose of this program is to demonstrate the use of a stack in handling text files.
import java.io.FileNotFoundException;
import java.util.*;
import java.io.File;
public class DataHandlerStack {
public String DataHandler (String fileName) throws FileNotFoundException {
String originalData = "";
String reversedData = "";
Stack<String> stack = new Stack<String>();
File myFile = new File(fileName);
Scanner myScanner = new Scanner(myFile);
while (myScanner.hasNextLine()){
String line = myScanner.nextLine();
stack.push(line);
originalData += line + "\n";
}
myScanner.close();
while (!stack.isEmpty()){
reversedData += stack.pop() + "\n";
}
return "Original data: \n" + originalData + "Reversed data: " + reversedData;
}
public static void main(String[] args) {
DataHandlerStack dh = new DataHandlerStack();
try{
String data = dh.DataHandler("text.txt");
System.out.println(data);
}
catch(FileNotFoundException e){
System.out.println("An error occured, file was not found");
}
}
}