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

Thread: unable to add my array(Words[]) to PriorityQueue

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post unable to add my array(Words[]) to PriorityQueue

    import java.util.*;

    public class PriorityCustom {

    public static void main(String[] args) {

    String[] words = {"apple", "crowd", "snow", "place", "snap", "zoo", "zing", "smile", "motto", "simple", "elegant","zebra"};

    PriorityQueue<String> stringQueue = new PriorityQueue<String>(20,new Comparator<String>() {
    String Z = "z";
    String S = "s";
    //@Override

    public int compare(String S1,String S2) {

    if (S1.startsWith(Z)) {
    return +3;
    }
    else if (S2.startsWith(S)) {
    return -1;
    }
    else {
    return 3;
    }
    }
    });


    for (int i = 0; i < 20; i++)
    PriorityQueues.add(String[] words);

    while(!stringQueue.isEmpty)
    System.out.println(stringQueue.remove());

    }
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: unable to add my array(Words[]) to PriorityQueue

    1. Use code tags to wrap the code in tags and if you don't know read FAQ.
    2. What do you want to do?
    3. What is the error/exception when you try to do this?
    4. Paste here the full error/exception trace.

    NOTE: READ THE GOD DAMN FORUMS RULES BEFORE POSTING

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: unable to add my array(Words[]) to PriorityQueue

    when i call my "remove()" method
    I want my Priority Queue to first give out the words starting with 's' then 'z' and then the rest.

    these are the errors I got when I tried to complile

    PriorityCustom.java:30: ')' expected
    stringQueue.add(String[] words);
    ^
    PriorityCustom.java:30: illegal start of expression
    stringQueue.add(String[] words);

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: unable to add my array(Words[]) to PriorityQueue

    for (int i = 0; i < 20; i++)
    PriorityQueues.add(String[] words);
     
    while(!stringQueue.isEmpty)
    System.out.println(stringQueue.remove());
    You must look the statement under for loop and the function you want to call in while condition. A function must have () .
    While PriorityQueues is nothing in knowledge of compiler. And why do you want to add the array of words (empty though) everytime in so called PriorityQueues?
    Also, add() expects String not the array of String.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: unable to add my array(Words[]) to PriorityQueue

    I did correct the code

    I separates words starting with S and Z from other but does not separate words staring with S and Z

    as mentioned above, i was to remove wile displaying, first the words starting with S then Z and then the rest ...

    //Hope to find some help//

    import java.util.*;

    public class PriorityCustom {


    public static void main(String[] args) throws Exception {

    //String[] words = {"apple", "crowd", "snow", "place", "snap", "zoo", "zing", "smile", "motto", "simple", "elegant","zebra"};


    PriorityQueue<String> stringQueue = new PriorityQueue<String>(20,new Comparator<String>() {


    String Z = "z";
    String S = "s";
    //@Override

    public int compare(String S1,String S2) {

    if (S1.startsWith(S)) {
    //System.out.println("if1" +S1.startsWith(S));
    return -1;

    }
    if (S1.startsWith(Z)) {
    //System.out.println("if2" +S2.startsWith(Z));
    return 0;
    }
    else {
    return +1;
    }
    }

    });

    stringQueue.add("apple");
    stringQueue.add("simple");
    stringQueue.add("snap");
    stringQueue.add("zing");
    stringQueue.add("elegant");
    stringQueue.add("crowd");
    stringQueue.add("snow");
    stringQueue.add("place");
    stringQueue.add("smile");
    stringQueue.add("motto");
    stringQueue.add("zebra");
    stringQueue.add("zoo");


    while(!stringQueue.isEmpty())
    System.out.println(stringQueue.remove());

    }
    }

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: unable to add my array(Words[]) to PriorityQueue

    PriorityQueue auto sorts the data it holds. You must read it again for what you need or explain in a precise way. Atleast i am unable to understand what you mean to ask/say...

Similar Threads

  1. Replies: 2
    Last Post: September 8th, 2011, 09:50 PM
  2. Highlighted words Not resolved
    By Rajiv in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 4th, 2011, 07:28 AM
  3. HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 3rd, 2011, 03:44 PM
  4. [SOLVED] implementing a dictionary of words using an array of linked lists
    By McTown in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 02:19 PM
  5. Help: Num to words
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2010, 06:55 PM

Tags for this Thread