Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class Finals_Queue_KJSE {
  4.  
  5. static ArrayList<Integer> arrayInt= new ArrayList<Integer>();
  6.  
  7. public static void main(String[] args) throws IOException{
  8. loopback();
  9. }
  10. public static void loopback(){
  11.  
  12. Scanner input= new Scanner(System.in);
  13. Scanner inChoice= new Scanner(System.in);
  14. while(true){
  15. try{
  16. System.out.println("\n*****MENU*****");
  17. System.out.println("[1]Enqueue\n[2]Dequeue\n[3]Display\n[4]Front\n[5]Rear\n[6]Exit");
  18.  
  19. System.out.println("\n*****Now select a choice from Menu*****");
  20. int menu=inChoice.nextInt();
  21.  
  22. switch(menu){
  23.  
  24. case 1:
  25. try {
  26. System.out.println("How many numbers you want to add? ");
  27. int num=input.nextInt();
  28. for(int i =0; i<num; i++){
  29. System.out.print("Enter a Number:");
  30. int num_arrInt = input.nextInt();
  31. arrayInt.add(num_arrInt);
  32. }
  33. }catch(Exception e) {
  34. inputdata();
  35. }
  36. break;
  37.  
  38. case 2:
  39. if(arrayInt.isEmpty()) {
  40. System.out.println("There are no data inputted on arraylist.");
  41. } else {
  42. System.out.println("Enter the number you want to be removed: ");
  43. int tindx = input.nextInt();
  44. int rindx = arrayInt.indexOf(tindx);
  45. arrayInt.remove(rindx);
  46. }
  47. break;
  48.  
  49. case 3:
  50. if(arrayInt.isEmpty()) {
  51. System.out.println("There are no data inputted on arraylist.");
  52. } else {
  53. System.out.print("\nContents of Array List: ");
  54. for(Integer display : arrayInt){
  55. System.out.print("["+display+"]");
  56. }
  57. }
  58. break;
  59.  
  60. case 4:
  61. if(arrayInt.isEmpty()) {
  62. System.out.println("There are no data inputted on arraylist.");
  63. } else {
  64. System.out.print("Front number is: "+"["+arrayInt.get(0)+"]");
  65. }
  66. break;
  67. case 5:
  68. if(arrayInt.isEmpty()) {
  69. System.out.println("There are no data inputted on arraylist.");
  70. } else {
  71. System.out.print("Rear number is: "+"["+arrayInt.get((arrayInt.size())-1)+"]");
  72. }
  73. break;
  74. case 6:
  75. System.out.println("The Program will now exit.");
  76. System.exit(0);
  77. break;
  78. default:
  79. System.out.println("Out of choice");
  80. }
  81. }catch(Exception e){
  82. System.out.println("\nInvalid Input");
  83. loopback();
  84. }
  85. }
  86. }
  87.  
  88. public static void inputdata() {
  89. Scanner input= new Scanner(System.in);
  90. Scanner inChoice= new Scanner(System.in);
  91. try {
  92. System.out.println("Invalid Input");
  93. System.out.println("How many numbers you want to add? ");
  94. int num=input.nextInt();
  95. for(int i =0; i<num; i++){
  96. System.out.print("Enter a Number:");
  97. int num_arrInt = input.nextInt();
  98. arrayInt.add(num_arrInt);
  99. }
  100. }catch(Exception e) {
  101. inputdata();
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement