mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-11 06:04:27 +08:00
Close Scanner
to avoid resource leak (#5077)
This commit is contained in:
@ -5,7 +5,7 @@ import java.util.Scanner;
|
||||
class ProductCipher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
try (Scanner sc = new Scanner(System.in)) {
|
||||
System.out.println("Enter the input to be encrypted: ");
|
||||
String substitutionInput = sc.nextLine();
|
||||
System.out.println(" ");
|
||||
@ -68,4 +68,5 @@ class ProductCipher {
|
||||
System.out.println(plaintext);
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,13 @@ package com.thealgorithms.datastructures.graphs;
|
||||
|
||||
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;
|
||||
@ -52,8 +56,8 @@ number between 0 and total number of vertices-1,both inclusive*/
|
||||
}
|
||||
|
||||
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
|
||||
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();
|
||||
@ -105,6 +109,7 @@ number between 0 and total number of vertices-1,both inclusive*/
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param source Starting vertex
|
||||
|
@ -11,7 +11,7 @@ import java.util.Stack;
|
||||
public class ReverseStack {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
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;
|
||||
@ -28,6 +28,7 @@ public class ReverseStack {
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void reverseStack(Stack<Integer> stack) {
|
||||
if (stack.isEmpty()) {
|
||||
@ -48,16 +49,15 @@ public class ReverseStack {
|
||||
|
||||
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);
|
||||
|
||||
|
@ -10,12 +10,13 @@ import java.util.Scanner;
|
||||
public class NonRepeatingElement {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
try (Scanner sc = new Scanner(System.in)) {
|
||||
int i, res = 0;
|
||||
System.out.println("Enter the number of elements in the array");
|
||||
int n = sc.nextInt();
|
||||
if ((n & 1) == 1) {
|
||||
// Not allowing odd number of elements as we are expecting 2 non repeating numbers
|
||||
// Not allowing odd number of elements as we are expecting 2 non repeating
|
||||
// numbers
|
||||
System.out.println("Array should contain even number of elements");
|
||||
return;
|
||||
}
|
||||
@ -52,19 +53,27 @@ public class NonRepeatingElement {
|
||||
System.out.println("The two non repeating elements are " + num1 + " and " + num2);
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
/*
|
||||
Explanation of the code:
|
||||
let us assume we have an array [1,2,1,2,3,4]
|
||||
Property of XOR: num ^ num = 0.
|
||||
If we XOR all the elemnets of the array we will be left with 3 ^ 4 as 1 ^ 1 and 2 ^ 2 would give
|
||||
0. Our task is to find num1 and num2 from the result of 3 ^ 4 = 7. We need to find two's
|
||||
complement of 7 and find the rightmost set bit. i.e. (num & (-num)) Two's complement of 7 is 001
|
||||
and hence res = 1. There can be 2 options when we Bitise AND this res with all the elements in our
|
||||
array
|
||||
1. Result will come non zero number
|
||||
2. Result will be 0.
|
||||
In the first case we will XOR our element with the first number (which is initially 0)
|
||||
In the second case we will XOR our element with the second number(which is initially 0)
|
||||
This is how we will get non repeating elements with the help of bitwise operators.
|
||||
* Explanation of the code:
|
||||
* let us assume we have an array [1,2,1,2,3,4]
|
||||
* Property of XOR: num ^ num = 0.
|
||||
* If we XOR all the elemnets of the array we will be left with 3 ^ 4 as 1 ^ 1
|
||||
* and 2 ^ 2 would give
|
||||
* 0. Our task is to find num1 and num2 from the result of 3 ^ 4 = 7. We need to
|
||||
* find two's
|
||||
* complement of 7 and find the rightmost set bit. i.e. (num & (-num)) Two's
|
||||
* complement of 7 is 001
|
||||
* and hence res = 1. There can be 2 options when we Bitise AND this res with
|
||||
* all the elements in our
|
||||
* array
|
||||
* 1. Result will come non zero number
|
||||
* 2. Result will be 0.
|
||||
* In the first case we will XOR our element with the first number (which is
|
||||
* initially 0)
|
||||
* In the second case we will XOR our element with the second number(which is
|
||||
* initially 0)
|
||||
* This is how we will get non repeating elements with the help of bitwise
|
||||
* operators.
|
||||
*/
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import java.util.*;
|
||||
public class InsertDeleteInArray {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner s = new Scanner(System.in); // Input statement
|
||||
try (Scanner s = new Scanner(System.in)) {
|
||||
System.out.println("Enter the size of the array");
|
||||
int size = s.nextInt();
|
||||
int[] a = new int[size];
|
||||
@ -47,4 +47,5 @@ public class InsertDeleteInArray {
|
||||
}
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
// File Name should be RecursiveBinarySearch.java
|
||||
// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// Create a SearchAlgorithm class with a generic type
|
||||
@ -47,7 +48,7 @@ public class RecursiveBinarySearch<T extends Comparable<T>> extends SearchAlgori
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
try (Scanner sc = new Scanner(System.in)) {
|
||||
// User inputs
|
||||
System.out.print("Enter the number of elements in the array: ");
|
||||
int n = sc.nextInt();
|
||||
@ -71,4 +72,5 @@ public class RecursiveBinarySearch<T extends Comparable<T>> extends SearchAlgori
|
||||
else
|
||||
System.out.println("Element found at index " + res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user