Skip to content

Commit ab37184

Browse files
authored
Close Scanner to avoid resource leak (TheAlgorithms#5077)
1 parent 47a9b1b commit ab37184

File tree

6 files changed

+243
-225
lines changed

6 files changed

+243
-225
lines changed

src/main/java/com/thealgorithms/ciphers/ProductCipher.java

+54-53
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,68 @@
55
class ProductCipher {
66

77
public static void main(String[] args) {
8-
Scanner sc = new Scanner(System.in);
9-
System.out.println("Enter the input to be encrypted: ");
10-
String substitutionInput = sc.nextLine();
11-
System.out.println(" ");
12-
System.out.println("Enter a number: ");
13-
int n = sc.nextInt();
8+
try (Scanner sc = new Scanner(System.in)) {
9+
System.out.println("Enter the input to be encrypted: ");
10+
String substitutionInput = sc.nextLine();
11+
System.out.println(" ");
12+
System.out.println("Enter a number: ");
13+
int n = sc.nextInt();
1414

15-
// Substitution encryption
16-
StringBuffer substitutionOutput = new StringBuffer();
17-
for (int i = 0; i < substitutionInput.length(); i++) {
18-
char c = substitutionInput.charAt(i);
19-
substitutionOutput.append((char) (c + 5));
20-
}
21-
System.out.println(" ");
22-
System.out.println("Substituted text: ");
23-
System.out.println(substitutionOutput);
15+
// Substitution encryption
16+
StringBuffer substitutionOutput = new StringBuffer();
17+
for (int i = 0; i < substitutionInput.length(); i++) {
18+
char c = substitutionInput.charAt(i);
19+
substitutionOutput.append((char) (c + 5));
20+
}
21+
System.out.println(" ");
22+
System.out.println("Substituted text: ");
23+
System.out.println(substitutionOutput);
2424

25-
// Transposition encryption
26-
String transpositionInput = substitutionOutput.toString();
27-
int modulus;
28-
if ((modulus = transpositionInput.length() % n) != 0) {
29-
modulus = n - modulus;
25+
// Transposition encryption
26+
String transpositionInput = substitutionOutput.toString();
27+
int modulus;
28+
if ((modulus = transpositionInput.length() % n) != 0) {
29+
modulus = n - modulus;
3030

31-
for (; modulus != 0; modulus--) {
32-
transpositionInput += "/";
31+
for (; modulus != 0; modulus--) {
32+
transpositionInput += "/";
33+
}
3334
}
34-
}
35-
StringBuffer transpositionOutput = new StringBuffer();
36-
System.out.println(" ");
37-
System.out.println("Transposition Matrix: ");
38-
for (int i = 0; i < n; i++) {
39-
for (int j = 0; j < transpositionInput.length() / n; j++) {
40-
char c = transpositionInput.charAt(i + (j * n));
41-
System.out.print(c);
42-
transpositionOutput.append(c);
35+
StringBuffer transpositionOutput = new StringBuffer();
36+
System.out.println(" ");
37+
System.out.println("Transposition Matrix: ");
38+
for (int i = 0; i < n; i++) {
39+
for (int j = 0; j < transpositionInput.length() / n; j++) {
40+
char c = transpositionInput.charAt(i + (j * n));
41+
System.out.print(c);
42+
transpositionOutput.append(c);
43+
}
44+
System.out.println();
4345
}
44-
System.out.println();
45-
}
46-
System.out.println(" ");
47-
System.out.println("Final encrypted text: ");
48-
System.out.println(transpositionOutput);
46+
System.out.println(" ");
47+
System.out.println("Final encrypted text: ");
48+
System.out.println(transpositionOutput);
4949

50-
// Transposition decryption
51-
n = transpositionOutput.length() / n;
52-
StringBuffer transpositionPlaintext = new StringBuffer();
53-
for (int i = 0; i < n; i++) {
54-
for (int j = 0; j < transpositionOutput.length() / n; j++) {
55-
char c = transpositionOutput.charAt(i + (j * n));
56-
transpositionPlaintext.append(c);
50+
// Transposition decryption
51+
n = transpositionOutput.length() / n;
52+
StringBuffer transpositionPlaintext = new StringBuffer();
53+
for (int i = 0; i < n; i++) {
54+
for (int j = 0; j < transpositionOutput.length() / n; j++) {
55+
char c = transpositionOutput.charAt(i + (j * n));
56+
transpositionPlaintext.append(c);
57+
}
5758
}
58-
}
5959

60-
// Substitution decryption
61-
StringBuffer plaintext = new StringBuffer();
62-
for (int i = 0; i < transpositionPlaintext.length(); i++) {
63-
char c = transpositionPlaintext.charAt(i);
64-
plaintext.append((char) (c - 5));
65-
}
60+
// Substitution decryption
61+
StringBuffer plaintext = new StringBuffer();
62+
for (int i = 0; i < transpositionPlaintext.length(); i++) {
63+
char c = transpositionPlaintext.charAt(i);
64+
plaintext.append((char) (c - 5));
65+
}
6666

67-
System.out.println("Plaintext: ");
68-
System.out.println(plaintext);
69-
sc.close();
67+
System.out.println("Plaintext: ");
68+
System.out.println(plaintext);
69+
sc.close();
70+
}
7071
}
7172
}

src/main/java/com/thealgorithms/datastructures/graphs/BellmanFord.java

+57-52
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import java.util.*;
44

5-
class BellmanFord /*Implementation of Bellman ford to detect negative cycles. Graph accepts inputs
6-
in form of edges which have start vertex, end vertex and weights. Vertices should be labelled with a
7-
number between 0 and total number of vertices-1,both inclusive*/
5+
class BellmanFord /*
6+
* Implementation of Bellman ford to detect negative cycles. Graph accepts
7+
* inputs
8+
* in form of edges which have start vertex, end vertex and weights. Vertices
9+
* should be labelled with a
10+
* number between 0 and total number of vertices-1,both inclusive
11+
*/
812
{
913

1014
int vertex, edge;
@@ -36,7 +40,7 @@ public Edge(int a, int b, int c) {
3640

3741
/**
3842
* @param p[] Parent array which shows updates in edges
39-
* @param i Current vertex under consideration
43+
* @param i Current vertex under consideration
4044
*/
4145
void printPath(int[] p, int i) {
4246
if (p[i] == -1) { // Found the path back to parent
@@ -52,64 +56,65 @@ public static void main(String[] args) {
5256
}
5357

5458
public void go() { // shows distance to all vertices // Interactive run for understanding the
55-
// class first time. Assumes source vertex is 0 and
56-
Scanner sc = new Scanner(System.in); // Grab scanner object for user input
57-
int i, v, e, u, ve, w, j, neg = 0;
58-
System.out.println("Enter no. of vertices and edges please");
59-
v = sc.nextInt();
60-
e = sc.nextInt();
61-
Edge[] arr = new Edge[e]; // Array of edges
62-
System.out.println("Input edges");
63-
for (i = 0; i < e; i++) {
64-
u = sc.nextInt();
65-
ve = sc.nextInt();
66-
w = sc.nextInt();
67-
arr[i] = new Edge(u, ve, w);
68-
}
69-
int[] dist = new int[v]; // Distance array for holding the finalized shortest path distance
70-
// between source
71-
// and all vertices
72-
int[] p = new int[v]; // Parent array for holding the paths
73-
for (i = 0; i < v; i++) {
74-
dist[i] = Integer.MAX_VALUE; // Initializing distance values
75-
}
76-
dist[0] = 0;
77-
p[0] = -1;
78-
for (i = 0; i < v - 1; i++) {
59+
try ( // class first time. Assumes source vertex is 0 and
60+
Scanner sc = new Scanner(System.in)) {
61+
int i, v, e, u, ve, w, j, neg = 0;
62+
System.out.println("Enter no. of vertices and edges please");
63+
v = sc.nextInt();
64+
e = sc.nextInt();
65+
Edge[] arr = new Edge[e]; // Array of edges
66+
System.out.println("Input edges");
67+
for (i = 0; i < e; i++) {
68+
u = sc.nextInt();
69+
ve = sc.nextInt();
70+
w = sc.nextInt();
71+
arr[i] = new Edge(u, ve, w);
72+
}
73+
int[] dist = new int[v]; // Distance array for holding the finalized shortest path distance
74+
// between source
75+
// and all vertices
76+
int[] p = new int[v]; // Parent array for holding the paths
77+
for (i = 0; i < v; i++) {
78+
dist[i] = Integer.MAX_VALUE; // Initializing distance values
79+
}
80+
dist[0] = 0;
81+
p[0] = -1;
82+
for (i = 0; i < v - 1; i++) {
83+
for (j = 0; j < e; j++) {
84+
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
85+
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
86+
p[arr[j].v] = arr[j].u;
87+
}
88+
}
89+
}
90+
// Final cycle for negative checking
7991
for (j = 0; j < e; j++) {
8092
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
81-
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
82-
p[arr[j].v] = arr[j].u;
93+
neg = 1;
94+
System.out.println("Negative cycle");
95+
break;
8396
}
8497
}
85-
}
86-
// Final cycle for negative checking
87-
for (j = 0; j < e; j++) {
88-
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
89-
neg = 1;
90-
System.out.println("Negative cycle");
91-
break;
92-
}
93-
}
94-
if (neg == 0) { // Go ahead and show results of computation
95-
System.out.println("Distances are: ");
96-
for (i = 0; i < v; i++) {
97-
System.out.println(i + " " + dist[i]);
98-
}
99-
System.out.println("Path followed:");
100-
for (i = 0; i < v; i++) {
101-
System.out.print("0 ");
102-
printPath(p, i);
103-
System.out.println();
98+
if (neg == 0) { // Go ahead and show results of computation
99+
System.out.println("Distances are: ");
100+
for (i = 0; i < v; i++) {
101+
System.out.println(i + " " + dist[i]);
102+
}
103+
System.out.println("Path followed:");
104+
for (i = 0; i < v; i++) {
105+
System.out.print("0 ");
106+
printPath(p, i);
107+
System.out.println();
108+
}
104109
}
110+
sc.close();
105111
}
106-
sc.close();
107112
}
108113

109114
/**
110115
* @param source Starting vertex
111-
* @param end Ending vertex
112-
* @param Edge Array of edges
116+
* @param end Ending vertex
117+
* @param Edge Array of edges
113118
*/
114119
public void show(int source, int end,
115120
Edge[] arr) { // be created by using addEdge() method and passed by calling getEdgeArray()

src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java

+20-20
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
public class ReverseStack {
1212

1313
public static void main(String[] args) {
14-
Scanner sc = new Scanner(System.in);
15-
System.out.println("Enter the number of elements you wish to insert in the stack");
16-
int n = sc.nextInt();
17-
int i;
18-
Stack<Integer> stack = new Stack<Integer>();
19-
System.out.println("Enter the stack elements");
20-
for (i = 0; i < n; i++) {
21-
stack.push(sc.nextInt());
22-
}
23-
sc.close();
24-
reverseStack(stack);
25-
System.out.println("The reversed stack is:");
26-
while (!stack.isEmpty()) {
27-
System.out.print(stack.peek() + ",");
28-
stack.pop();
14+
try (Scanner sc = new Scanner(System.in)) {
15+
System.out.println("Enter the number of elements you wish to insert in the stack");
16+
int n = sc.nextInt();
17+
int i;
18+
Stack<Integer> stack = new Stack<Integer>();
19+
System.out.println("Enter the stack elements");
20+
for (i = 0; i < n; i++) {
21+
stack.push(sc.nextInt());
22+
}
23+
sc.close();
24+
reverseStack(stack);
25+
System.out.println("The reversed stack is:");
26+
while (!stack.isEmpty()) {
27+
System.out.print(stack.peek() + ",");
28+
stack.pop();
29+
}
2930
}
3031
}
3132

@@ -48,16 +49,15 @@ private static void reverseStack(Stack<Integer> stack) {
4849

4950
private static void insertAtBottom(Stack<Integer> stack, int element) {
5051
if (stack.isEmpty()) {
51-
// When stack is empty, insert the element so it will be present at the bottom of the
52-
// stack
52+
// When stack is empty, insert the element so it will be present at
53+
// the bottom of the stack
5354
stack.push(element);
5455
return;
5556
}
5657

5758
int ele = stack.peek();
58-
/*Keep popping elements till stack becomes empty. Push the elements once the topmost element
59-
has moved to the bottom of the stack.
60-
*/
59+
// Keep popping elements till stack becomes empty. Push the elements
60+
// once the topmost element has moved to the bottom of the stack.
6161
stack.pop();
6262
insertAtBottom(stack, element);
6363

0 commit comments

Comments
 (0)