Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How do I fix my program so I can use the keyboard to type in the numbers?

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I fix my program so I can use the keyboard to type in the numbers?

    I am having trouble fixing my program so I can enter the data with the keyboard method. Please Help.
    How do I fix my program so I can use the keyboard to type in the numbers?

    // Queue.java
    // demonstrates queue
    // to run this program: C>java QueueApp
    //////////////////////////////////////...
    class Queue
    {
    private int maxSize;
    private long[] queArray;
    private int front;
    private int rear;
    private int nItems;
    //------------------------------------...
    public Queue(int s) // constructor
    {
    maxSize = s;
    queArray = new long[maxSize];
    front = 0;
    rear = -1;
    nItems = 0;
    }
    //------------------------------------...
    public void insert(long j) // put item at rear of queue
    {
    if(rear == maxSize-1) // deal with wraparound
    rear = -1;
    queArray[++rear] = j; // increment rear and insert
    nItems++; // one more item
    }
    //------------------------------------...
    public long remove() // take item from front of queue
    {
    long temp = queArray[front++]; // get value and incr front
    if(front == maxSize) // deal with wraparound
    front = 0;
    nItems--; // one less item
    return temp;
    }
    //------------------------------------...
    public long peekFront() // peek at front of queue
    {
    return queArray[front];
    }
    //------------------------------------...
    public boolean isEmpty() // true if queue is empty
    {
    return (nItems==0);
    }
    //------------------------------------...
    public boolean isFull() // true if queue is full
    {
    return (nItems==maxSize);
    }
    //------------------------------------...
    public int size() // number of items in queue
    {
    return nItems;
    }
    //------------------------------------...
    } // end class Queue
    //////////////////////////////////////...
    import java.util.Scanner;
    class QueueApp
    {
    public static void main(String[] args)
    {
    Queue theQueue = new Queue(5); // queue holds 5 items
     
    Scanner scannerObject = new Scanner(System.in);
     
    System.out.println("Enter 10 numbers");
     
    double[] score = new double[7];
     
    theQueue.insert() = scannerObject.nextInt(); 
     
    System.out.println("You entered " + x);
     
    theQueue.insert(10); // insert 4 items
    theQueue.insert(20);
    theQueue.insert(30);
    theQueue.insert(40);
     
    theQueue.remove(); // remove 3 items
    theQueue.remove(); // (10, 20, 30)
    theQueue.remove();
     
    theQueue.insert(50); // insert 4 more items
    theQueue.insert(60); // (wraps around)
    theQueue.insert(70);
    theQueue.insert(80);
     
    while( !theQueue.isEmpty() ) // remove and display
    { // all items
    long n = theQueue.remove(); // (40, 50, 60, 70, 80)
    System.out.print(n);
    System.out.print(" ");
    }
    System.out.println("");
    } // end main()
    } // end class QueueApp


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Fill in the queue from the keyboard help

    theQueue.insert(scannerObject.nextInt());

    Regards,
    Chris

Similar Threads

  1. How to create a system wide mouse or keyboard hook?
    By Freaky Chris in forum Java Native Interface
    Replies: 17
    Last Post: June 17th, 2009, 01:06 PM
  2. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM
  3. How can i control keyboard through programming?
    By Mohd in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2009, 07:10 AM