Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

ajprincess

  • 2 years ago

Please help:) Can anyone please check the error in this code?

  • This Question is Closed
  1. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    import java.io.*; class Queue2 { private int maxSize; private int[] queueArr; private int front; private int rear; public Queue2(int s) { maxSize=s; queueArr=new int[maxSize]; front=0; rear=-1; } public void insert(int j) { if(rear==maxSize-1) rear=-1; queueArr[++rear]=j; } public int remove() { int temp=queueArr[front++]; if(front==maxSize) front=0; return temp; } public int peekFront() { return queueArr[front]; } public boolean isEmpty() { return (maxSize-1+front==rear); } public boolean isFull() { return (maxSize-2+front==rear); } } class QueueApp2 { public static void main(String [] args) throws IOException { BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in)); int a; int b; int c; String d; do { System.out.print("Enter the number of items to be inserted:"); a=Integer.parseInt(keyboard.readLine()); Queue theQueue=new Queue(a+1); for(int i=1;i<=a;i++) { System.out.print("Enter the item:"); b=Integer.parseInt(keyboard.readLine()); theQueue.insert(b); } System.out.print("Enter the number of items to be removed:"); c=Integer.parseInt(keyboard.readLine()); for(int k=1;k<=c;k++) { theQueue.remove(); } while(!theQueue.isEmpty()) { int n=theQueue.remove(); System.out.print(n); System.out.print(" "); } System.out.println(" "); System.out.print("Do you want to continue:"); d=keyboard.readLine(); } while(d!="Quit"); } }

  2. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it compiled for me. i got both the class files... could u tell me how to run, il test.. . .

  3. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I saved this as Queue2.java. javac Queue2.java-for compiling java QueueApp2-to run

  4. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    morpheus/users/durgat $ /tools/cfr/bin/java QueueApp2 Enter the number of items to be inserted:4 Enter the item:a Exception in thread "main" java.lang.NumberFormatException: For input string: "a" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.parseInt(Integer.java:497) at QueueApp2.main(QueueApp2.java:21)

  5. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it throws that... u seeing the same ?

  6. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    its running fine now.... looks u declared the items as integers.. .

  7. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    morpheus/users/durgat $ /tools/cfr/bin/java QueueApp2 Enter the number of items to be inserted:5 Enter the item:1 Enter the item:2 Enter the item:3 Enter the item:4 Enter the item:5 Enter the number of items to be removed:

  8. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ya. it asks whether we want to continue. if we input Quit it shud terminate bt it keeps on repeating. that's the problem I am havng in it.

  9. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yea i see its looping foreverin that last while loop seems !theQueue.isEmpty() is never becoming false - the fifo is never becoming empty. looks somthing worong with isEmpty method

  10. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    return (maxSize-1+front==rear) here we testing if the front and rear are equal, if they're equal, the fifo is empty, right ?

  11. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ya

  12. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    import java.io.*; class Queue2 { private int maxSize; private int[] queueArr; private int front; private int rear; public Queue2(int s) { maxSize=s; queueArr=new int[maxSize]; front=0; rear=-1; } public void insert(int j) { if(rear==maxSize-1) rear=-1; queueArr[++rear]=j; } public int remove() { int temp=queueArr[front++]; if(front==maxSize) front=0; return temp; } public int peekFront() { return queueArr[front]; } public boolean isEmpty() { return (maxSize-1+front==rear); } public boolean isFull() { return (maxSize-2+front==rear); } } class QueueApp2 { public static void main(String [] args) throws IOException { BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in)); int a; int b; int c; int d; do { System.out.print("Enter the number of items to be inserted:"); a=Integer.parseInt(keyboard.readLine()); Queue theQueue=new Queue(a+1); for(int i=1;i<=a;i++) { System.out.print("Enter the item:"); b=Integer.parseInt(keyboard.readLine()); theQueue.insert(b); } System.out.print("Enter the number of items to be removed:"); c=Integer.parseInt(keyboard.readLine()); for(int k=1;k<=c;k++) { theQueue.remove(); } while(!theQueue.isEmpty()) { int n=theQueue.remove(); System.out.print(n); System.out.print(" "); } System.out.println(" "); System.out.print("Enter 0 to exit:"); d=Integer.parseInt(keyboard.readLine()); } while(d!=0); } } if u run this code and enter 0 when it says to enter 0 to exit the program the program terminates. bt if we enter different number the program repeats. this is vat I want

  13. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    its looping forever while removing items itself !

  14. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    bt I dnt get that output when I declare d as string and do:(

  15. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    hey could u tell me how this checks if the queue is empty : public boolean isEmpty() { return (maxSize-1+front==rear); }

  16. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i think the correct method should be as below : public boolean isEmpty() { return (front-1 == rear); }

  17. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    that maxSize over there in ur method is throwing me off... im not able to get what its doing there.. .

  18. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    public boolean isEmpty() { return ((maxSize-1)+front==rear);//or (front==rear+1); }

  19. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    well maxSize is the size of the queue.

  20. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    how below thing works : (maxSize-1)+front==rear

  21. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    To be honest I am nt so clear with this code.:(

  22. bookworm00981
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I got lost while looking at all those stands of code for a sec

  23. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @Chris2332 please help me:)

  24. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @eSpeX please help me:)

  25. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    This is the output I get when i run the second code. Enter the number of items to be inserted:5 Enter the item:12 Enter the item:13 Enter the item:14 Enter the item:15 Enter the item:16 Enter the number of items to be removed:2 14 15 16 Enter 0 to exit:0 The program does not repeat again This is the output I get when i run the second code. Enter the number of items to be inserted:5 Enter the item:12 Enter the item:13 Enter the item:14 Enter the item:15 Enter the item:16 Enter the number of items to be removed:2 14 15 16 Enter 0 to exit:5 Enter the number of items to be inserted:5 Enter the item:17 Enter the item:18 Enter the item:19 Enter the item:20 Enter the item:21 Enter the number of items to be removed:2 19 20 21 Enter 0 to exit:0

  26. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    looks its working fine now. what u changed

  27. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I declared d as int. that's the only difference I made

  28. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I want the same output when I declare d as char or string.

  29. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ha.. did u try using .equals() for comparinng "Quit"

  30. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    no. how do we use it?

  31. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    could u paste the complete code u using in a txt file, so that i test it at my end. and if it works il get back ?

  32. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    tha java source code file... pls attach the file..

  33. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    sure:)

  34. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    also if u have teamviewer we can do that..

  35. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    But I do not have it:(

    1 Attachment
  36. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    /tools/cfr/bin/javac Queue2.java Queue2.java:55: cannot find symbol symbol : class Queue location: class QueueApp2 Queue theQueue=new Queue(a+1); ^ Queue2.java:55: cannot find symbol symbol : class Queue location: class QueueApp2 Queue theQueue=new Queue(a+1); ^ 2 errors

  37. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    im getting errors when compiling ur code as-it-is

  38. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    pls check once.. and attach the code which worked/compiled for u

  39. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    That's the code worked for me. I am using jdk1.7.0_07 and u?

  40. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Queue2 theQueue=new Queue2(a+1); I thnk it will work fine when it is edited as above in urs.

  41. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thats right, i did the same yesterday.

  42. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so other than that, the code which u using is same as the file u just posted ?

  43. hartnn
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    try this string d= "Quit" ........ while (! equalsIgnoreCase(String d))

  44. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    when I edit it nd compile in mine it does nt show the correct output. ya the same code

  45. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    huh it repeats during while loop itself. pls check the file u attached again

  46. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ya it is the same. vat is the output u r getting?

  47. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    durgat/ajpr/v2 $ /tools/cfr/bin/java QueueApp2 Enter the number of items to be inserted:5 Enter the item:1 Enter the item:2 Enter the item:3 Enter the item:4 Enter the item:5 Enter the number of items to be removed:2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3

  48. ganeshie8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it is repeating forever during removing stage itself.

  49. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    oh I c:(

  50. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @ganeshie8 I will include the pdf book. in that book in page number 111 u will find that code.

  51. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    If you are trying to replicate the code in the book then you do have several omissions. One is when they declare the array 1 cell larger than maxSize to work around an anomaly where the queue can appear full and empty at the same time. There are also some discrepancies in your isEmpty() and isFull() methods, were these differences intentional?

  52. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    For your while condition, try replacing this: System.out.print("Do you want to continue:"); d=keyboard.readLine(); } while(d!="Quit"); with this: // declare your string String exit = new String("Quit"); // declare your old string String d = new String(); ... System.out.print("Do you want to continue:"); d=keyboard.readLine(); } while(!d.equalsIgnoreCase(exit));

  53. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Wowza this is long.

  54. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    They wanted us to declare the array size 1 greater than the number of items to be inserted. System.out.print("Enter the number of items to be inserted:"); a=Integer.parseInt(keyboard.readLine()); Queue theQueue=new Queue(a+1); Also I really dnt get the conditions of isFull() nd isEmpty() methods. i jst included the one my instructor gave us.:( If u dnt mind will u be able to explain me that @eSpeX? after changing it in the way u suggested it worked fine. thank u soooooooooooo much.

  55. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    The logic in the isFull() basically says that if your rear pointer is pointing to the index of maxSize (plus two will put it in index[0]), or your front is pointing at index 0, the array is full. Similarly the isEmpty() looks to test if your rear pointer and your front pointer are both pointing to the location of maxSize - 1 as an indication the array is empty.

  56. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Reallyyy sorrry for troubling you. I still dont get it. If u dnt mind can u please explain it a bit further. maybe with an example of maxSize.

  57. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @ganeshie8 try running this one

    1 Attachment
  58. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Let's say your max size is 10 because you had items 's=9' and you're creating your array to 's+1', then you have an index of 0 - 9. isEmpty() tests the conditions: (front==rear+1)||(front+maxSize-1==rear), so your initial conditions are front = 0 rear = -1 So, if front == rear + 1 you have 'if 0 == -1+1', this means that both of your pointers are now looking at the beginning of the array. Similarly, if after your operations your rear is pointing at the last location (9 in this example) and your front is pointing to maxSize-1 then you have 0+(10-1)==9, because your maxSize is s+1. Through similar logic you can examine your isFull() method. (front==rear+2)||(front+maxSize-2==rear) the logic is applied after you insert items into the array, so if your front = 0 and your rear has reached the end of the items, 8, then the logic tests to see if 0 == 8 + 2, the counter will loop on itself first pointing at index 9 and then at index 0. The other direction is also true, if your rear pointer is on index 8, then front+maxSize-2==rear translates to 0+(10-2)==8

  59. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank u sooo much. it makes sense nw. u r the best:)

  60. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.