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 15 of 15

Thread: Trouble loading elements into a queue

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Trouble loading elements into a queue

    Hey guys ,
    I am working on a print queue read from a file but I can only read the input file and parse it, I don't know how to load the elements from the input to the queue.
    The file input looks like this :q,5,8

    	if (line.charAt(0) == 'q')
    			{
    			String A = line.substring(0,line.indexOf(","));
    			String B = line.substring(line.indexOf(",")+1,line.lastIndexOf(','));
    			String C = line.substring(line.lastIndexOf(',')+1,line.length());
     
     
     
    jobInfo.setPrintJobInfo(A,Integer.parseInt(B),Integer.parseInt(C))mainQueue.addQueue(jobInfo);
     
     
    public static void setPrintJobInfo(String A,int B,int C)
    {
    		type = A;
    		job = B;
    		cycle = C;
    }
    Do any of you guys have an idea of what i need to do ?
    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble loading elements into a queue

    how to load the elements from the input to the queue.
    What variables is the input in?
    What does "load" mean?
    What are the definitions of the variables: jobInfo and the mainQueue?

    What does this statement do:
    jobInfo.setPrintJobInfo(A, Integer.parseInt(B), Integer.parseInt(C))mainQueue.addQueue(jobInfo);
    Does it compile?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble loading elements into a queue

    What variables is the input in? A is suppose to be a string , both B and c are integers
    What does "load" mean? I mean I want to pass each variables

    What are the definitions of the variables:

    jobInfo is located in the queueclass

     public void addQueue(T queueElement)
                                throws QueueOverflowException
        {
            if (isFullQueue())
                throw new QueueOverflowException();
     
            queueRear = (queueRear + 1) % maxQueueSize; //use the
                                //mod operator to advance queueRear
                                //because the array is circular
            count++;
            list[queueRear] = queueElement;
        }


    mainQueue is in the testPrintQueue class

    public static QueueClass mainQueue
                     = new QueueClass();

    What does this statement do:
    jobInfo.setPrintJobInfo(A, Integer.parseInt(B), Integer.parseInt(C))mainQueue.addQueue(jobInfo);
    I am trying to pass each variable into the setPrintJobInfo , i know that jobInfo is suppose to be like an array but I don't know how to

    public static void setPrintJobInfo(String A,int B,int C)
    {
    		type = A;
    		job = B;
    		cycle = C;
    }
    type of the print which is q and is aString
    job number = 5 and is an int
    the number of cycle which is 8 and is an int

    It does compile and print out the readings from the file when i have a system.out.println();
    q
    5
    8

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble loading elements into a queue

    jobInfo.setPrintJobInfo(A,Integer.parseInt(B),Inte ger.parseInt(C))mainQueue.addQueue(jobInfo);
    That does not look like good syntax. I don't think the ))ma would compile.

    No way to make sense of this without a complete program that compiles and executes.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble loading elements into a queue

    Here is the test file
    public class testPrintQueue
    	{
    	public static QueueClass mainQueue
                     = new QueueClass();
    	public static QueueClass addQueuePriority
                     = new QueueClass();
     
    	static String type = "a";
    	static int job = 0;
    	static int cycle = 0;
     
    	public static void readFile(String line)
    		{
     
    		if (line.charAt(0) == 'q')
    			{
    			String A = line.substring(0,line.indexOf(","));
    			String B = line.substring(line.indexOf(",")+1,line.lastIndexOf(','));
    			String C = line.substring(line.lastIndexOf(',')+1,line.length());
     
    		jobInfo.setPrintJobInfo(A,Integer.parseInt(B),Integer.parseInt(C));
    			mainQueue.addQueue(jobInfo);
    		//	System.out.println();
    			}
     
    	public static void setPrintJobInfo(String A,int B,int C)
    	{
    		type = A;
    		job = B;
    		cycle = C;
    	}
     
    	public void printJob()
    	{
    		setPrintJobInfo("a",0,0);
    	}
     
    	public void printJob(String type,int job,int cycle)
    	{
    		setPrintJobInfo(type,job,cycle);
    	}
     
    	public int getCycle()
    	{
    		return cycle;
    	}
     
    	public void incrementCycle()
    	{
    		cycle++;
    	}
     
     
    	public static void main(String[] args)
    						throws FileNotFoundException, IOException
    		{
    		int count = 0;
    		int cjob = 0;
    		int ccycle = 0;
    		String clet = "a";
    		Scanner input = new Scanner(new File("C://QueueIn.txt"));
     
    		while(input.hasNext())
    			{
    			readFile(input.nextLine());
    			}
     
     
    		}
    	}

    and the queueClass
     
    public class QueueClass<T> implements QueueADT<T>
    {
        private int maxQueueSize;
        private int count;
        private int queueFront;
        private int queueRear;
        private T[] list;  //Array of references to the
                           //objects that store queue elements
     
           //Default constructor
           //Postcondition: Creates the array of references to the
           //               objects that store queue elements.
           //               maxQueueSize = 100;
           //               count = 0; queueFront = 0;
           //               queueRear = maxQueueSize - 1;
        public QueueClass()
        {
            maxQueueSize = 100;
     
            queueFront = 0;                 //initialize queueFront
            queueRear = maxQueueSize - 1;   //initialize queueRear
            count = 0;
            list = (T[]) new Object[maxQueueSize];  //create the
                                    //array to implement the queue
        }
     
           //Constructor with a parameter
           //Postcondition: Creates the array of references to the
           //               objects that store queue elements.
           //               maxQueueSize = queueSize;
           //               count = 0; queueFront = 0;
           //               queueRear = maxQueueSize - 1;
           //               If queueSize <= 0, maxQueueSize = 100;
        public QueueClass(int queueSize)
        {
            if (queueSize <= 0)
            {
                System.err.println("The size of the array to "
                                 + "implement the queue must be "
                                 + "positive.");
                System.err.println("Creating an array of the "
                                 + "size 100.");
     
                maxQueueSize = 100;
            }
            else
                maxQueueSize = queueSize; //set maxQueueSize to
                                          //queueSize
     
            queueFront = 0;                 //initialize queueFront
            queueRear = maxQueueSize - 1;   //initialize queueRear
            count = 0;
            list = (T[]) new Object[maxQueueSize]; //create the
                                    //array to implement the queue
        }
     
           //Method to initialize the queue to an empty state.
           //Postcondition: count = 0; queueFront = 0;
           //               queueRear = maxQueueSize - 1
        public void initializeQueue()
        {
            for (int i = queueFront; i < queueRear;
                                     i = (i + 1) % maxQueueSize)
            list[i] = null;
     
            queueFront = 0;
            queueRear = maxQueueSize - 1;
            count = 0;
        }
     
           //Method to determine whether the queue is empty.
           //Postcondition: Returns true if the queue is empty;
           //               otherwise, returns false.
        public boolean isEmptyQueue()
        {
            return (count == 0);
        }
     
           //Method to determine whether the queue is full.
           //Postcondition: Returns true if the queue is full;
           //               otherwise, returns false.
        public boolean isFullQueue()
        {
            return (count == maxQueueSize);
        }
     
           //Method to return the first element of the queue.
           //Precondition: The queue exists and is not empty.
           //Postcondition: If the queue is empty, the method
           //               throws QueueUnderflowException;
           //               otherwise, a reference to a copy of
           //               the first element of the queue
           //               is returned.
        public T front() throws QueueUnderflowException
        {
            if (isEmptyQueue())
                throw new QueueUnderflowException();
     
            return (T) list[queueFront];
        }
     
           //Method to return the last element of the queue.
           //Precondition: The queue exists and is not empty.
           //Postcondition: If the queue is empty, the method
           //               throws QueueUnderflowException;
           //               otherwise, a reference to a copy
           //               of the last element of the queue
           //              is returned.
        public T back() throws QueueUnderflowException
        {
            if (isEmptyQueue())
                throw new QueueUnderflowException();
     
            return (T) list[queueRear];
        }
     
           //Method to add queueElement to the queue.
           //Precondition: The queue exists and is not full.
           //Postcondition: The queue is changed and queueElement
           //               is added to the queue.
        public void addQueue(T queueElement)
                                throws QueueOverflowException
        {
            if (isFullQueue())
                throw new QueueOverflowException();
     
            queueRear = (queueRear + 1) % maxQueueSize; //use the
                                //mod operator to advance queueRear
                                //because the array is circular
            count++;
            list[queueRear] = queueElement;
        }
     
     
           //Method to remove the first element of the queue.
           //Precondition: The queue exists and is not empty.
           //Postcondition: The queue is changed and the first
           //               element is removed from the queue.
        public void deleteQueue() throws QueueUnderflowException
        {
            if (isEmptyQueue())
                throw new QueueUnderflowException();
     
            count--;
            list[queueFront] = null;
            queueFront = (queueFront + 1) % maxQueueSize; //use the
                               //mod operator to advance queueFront
                               //because the array is circular
        }
     
    }
    I am trying to Implement a virtual print queue. A single print queue is servicing a single printer.
    q,5,8 is a q type record which indicates that a print job, # 5, shall be placed at the rear of the queue and print for 8 cycles when it is removed from the front of the queue and prints.
    I think i got everything done but i don't know how to pass each elements after I input it from the file
    Thanks

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble loading elements into a queue

    The code does not compile. Its missing the import statements
    and implements QueueADT<T>
    Last edited by Norm; April 17th, 2012 at 01:39 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble loading elements into a queue

    Sorry lol
    import java.io.*;
    import java.util.*;
    import java.lang.*;

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble loading elements into a queue

    Also missing many class definitions
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble loading elements into a queue

    I have attached the folder where i have everything stored and thanks again for helping me
    Attached Files Attached Files

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble loading elements into a queue

    What's in the QueueIn.txt file?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble loading elements into a queue

    The code in the zip file does NOT compile!!!!

    Running: F:\Java\jdk1.6.0_29\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;.;..\. testPrintQueue.java

    testPrintQueue.java:30: illegal start of expression
    public static void setPrintJobInfo(String A,int B,int C)
    ^
    testPrintQueue.java:30: illegal start of expression
    public static void setPrintJobInfo(String A,int B,int C)
    ^
    testPrintQueue.java:30: ';' expected
    public static void setPrintJobInfo(String A,int B,int C)
    ^
    testPrintQueue.java:30: ')' expected
    public static void setPrintJobInfo(String A,int B,int C)
    ^
    testPrintQueue.java:30: illegal start of expression
    public static void setPrintJobInfo(String A,int B,int C)
    ^
    testPrintQueue.java:30: ';' expected
    public static void setPrintJobInfo(String A,int B,int C)
    ^
    testPrintQueue.java:30: not a statement
    public static void setPrintJobInfo(String A,int B,int C)
    ^
    testPrintQueue.java:30: ';' expected
    public static void setPrintJobInfo(String A,int B,int C)
    ^
    testPrintQueue.java:30: ';' expected
    public static void setPrintJobInfo(String A,int B,int C)
    ^
    testPrintQueue.java:37: illegal start of expression
    public void printJob()
    ^
    testPrintQueue.java:37: illegal start of expression
    public void printJob()
    ^
    testPrintQueue.java:37: ';' expected
    public void printJob()
    ^
    testPrintQueue.java:42: illegal start of expression
    public void printJob(String type,int job,int cycle)
    ^
    testPrintQueue.java:42: illegal start of expression
    public void printJob(String type,int job,int cycle)
    ^
    testPrintQueue.java:42: ';' expected
    public void printJob(String type,int job,int cycle)
    ^
    testPrintQueue.java:42: <identifier> expected
    public void printJob(String type,int job,int cycle)
    ^
    testPrintQueue.java:42: not a statement
    public void printJob(String type,int job,int cycle)
    ^
    testPrintQueue.java:42: ';' expected
    public void printJob(String type,int job,int cycle)
    ^
    testPrintQueue.java:42: ';' expected
    public void printJob(String type,int job,int cycle)
    ^
    testPrintQueue.java:47: illegal start of expression
    public int getCycle()
    ^
    testPrintQueue.java:47: ';' expected
    public int getCycle()
    ^
    testPrintQueue.java:52: illegal start of expression
    public void incrementCycle()
    ^
    testPrintQueue.java:52: illegal start of expression
    public void incrementCycle()
    ^
    testPrintQueue.java:52: ';' expected
    public void incrementCycle()
    ^
    testPrintQueue.java:58: illegal start of expression
    public static void main(String[] args)
    ^
    testPrintQueue.java:58: illegal start of expression
    public static void main(String[] args)
    ^
    testPrintQueue.java:58: ';' expected
    public static void main(String[] args)
    ^
    testPrintQueue.java:58: '.class' expected
    public static void main(String[] args)
    ^
    testPrintQueue.java:58: ';' expected
    public static void main(String[] args)
    ^
    testPrintQueue.java:59: illegal start of expression
    throws FileNotFoundException, IOException
    ^
    testPrintQueue.java:59: ';' expected
    throws FileNotFoundException, IOException
    ^
    testPrintQueue.java:74: reached end of file while parsing
    }
    31 error(s)
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble loading elements into a queue

    the error is from this line
    jobInfo.setPrintJobInfo(A,Integer.parseInt(B),Inte ger.parseInt(C));
    mainQueue.addQueue(jobInfo);
    that's why i have a system.out.println(); to see it the parsing is actually working , now i am trying to pass each variables into the setPrintjobInfo method
    type is the q , job is 5 and cycle is 8

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble loading elements into a queue

    Please fix the compiler errors before posting the source code, unless you are getting compiler errors.
    If you are getting compiler errors, post them and the source causing the errors.

    Don't post code that does not compile without explaining ALL the compiler problems with the posted code.

    I'll wait until you have a source that will compile without errors and can be executed to show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble loading elements into a queue

    Alright I made some change to the testprintqueue :
    import java.io.*;
    import java.util.*;
    import java.lang.*;
     
    public class testPrintQueue
    	{
    	public static QueueClass mainQueue
                     = new QueueClass();
    	public static QueueClass addQueuePriority
                     = new QueueClass();
     
    	static String type = "a";
    	static int job = 0;
    	static int cycle = 0;
     
    	public static void readFile(String line)
    		{
     
    		if (line.charAt(0) == 'q')
    			{
    			String A = line.substring(0,line.indexOf(","));
    			String B = line.substring(line.indexOf(",")+1,line.lastIndexOf(','));
    			String C = line.substring(line.lastIndexOf(',')+1,line.length());
     
    			System.out.println(A);
    			System.out.println(Integer.parseInt(B));
    			System.out.println(Integer.parseInt(C));
    			}
    		}
     
    	public static void setPrintJobInfo(String A,int B,int C)
    	{
    		type = A;
    		job = B;
    		cycle = C;
     
    	}
     
    	public void printJob()
    	{
    		setPrintJobInfo("a",0,0);
    	}
     
    	public void printJob(String type,int job,int cycle)
    	{
    		setPrintJobInfo(type,job,cycle);
    	}
     
    	public int getCycle()
    	{
    		return cycle;
    	}
     
    	public void incrementCycle()
    	{
    		cycle++;
    	}
     
     
    	public static void main(String[] args)
    						throws FileNotFoundException, IOException
    		{
    		int count = 0;
    		int cjob = 0;
    		int ccycle = 0;
    		String clet = "a";
    		Scanner input = new Scanner(new File("C://QueueIn.txt"));
     
    		while(input.hasNext())
    			{
    			readFile(input.nextLine());
     
    			}
     
     
    		}
    	}
    I can print individually all the string(A,B,C) from the input file now I want to pass those strings as element to the setPrintJobInfo method .

    but i get a "Picked up _JAVA_OPTIONS: -Xmx1g"error . The whole point of this program is to act like a printer so each print job will decrement the number of remaining cycles I don't know if you get what i am trying to do i know i seems confusing
    This what the input text file look like :
    q,1,8
    Output :
    q
    1
    8
    Picked up _JAVA_OPTIONS: -Xmx1g

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble loading elements into a queue

    "Picked up _JAVA_OPTIONS: -Xmx1g"error
    Is that from the java command options when you execute the program?

    I want to pass those strings as element to the setPrintJobInfo method
    Wrap them in a class and pass that or pass them as individual args to the method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  2. Trouble adding exceptions to fixed queue class
    By Farmer in forum Exceptions
    Replies: 5
    Last Post: December 19th, 2011, 07:23 AM
  3. Priority Queue help
    By BuhRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 3rd, 2011, 06:37 PM
  4. queue question
    By Herah in forum Object Oriented Programming
    Replies: 2
    Last Post: November 3rd, 2011, 06:04 AM
  5. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM