Results 1 to 2 of 2

Thread: Program Exit

  1. #1
    mdavda909 is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Program Exit

    Hi,
    How do i exit a java program is the user input is nothing "",

    This is my code
    Java Code:
    import java.util.Scanner;
    
    
    public class MyQueue<T> implements Queue<T> {
        private T[] ringbuffer;
        static int capacity = 0;
        int mysize = 0;
    
        @SuppressWarnings ({"unchecked"})
        public MyQueue (int size) {
            ringbuffer = (T[]) new Object[size];
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int userinput = Integer.parseInt(args[0]);
            final Queue<Integer> queue = new MyQueue<Integer>(userinput);
            final Scanner kb = new Scanner(System.in);
            capacity = userinput;
    
            while(kb.hasNext()) {
                int value = 0;
                String input = kb.next();
                if(input.equals("enqueue")) {
                    value = kb.nextInt();
                }
    
                try {
                    // Enqueue or dequeue
                    if (input.equals("enqueue")) {
                        queue.enqueue(value);
                    }
                    if (input.equals("dequeue")) {
                        queue.dequeue();
                    }
                } catch (final BufferEmptyException ex) {
                    // Do something in case of an BufferEmptyException exception
                    System.out.println("Queue is empty");
                } catch (final BufferFullException ex) {
                    // Do something in case of an BufferFullException exception
                    System.out.println("Queue is full");
                }
            }
            kb.close();
        }
    
    
    
        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return (mysize < capacity) && (mysize >= 0);
        }
        @Override
        public T dequeue() throws BufferEmptyException {
            // TODO Auto-generated method stub
            // Check if there is an element, return error if empty
            final boolean hasElement = (mysize > 0);
    
            // Return dequeued value else display error
            if (!hasElement) {
                throw new BufferEmptyException();
            } else {
                // Remove element at the front
                System.out.println("dequeue " + ringbuffer[0]);
                mysize--;
                // Move all subsequent elements by one index
                for (int i = 0; i < (capacity - 1); i++) {
                    ringbuffer[i] = ringbuffer[i + 1];
                }
            }
            return null;
        }
        @Override
        public void enqueue(T item) throws BufferFullException {
            // TODO Auto-generated method stub
            final boolean emptiness = isEmpty();
    
            // Return enqueued value else display error
            if (emptiness) {
                // Add element to the end
                ringbuffer[mysize] = item;
                mysize++;
                System.out.println("enqueue " + item);
            } else {
                throw new BufferFullException();
            }
        }
    
    }

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: Program Exit

    Hi,

    Some comments from my side:
    - the exit could be done through a command exit or quit. So you loop as long as this command wasn't given.
    - you should properly check the user input. If the user types something illegal then I would expect some reaction.
    - isEmpty() logic seems wrong. What is the logic of stack is empty? And what did your method check?
    - your capacity is static? And initialised in main? What happens if someone wants to have 2 queues of different sizes?

    With kind regards,

    Konrad

Similar Threads

  1. Replies: 7
    Last Post: 06-27-2012, 05:30 PM
  2. How do I stop the program but not exit the JFrame?
    By rajkobie in forum New To Java
    Replies: 1
    Last Post: 04-23-2011, 03:53 PM
  3. Fatal Exception, Program will exit(pop up)
    By singularity in forum New To Java
    Replies: 5
    Last Post: 05-04-2010, 05:39 PM
  4. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 08:32 PM
  5. How to exit the program..
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •