Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Finals_Queue_KJSE {
- static ArrayList<Integer> arrayInt= new ArrayList<Integer>();
- public static void main(String[] args) throws IOException{
- loopback();
- }
- public static void loopback(){
- Scanner input= new Scanner(System.in);
- Scanner inChoice= new Scanner(System.in);
- while(true){
- try{
- System.out.println("\n*****MENU*****");
- System.out.println("[1]Enqueue\n[2]Dequeue\n[3]Display\n[4]Front\n[5]Rear\n[6]Exit");
- System.out.println("\n*****Now select a choice from Menu*****");
- int menu=inChoice.nextInt();
- switch(menu){
- case 1:
- try {
- System.out.println("How many numbers you want to add? ");
- int num=input.nextInt();
- for(int i =0; i<num; i++){
- System.out.print("Enter a Number:");
- int num_arrInt = input.nextInt();
- arrayInt.add(num_arrInt);
- }
- }catch(Exception e) {
- inputdata();
- }
- break;
- case 2:
- if(arrayInt.isEmpty()) {
- System.out.println("There are no data inputted on arraylist.");
- } else {
- System.out.println("Enter the number you want to be removed: ");
- int tindx = input.nextInt();
- int rindx = arrayInt.indexOf(tindx);
- arrayInt.remove(rindx);
- }
- break;
- case 3:
- if(arrayInt.isEmpty()) {
- System.out.println("There are no data inputted on arraylist.");
- } else {
- System.out.print("\nContents of Array List: ");
- for(Integer display : arrayInt){
- System.out.print("["+display+"]");
- }
- }
- break;
- case 4:
- if(arrayInt.isEmpty()) {
- System.out.println("There are no data inputted on arraylist.");
- } else {
- System.out.print("Front number is: "+"["+arrayInt.get(0)+"]");
- }
- break;
- case 5:
- if(arrayInt.isEmpty()) {
- System.out.println("There are no data inputted on arraylist.");
- } else {
- System.out.print("Rear number is: "+"["+arrayInt.get((arrayInt.size())-1)+"]");
- }
- break;
- case 6:
- System.out.println("The Program will now exit.");
- System.exit(0);
- break;
- default:
- System.out.println("Out of choice");
- }
- }catch(Exception e){
- System.out.println("\nInvalid Input");
- loopback();
- }
- }
- }
- public static void inputdata() {
- Scanner input= new Scanner(System.in);
- Scanner inChoice= new Scanner(System.in);
- try {
- System.out.println("Invalid Input");
- System.out.println("How many numbers you want to add? ");
- int num=input.nextInt();
- for(int i =0; i<num; i++){
- System.out.print("Enter a Number:");
- int num_arrInt = input.nextInt();
- arrayInt.add(num_arrInt);
- }
- }catch(Exception e) {
- inputdata();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement