Skip to content

Commit

Permalink
Close Scanner to avoid resource leak (TheAlgorithms#5077)
Browse files Browse the repository at this point in the history
  • Loading branch information
sozelfist authored Mar 12, 2024
1 parent 47a9b1b commit ab37184
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 225 deletions.
107 changes: 54 additions & 53 deletions src/main/java/com/thealgorithms/ciphers/ProductCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,68 @@
class ProductCipher {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the input to be encrypted: ");
String substitutionInput = sc.nextLine();
System.out.println(" ");
System.out.println("Enter a number: ");
int n = sc.nextInt();
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the input to be encrypted: ");
String substitutionInput = sc.nextLine();
System.out.println(" ");
System.out.println("Enter a number: ");
int n = sc.nextInt();

// Substitution encryption
StringBuffer substitutionOutput = new StringBuffer();
for (int i = 0; i < substitutionInput.length(); i++) {
char c = substitutionInput.charAt(i);
substitutionOutput.append((char) (c + 5));
}
System.out.println(" ");
System.out.println("Substituted text: ");
System.out.println(substitutionOutput);
// Substitution encryption
StringBuffer substitutionOutput = new StringBuffer();
for (int i = 0; i < substitutionInput.length(); i++) {
char c = substitutionInput.charAt(i);
substitutionOutput.append((char) (c + 5));
}
System.out.println(" ");
System.out.println("Substituted text: ");
System.out.println(substitutionOutput);

// Transposition encryption
String transpositionInput = substitutionOutput.toString();
int modulus;
if ((modulus = transpositionInput.length() % n) != 0) {
modulus = n - modulus;
// Transposition encryption
String transpositionInput = substitutionOutput.toString();
int modulus;
if ((modulus = transpositionInput.length() % n) != 0) {
modulus = n - modulus;

for (; modulus != 0; modulus--) {
transpositionInput += "/";
for (; modulus != 0; modulus--) {
transpositionInput += "/";
}
}
}
StringBuffer transpositionOutput = new StringBuffer();
System.out.println(" ");
System.out.println("Transposition Matrix: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < transpositionInput.length() / n; j++) {
char c = transpositionInput.charAt(i + (j * n));
System.out.print(c);
transpositionOutput.append(c);
StringBuffer transpositionOutput = new StringBuffer();
System.out.println(" ");
System.out.println("Transposition Matrix: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < transpositionInput.length() / n; j++) {
char c = transpositionInput.charAt(i + (j * n));
System.out.print(c);
transpositionOutput.append(c);
}
System.out.println();
}
System.out.println();
}
System.out.println(" ");
System.out.println("Final encrypted text: ");
System.out.println(transpositionOutput);
System.out.println(" ");
System.out.println("Final encrypted text: ");
System.out.println(transpositionOutput);

// Transposition decryption
n = transpositionOutput.length() / n;
StringBuffer transpositionPlaintext = new StringBuffer();
for (int i = 0; i < n; i++) {
for (int j = 0; j < transpositionOutput.length() / n; j++) {
char c = transpositionOutput.charAt(i + (j * n));
transpositionPlaintext.append(c);
// Transposition decryption
n = transpositionOutput.length() / n;
StringBuffer transpositionPlaintext = new StringBuffer();
for (int i = 0; i < n; i++) {
for (int j = 0; j < transpositionOutput.length() / n; j++) {
char c = transpositionOutput.charAt(i + (j * n));
transpositionPlaintext.append(c);
}
}
}

// Substitution decryption
StringBuffer plaintext = new StringBuffer();
for (int i = 0; i < transpositionPlaintext.length(); i++) {
char c = transpositionPlaintext.charAt(i);
plaintext.append((char) (c - 5));
}
// Substitution decryption
StringBuffer plaintext = new StringBuffer();
for (int i = 0; i < transpositionPlaintext.length(); i++) {
char c = transpositionPlaintext.charAt(i);
plaintext.append((char) (c - 5));
}

System.out.println("Plaintext: ");
System.out.println(plaintext);
sc.close();
System.out.println("Plaintext: ");
System.out.println(plaintext);
sc.close();
}
}
}
109 changes: 57 additions & 52 deletions src/main/java/com/thealgorithms/datastructures/graphs/BellmanFord.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import java.util.*;

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

int vertex, edge;
Expand Down Expand Up @@ -36,7 +40,7 @@ public Edge(int a, int b, int c) {

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

public void go() { // shows distance to all vertices // Interactive run for understanding the
// class first time. Assumes source vertex is 0 and
Scanner sc = new Scanner(System.in); // Grab scanner object for user input
int i, v, e, u, ve, w, j, neg = 0;
System.out.println("Enter no. of vertices and edges please");
v = sc.nextInt();
e = sc.nextInt();
Edge[] arr = new Edge[e]; // Array of edges
System.out.println("Input edges");
for (i = 0; i < e; i++) {
u = sc.nextInt();
ve = sc.nextInt();
w = sc.nextInt();
arr[i] = new Edge(u, ve, w);
}
int[] dist = new int[v]; // Distance array for holding the finalized shortest path distance
// between source
// and all vertices
int[] p = new int[v]; // Parent array for holding the paths
for (i = 0; i < v; i++) {
dist[i] = Integer.MAX_VALUE; // Initializing distance values
}
dist[0] = 0;
p[0] = -1;
for (i = 0; i < v - 1; i++) {
try ( // class first time. Assumes source vertex is 0 and
Scanner sc = new Scanner(System.in)) {
int i, v, e, u, ve, w, j, neg = 0;
System.out.println("Enter no. of vertices and edges please");
v = sc.nextInt();
e = sc.nextInt();
Edge[] arr = new Edge[e]; // Array of edges
System.out.println("Input edges");
for (i = 0; i < e; i++) {
u = sc.nextInt();
ve = sc.nextInt();
w = sc.nextInt();
arr[i] = new Edge(u, ve, w);
}
int[] dist = new int[v]; // Distance array for holding the finalized shortest path distance
// between source
// and all vertices
int[] p = new int[v]; // Parent array for holding the paths
for (i = 0; i < v; i++) {
dist[i] = Integer.MAX_VALUE; // Initializing distance values
}
dist[0] = 0;
p[0] = -1;
for (i = 0; i < v - 1; i++) {
for (j = 0; j < e; j++) {
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
p[arr[j].v] = arr[j].u;
}
}
}
// Final cycle for negative checking
for (j = 0; j < e; j++) {
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
p[arr[j].v] = arr[j].u;
neg = 1;
System.out.println("Negative cycle");
break;
}
}
}
// Final cycle for negative checking
for (j = 0; j < e; j++) {
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
neg = 1;
System.out.println("Negative cycle");
break;
}
}
if (neg == 0) { // Go ahead and show results of computation
System.out.println("Distances are: ");
for (i = 0; i < v; i++) {
System.out.println(i + " " + dist[i]);
}
System.out.println("Path followed:");
for (i = 0; i < v; i++) {
System.out.print("0 ");
printPath(p, i);
System.out.println();
if (neg == 0) { // Go ahead and show results of computation
System.out.println("Distances are: ");
for (i = 0; i < v; i++) {
System.out.println(i + " " + dist[i]);
}
System.out.println("Path followed:");
for (i = 0; i < v; i++) {
System.out.print("0 ");
printPath(p, i);
System.out.println();
}
}
sc.close();
}
sc.close();
}

/**
* @param source Starting vertex
* @param end Ending vertex
* @param Edge Array of edges
* @param end Ending vertex
* @param Edge Array of edges
*/
public void show(int source, int end,
Edge[] arr) { // be created by using addEdge() method and passed by calling getEdgeArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@
public class ReverseStack {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements you wish to insert in the stack");
int n = sc.nextInt();
int i;
Stack<Integer> stack = new Stack<Integer>();
System.out.println("Enter the stack elements");
for (i = 0; i < n; i++) {
stack.push(sc.nextInt());
}
sc.close();
reverseStack(stack);
System.out.println("The reversed stack is:");
while (!stack.isEmpty()) {
System.out.print(stack.peek() + ",");
stack.pop();
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the number of elements you wish to insert in the stack");
int n = sc.nextInt();
int i;
Stack<Integer> stack = new Stack<Integer>();
System.out.println("Enter the stack elements");
for (i = 0; i < n; i++) {
stack.push(sc.nextInt());
}
sc.close();
reverseStack(stack);
System.out.println("The reversed stack is:");
while (!stack.isEmpty()) {
System.out.print(stack.peek() + ",");
stack.pop();
}
}
}

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

private static void insertAtBottom(Stack<Integer> stack, int element) {
if (stack.isEmpty()) {
// When stack is empty, insert the element so it will be present at the bottom of the
// stack
// When stack is empty, insert the element so it will be present at
// the bottom of the stack
stack.push(element);
return;
}

int ele = stack.peek();
/*Keep popping elements till stack becomes empty. Push the elements once the topmost element
has moved to the bottom of the stack.
*/
// Keep popping elements till stack becomes empty. Push the elements
// once the topmost element has moved to the bottom of the stack.
stack.pop();
insertAtBottom(stack, element);

Expand Down
Loading

0 comments on commit ab37184

Please sign in to comment.