Skip to content

Commit 17c1021

Browse files
authored
Merge pull request #16 from Nullkernel/codex/improve-command-line-demo-reliability
Harden CLI demos against invalid input and validate HashTable size
2 parents 6dec9bc + 6bce3b1 commit 17c1021

4 files changed

Lines changed: 194 additions & 128 deletions

File tree

Data-Structures/HashTable.java

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import java.util.Scanner;
22

3-
public class HashTable {
4-
static int size;
5-
static Dataitem[] hasharray;
6-
static final Dataitem DELETED = new Dataitem(-1, -1);
3+
public class HashTable {
4+
static int size;
5+
static Dataitem[] hasharray;
6+
static final Dataitem DELETED = new Dataitem(-1, -1);
7+
private static final String INVALID_INT_MESSAGE = "Invalid input. Please enter an integer.";
78

89
static class Dataitem {
910
int data;
@@ -15,12 +16,31 @@ static class Dataitem {
1516
}
1617
}
1718

18-
static void initialise() {
19-
Scanner sc = new Scanner(System.in);
20-
System.out.print("Enter the size of the Hash Table: ");
21-
size = sc.nextInt();
22-
hasharray = new Dataitem[size];
23-
}
19+
static void initialise(Scanner sc) {
20+
size = readPositiveInt(sc, "Enter the size of the Hash Table: ");
21+
hasharray = new Dataitem[size];
22+
}
23+
24+
static int readInt(Scanner sc, String prompt) {
25+
while (true) {
26+
System.out.print(prompt);
27+
if (sc.hasNextInt()) {
28+
return sc.nextInt();
29+
}
30+
System.out.println(INVALID_INT_MESSAGE);
31+
sc.next();
32+
}
33+
}
34+
35+
static int readPositiveInt(Scanner sc, String prompt) {
36+
while (true) {
37+
int value = readInt(sc, prompt);
38+
if (value > 0) {
39+
return value;
40+
}
41+
System.out.println("Invalid input. Please enter an integer greater than 0.");
42+
}
43+
}
2444

2545
static int hashcode(int key) {
2646
return Math.floorMod(key, size);
@@ -69,22 +89,20 @@ static void display() {
6989
}
7090
}
7191

72-
public static void main(String[] args) {
73-
initialise();
74-
Scanner sc = new Scanner(System.in);
75-
76-
System.out.print("Enter the number of Insertions: ");
77-
int n = sc.nextInt();
78-
79-
for (int i = 0; i < n; i++) {
80-
System.out.print("Enter the key: ");
81-
int key = sc.nextInt();
82-
System.out.print("Enter the element: ");
83-
int element = sc.nextInt();
84-
insert(key, element);
85-
}
86-
87-
System.out.println("Insertion done!");
88-
display();
89-
}
90-
}
92+
public static void main(String[] args) {
93+
Scanner sc = new Scanner(System.in);
94+
initialise(sc);
95+
96+
int n = readPositiveInt(sc, "Enter the number of Insertions: ");
97+
98+
for (int i = 0; i < n; i++) {
99+
int key = readInt(sc, "Enter the key: ");
100+
int element = readInt(sc, "Enter the element: ");
101+
insert(key, element);
102+
}
103+
104+
System.out.println("Insertion done!");
105+
display();
106+
sc.close();
107+
}
108+
}

Data-Structures/Queue/CircularQueue.java

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,34 @@ public boolean isFull() {
7777
}
7878
}
7979

80-
public class CircularQueue {
81-
public static void main(String[] args) {
82-
Scanner sc = new Scanner(System.in);
83-
84-
System.out.print("Enter Queue size: ");
85-
int capacity = sc.nextInt();
86-
87-
Queue queue = new Queue(capacity);
88-
int choice;
80+
public class CircularQueue {
81+
private static int readInt(Scanner sc, String prompt) {
82+
while (true) {
83+
System.out.print(prompt);
84+
if (sc.hasNextInt()) {
85+
return sc.nextInt();
86+
}
87+
System.out.println("Invalid input. Please enter an integer.");
88+
sc.next();
89+
}
90+
}
91+
92+
private static int readPositiveInt(Scanner sc, String prompt) {
93+
while (true) {
94+
int value = readInt(sc, prompt);
95+
if (value > 0) {
96+
return value;
97+
}
98+
System.out.println("Invalid input. Please enter an integer greater than 0.");
99+
}
100+
}
101+
102+
public static void main(String[] args) {
103+
Scanner sc = new Scanner(System.in);
104+
int capacity = readPositiveInt(sc, "Enter Queue size: ");
105+
106+
Queue queue = new Queue(capacity);
107+
int choice;
89108

90109
do {
91110
System.out.println("\n--- Circular Queue Operations ---");
@@ -94,18 +113,15 @@ public static void main(String[] args) {
94113
System.out.println("3. Peek");
95114
System.out.println("4. Display");
96115
System.out.println("5. Is Full?");
97-
System.out.println("6. Is Empty?");
98-
System.out.println("7. Exit");
99-
System.out.print("Enter your choice: ");
100-
101-
choice = sc.nextInt();
102-
103-
switch (choice) {
104-
case 1:
105-
System.out.print("Enter element to Enqueue: ");
106-
int element = sc.nextInt();
107-
queue.enqueue(element);
108-
break;
116+
System.out.println("6. Is Empty?");
117+
System.out.println("7. Exit");
118+
choice = readInt(sc, "Enter your choice: ");
119+
120+
switch (choice) {
121+
case 1:
122+
int element = readInt(sc, "Enter element to Enqueue: ");
123+
queue.enqueue(element);
124+
break;
109125

110126
case 2:
111127
queue.dequeue();
@@ -127,14 +143,14 @@ public static void main(String[] args) {
127143
System.out.println(queue.isEmpty() ? "The Queue is Empty." : "The Queue is NOT Empty.");
128144
break;
129145

130-
case 7:
131-
System.out.println("Exiting...");
132-
break;
133-
134-
default:
135-
System.out.println("Invalid choice!");
136-
}
137-
} while (choice != 7);
146+
case 7:
147+
System.out.println("Exiting...");
148+
break;
149+
150+
default:
151+
System.out.println("Invalid choice. Please enter a valid menu option.");
152+
}
153+
} while (choice != 7);
138154

139155
sc.close();
140156
}

Data-Structures/Queue/Queue.java

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import java.util.Scanner;
22

3-
class Queue {
3+
class Queue {
44
private int[] arr;
55
private int front;
66
private int rear;
@@ -74,17 +74,36 @@ public boolean isEmpty() {
7474
return rear == -1;
7575
}
7676

77-
public boolean isFull() {
78-
return rear == capacity - 1;
79-
}
80-
81-
public static void main(String[] args) {
82-
Scanner sc = new Scanner(System.in);
83-
84-
System.out.print("Enter Queue size: ");
85-
int capacity = sc.nextInt();
86-
87-
Queue queue = new Queue(capacity);
77+
public boolean isFull() {
78+
return rear == capacity - 1;
79+
}
80+
81+
private static int readInt(Scanner sc, String prompt) {
82+
while (true) {
83+
System.out.print(prompt);
84+
if (sc.hasNextInt()) {
85+
return sc.nextInt();
86+
}
87+
System.out.println("Invalid input. Please enter an integer.");
88+
sc.next();
89+
}
90+
}
91+
92+
private static int readPositiveInt(Scanner sc, String prompt) {
93+
while (true) {
94+
int value = readInt(sc, prompt);
95+
if (value > 0) {
96+
return value;
97+
}
98+
System.out.println("Invalid input. Please enter an integer greater than 0.");
99+
}
100+
}
101+
102+
public static void main(String[] args) {
103+
Scanner sc = new Scanner(System.in);
104+
int capacity = readPositiveInt(sc, "Enter Queue size: ");
105+
106+
Queue queue = new Queue(capacity);
88107

89108
while (true) {
90109
System.out.println("\n--- Queue Operations ---");
@@ -93,18 +112,15 @@ public static void main(String[] args) {
93112
System.out.println("3. Peek");
94113
System.out.println("4. Display");
95114
System.out.println("5. Is Full?");
96-
System.out.println("6. Is Empty?");
97-
System.out.println("7. Exit");
98-
System.out.print("Enter your choice: ");
99-
100-
int choice = sc.nextInt();
101-
102-
switch (choice) {
103-
case 1:
104-
System.out.print("Enter element to Enqueue: ");
105-
int element = sc.nextInt();
106-
queue.enqueue(element);
107-
break;
115+
System.out.println("6. Is Empty?");
116+
System.out.println("7. Exit");
117+
int choice = readInt(sc, "Enter your choice: ");
118+
119+
switch (choice) {
120+
case 1:
121+
int element = readInt(sc, "Enter element to Enqueue: ");
122+
queue.enqueue(element);
123+
break;
108124

109125
case 2:
110126
queue.dequeue();
@@ -126,14 +142,14 @@ public static void main(String[] args) {
126142
System.out.println(queue.isEmpty() ? "The Queue is Empty." : "The Queue is NOT Empty.");
127143
break;
128144

129-
case 7:
130-
System.out.println("Exiting...");
131-
sc.close();
132-
return;
133-
134-
default:
135-
System.out.println("Invalid choice!");
136-
}
137-
}
138-
}
139-
}
145+
case 7:
146+
System.out.println("Exiting...");
147+
sc.close();
148+
return;
149+
150+
default:
151+
System.out.println("Invalid choice. Please enter a valid menu option.");
152+
}
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)