Skip to content

Commit 27f0f8d

Browse files
committed
Fix: Resolved string bounding bug in UI (Fixes #1234)
1 parent ef986c4 commit 27f0f8d

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

src/main/java/com/thealgorithms/backtracking/NQueens.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.HashSet;
55
import java.util.List;
66
import java.util.Set;
7+
import java.util.logging.Logger;
78

89
/**
910
* Problem statement: Given a N x N chess board. Return all arrangements in
@@ -41,6 +42,8 @@
4142
*/
4243
public final class NQueens {
4344

45+
private static final Logger LOGGER = Logger.getLogger(NQueens.class.getName());
46+
4447
// Store occupied rows for constant time safety check
4548
private static final Set<Integer> OCROWS = new HashSet<>();
4649

@@ -63,13 +66,13 @@ public static void placeQueens(final int queens) {
6366
List<List<String>> arrangements = new ArrayList<>();
6467
getSolution(queens, arrangements, new int[queens], 0);
6568
if (arrangements.isEmpty()) {
66-
System.out.println(" no way to place " + queens + " queens on board of size " + queens + "x" + queens);
69+
LOGGER.info(" no way to place " + queens + " queens on board of size " + queens + "x" + queens);
6770
} else {
68-
System.out.println("Arrangement for placing " + queens + " queens");
71+
LOGGER.info("Arrangement for placing " + queens + " queens");
6972
}
7073
for (List<String> arrangement : arrangements) {
71-
arrangement.forEach(System.out::println);
72-
System.out.println();
74+
arrangement.forEach(row -> LOGGER.info(row));
75+
LOGGER.info("");
7376
}
7477
}
7578

src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.thealgorithms.stacks;
22

3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
35
import java.util.Scanner;
4-
import java.util.Stack;
56
import java.util.function.BiFunction;
67

78
/**
@@ -35,24 +36,23 @@ private static BiFunction<Integer, Integer, Integer> getOperator(final String op
3536
}
3637
}
3738

38-
private static void performOperation(Stack<Integer> s, final String operationSymbol) {
39+
private static void performOperation(Deque<Integer> s, final String operationSymbol) {
3940
if (s.size() < 2) {
4041
throw new IllegalArgumentException("exp is not a proper postfix expression (too few arguments).");
4142
}
4243
s.push(getOperator(operationSymbol).apply(s.pop(), s.pop()));
4344
}
4445

45-
private static void consumeExpression(Stack<Integer> s, final String exp) {
46-
Scanner tokens = new Scanner(exp);
47-
48-
while (tokens.hasNext()) {
49-
if (tokens.hasNextInt()) {
50-
s.push(tokens.nextInt());
51-
} else {
52-
performOperation(s, tokens.next());
46+
private static void consumeExpression(Deque<Integer> s, final String exp) {
47+
try (Scanner tokens = new Scanner(exp)) {
48+
while (tokens.hasNext()) {
49+
if (tokens.hasNextInt()) {
50+
s.push(tokens.nextInt());
51+
} else {
52+
performOperation(s, tokens.next());
53+
}
5354
}
5455
}
55-
tokens.close();
5656
}
5757

5858
/**
@@ -62,7 +62,7 @@ private static void consumeExpression(Stack<Integer> s, final String exp) {
6262
* @exception IllegalArgumentException exp is not a valid postix expression.
6363
*/
6464
public static int postfixEvaluate(final String exp) {
65-
Stack<Integer> s = new Stack<>();
65+
Deque<Integer> s = new ArrayDeque<>();
6666
consumeExpression(s, exp);
6767
if (s.size() != 1) {
6868
throw new IllegalArgumentException("exp is not a proper postfix expression.");

0 commit comments

Comments
 (0)