diff --git a/Data-Structures/HashTable.java b/Data-Structures/HashTable.java index f5e7e49..7c14a02 100644 --- a/Data-Structures/HashTable.java +++ b/Data-Structures/HashTable.java @@ -1,9 +1,10 @@ import java.util.Scanner; -public class HashTable { - static int size; - static Dataitem[] hasharray; - static final Dataitem DELETED = new Dataitem(-1, -1); +public class HashTable { + static int size; + static Dataitem[] hasharray; + static final Dataitem DELETED = new Dataitem(-1, -1); + private static final String INVALID_INT_MESSAGE = "Invalid input. Please enter an integer."; static class Dataitem { int data; @@ -15,12 +16,31 @@ static class Dataitem { } } - static void initialise() { - Scanner sc = new Scanner(System.in); - System.out.print("Enter the size of the Hash Table: "); - size = sc.nextInt(); - hasharray = new Dataitem[size]; - } + static void initialise(Scanner sc) { + size = readPositiveInt(sc, "Enter the size of the Hash Table: "); + hasharray = new Dataitem[size]; + } + + static int readInt(Scanner sc, String prompt) { + while (true) { + System.out.print(prompt); + if (sc.hasNextInt()) { + return sc.nextInt(); + } + System.out.println(INVALID_INT_MESSAGE); + sc.next(); + } + } + + static int readPositiveInt(Scanner sc, String prompt) { + while (true) { + int value = readInt(sc, prompt); + if (value > 0) { + return value; + } + System.out.println("Invalid input. Please enter an integer greater than 0."); + } + } static int hashcode(int key) { return Math.floorMod(key, size); @@ -69,22 +89,20 @@ static void display() { } } - public static void main(String[] args) { - initialise(); - Scanner sc = new Scanner(System.in); - - System.out.print("Enter the number of Insertions: "); - int n = sc.nextInt(); - - for (int i = 0; i < n; i++) { - System.out.print("Enter the key: "); - int key = sc.nextInt(); - System.out.print("Enter the element: "); - int element = sc.nextInt(); - insert(key, element); - } - - System.out.println("Insertion done!"); - display(); - } -} + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + initialise(sc); + + int n = readPositiveInt(sc, "Enter the number of Insertions: "); + + for (int i = 0; i < n; i++) { + int key = readInt(sc, "Enter the key: "); + int element = readInt(sc, "Enter the element: "); + insert(key, element); + } + + System.out.println("Insertion done!"); + display(); + sc.close(); + } +} diff --git a/Data-Structures/Queue/CircularQueue.java b/Data-Structures/Queue/CircularQueue.java index f85e347..d098d21 100644 --- a/Data-Structures/Queue/CircularQueue.java +++ b/Data-Structures/Queue/CircularQueue.java @@ -77,15 +77,34 @@ public boolean isFull() { } } -public class CircularQueue { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - - System.out.print("Enter Queue size: "); - int capacity = sc.nextInt(); - - Queue queue = new Queue(capacity); - int choice; +public class CircularQueue { + private static int readInt(Scanner sc, String prompt) { + while (true) { + System.out.print(prompt); + if (sc.hasNextInt()) { + return sc.nextInt(); + } + System.out.println("Invalid input. Please enter an integer."); + sc.next(); + } + } + + private static int readPositiveInt(Scanner sc, String prompt) { + while (true) { + int value = readInt(sc, prompt); + if (value > 0) { + return value; + } + System.out.println("Invalid input. Please enter an integer greater than 0."); + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int capacity = readPositiveInt(sc, "Enter Queue size: "); + + Queue queue = new Queue(capacity); + int choice; do { System.out.println("\n--- Circular Queue Operations ---"); @@ -94,18 +113,15 @@ public static void main(String[] args) { System.out.println("3. Peek"); System.out.println("4. Display"); System.out.println("5. Is Full?"); - System.out.println("6. Is Empty?"); - System.out.println("7. Exit"); - System.out.print("Enter your choice: "); - - choice = sc.nextInt(); - - switch (choice) { - case 1: - System.out.print("Enter element to Enqueue: "); - int element = sc.nextInt(); - queue.enqueue(element); - break; + System.out.println("6. Is Empty?"); + System.out.println("7. Exit"); + choice = readInt(sc, "Enter your choice: "); + + switch (choice) { + case 1: + int element = readInt(sc, "Enter element to Enqueue: "); + queue.enqueue(element); + break; case 2: queue.dequeue(); @@ -127,14 +143,14 @@ public static void main(String[] args) { System.out.println(queue.isEmpty() ? "The Queue is Empty." : "The Queue is NOT Empty."); break; - case 7: - System.out.println("Exiting..."); - break; - - default: - System.out.println("Invalid choice!"); - } - } while (choice != 7); + case 7: + System.out.println("Exiting..."); + break; + + default: + System.out.println("Invalid choice. Please enter a valid menu option."); + } + } while (choice != 7); sc.close(); } diff --git a/Data-Structures/Queue/Queue.java b/Data-Structures/Queue/Queue.java index adf2d97..938b6a5 100644 --- a/Data-Structures/Queue/Queue.java +++ b/Data-Structures/Queue/Queue.java @@ -1,6 +1,6 @@ import java.util.Scanner; -class Queue { +class Queue { private int[] arr; private int front; private int rear; @@ -74,17 +74,36 @@ public boolean isEmpty() { return rear == -1; } - public boolean isFull() { - return rear == capacity - 1; - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - - System.out.print("Enter Queue size: "); - int capacity = sc.nextInt(); - - Queue queue = new Queue(capacity); + public boolean isFull() { + return rear == capacity - 1; + } + + private static int readInt(Scanner sc, String prompt) { + while (true) { + System.out.print(prompt); + if (sc.hasNextInt()) { + return sc.nextInt(); + } + System.out.println("Invalid input. Please enter an integer."); + sc.next(); + } + } + + private static int readPositiveInt(Scanner sc, String prompt) { + while (true) { + int value = readInt(sc, prompt); + if (value > 0) { + return value; + } + System.out.println("Invalid input. Please enter an integer greater than 0."); + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int capacity = readPositiveInt(sc, "Enter Queue size: "); + + Queue queue = new Queue(capacity); while (true) { System.out.println("\n--- Queue Operations ---"); @@ -93,18 +112,15 @@ public static void main(String[] args) { System.out.println("3. Peek"); System.out.println("4. Display"); System.out.println("5. Is Full?"); - System.out.println("6. Is Empty?"); - System.out.println("7. Exit"); - System.out.print("Enter your choice: "); - - int choice = sc.nextInt(); - - switch (choice) { - case 1: - System.out.print("Enter element to Enqueue: "); - int element = sc.nextInt(); - queue.enqueue(element); - break; + System.out.println("6. Is Empty?"); + System.out.println("7. Exit"); + int choice = readInt(sc, "Enter your choice: "); + + switch (choice) { + case 1: + int element = readInt(sc, "Enter element to Enqueue: "); + queue.enqueue(element); + break; case 2: queue.dequeue(); @@ -126,14 +142,14 @@ public static void main(String[] args) { System.out.println(queue.isEmpty() ? "The Queue is Empty." : "The Queue is NOT Empty."); break; - case 7: - System.out.println("Exiting..."); - sc.close(); - return; - - default: - System.out.println("Invalid choice!"); - } - } - } -} + case 7: + System.out.println("Exiting..."); + sc.close(); + return; + + default: + System.out.println("Invalid choice. Please enter a valid menu option."); + } + } + } +} diff --git a/Data-Structures/Stack.java b/Data-Structures/Stack.java index a70ee29..d39a8e1 100644 --- a/Data-Structures/Stack.java +++ b/Data-Structures/Stack.java @@ -1,6 +1,6 @@ import java.util.Scanner; -class Stack { +class Stack { private int[] arr; private int top; private int maxSize; @@ -37,7 +37,7 @@ int pop() { return arr[top--]; } - void display() { + void display() { if (isEmpty()) { System.out.println("Stack is Empty!"); return; @@ -46,34 +46,50 @@ void display() { for (int i = 0; i <= top; i++) { System.out.print(arr[i] + " "); } - System.out.println(); - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - - System.out.print("Enter the Stack size: "); - int size = sc.nextInt(); - - Stack stack = new Stack(size); - - while (true) { + System.out.println(); + } + + private static int readInt(Scanner sc, String prompt) { + while (true) { + System.out.print(prompt); + if (sc.hasNextInt()) { + return sc.nextInt(); + } + System.out.println("Invalid input. Please enter an integer."); + sc.next(); + } + } + + private static int readPositiveInt(Scanner sc, String prompt) { + while (true) { + int value = readInt(sc, prompt); + if (value > 0) { + return value; + } + System.out.println("Invalid input. Please enter an integer greater than 0."); + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int size = readPositiveInt(sc, "Enter the Stack size: "); + + Stack stack = new Stack(size); + + while (true) { System.out.println("\n*** Stack Operations ***"); System.out.println("1. Push"); System.out.println("2. Pop"); - System.out.println("3. Display"); - System.out.println("4. Exit"); - System.out.print("Enter your choice: "); - - int choice = sc.nextInt(); - - switch (choice) { - case 1: - System.out.print("Enter value to Push: "); - int value = sc.nextInt(); - stack.push(value); - break; - case 2: + System.out.println("3. Display"); + System.out.println("4. Exit"); + int choice = readInt(sc, "Enter your choice: "); + + switch (choice) { + case 1: + int value = readInt(sc, "Enter value to Push: "); + stack.push(value); + break; + case 2: stack.pop(); break; case 3: @@ -81,11 +97,11 @@ public static void main(String[] args) { break; case 4: System.out.println("Exiting..."); - sc.close(); - return; - default: - System.out.println("Invalid choice!"); - } - } - } -} + sc.close(); + return; + default: + System.out.println("Invalid choice. Please enter a valid menu option."); + } + } + } +}