Java help

Discussion in 'OT Technology' started by mrburner, Mar 7, 2003.

  1. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    if anyone is good at java, i could really use some help tonight

    i'm working with hashmap's and i need to print all the items in the hashmap by using a keyset method and i am completely lost...

    please im me at "AZMaxima" or reply here...would be much appreciated
     
  2. RaginBajin

    RaginBajin Have you punched a donkey today?

    Joined:
    Dec 24, 2001
    Messages:
    8,740
    Likes Received:
    0
    Location:
    NoVA
    Why don't you post some code and questions and stuff?

    That way we can all learn ;)
     
  3. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Post up some code. I haven't done much with hashmaps, but I'd be glad to learn. :bigthumb:
     
  4. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    ok here is the code

    import java.util.HashSet;
    import java.util.Set;
    import java.util.HashMap;
    import java.util.Iterator;

    /**
    * This class implements a Concordance. It is the top level class in this
    * project. The Concordance communicates via text input/output in the text terminal.
    * A Condordance holds a <word><ConcordanceValue> pair.
    *
    * This class uses an object of class ConcordanceValue as the
    * <value> stored in the Concordance. A Concordance consists of a list of
    * line numbers and a count of the line numbers.
    *
    * This class uses an object of class InputReader to read input from the user
    * until all text is consummed.
    *
    * It contains a loop that repeatedly reads input and places each word in a
    * concordance. The concordance is then printed to the screen.
    *
    * @version 2/28/03
    * @author Zach Leeds
    */
    public class Concordance
    {
    //reads text from keyboard
    private InputReader reader;

    //holds <word> <lines> for concordance
    private HashMap concord;

    /**
    * Creates a concordance.
    */
    public Concordance()
    {
    reader = new InputReader();

    concord = new HashMap();
    }

    /**
    * Start the concordance loop. This will print a directions message and read
    * text entered by the user, until the user exits. Each word is added to a
    * concordance. The concordance is printed when the user exits.
    */
    public void start()
    {
    boolean finished = false;
    int lineNumber = 1;
    printDirections();

    while(!finished) {

    //read the next line of text, individual words of text
    //placed in Set input object
    HashSet input = reader.getInput();

    if(input.contains("exit")) {
    finished = true;
    }
    else {
    //add words to concordance
    add(input, lineNumber);

    }

    lineNumber++;
    }
    printDone();
    printConcordance();
    concord.clear();
    }

    /**
    * Print directions for entering test lines to the screen.
    */
    private void printDirections()
    {
    System.out.println("Please type in your questions and if you would like to exit, type 'exit' at any time.");
    }

    /**
    * Print a good-bye message to the screen.
    */
    private void printDone()
    {
    System.out.println("Thank you for using this automated technical support system. Good bye.");
    }

    /**
    * Print concordance to the screen.
    */
    private void printConcordance()
    {

    }

    /**
    * Add words to concordance.
    */
    private void add(HashSet words, int lineNumber) //method to add word to concordance if the word isn't in it or to update line number if the word is in the concordance
    {
    Iterator it = words.iterator();

    while(it.hasNext()) //begins while loop
    {
    String bob = (String)it.next();
    ConcordanceValue i = (ConcordanceValue)concord.get(bob); //setting the concordance value equal to another concordance value rather than a string or integer

    if(i==null) //this says if i is not true
    {
    concord.put(i, lineNumber); //add concordance value to hashmap

    }
    else //if i is not true
    {
    i.addLine(String.valueOf(lineNumber)); //updates the line number
    }
    }

    }
    }




    problems: towards the end within the while loop, the if statement does not work...i need to put the new object into the hasmap but for some reason it keeps highlighting the line concord.put(i, lineNumber); and says incompatable types or something of the like

    and here is the main problem i'm havign
    5. The last problem to address is printing the Concordance in the printConcordance() method. HashMaps must be accessed with a key but we don't know all the keys stored in the Map! So we need to use the keySet() method of the HashMap class. This method returns a Set of all keys stored in the HashMap. Once we have the key Set we can use an Iterator to grab each key String and use that to grab the corresponding value (Concordance Value object) from the Map. The printConcordance() method should print each unique word in the concordance along with the line numbers (these can be printed using the printConcordanceValue() method of the ConcordanceValue class) where it appears. Format this output in a readable form.

    anyone? all help is appreciated
     
  5. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    what if you sell your car? you'll have to rename your name to AZyourcarbrand
     
  6. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    there are 2 other classes...the ConcordanceValue class

    code:

    import java.util.ArrayList;
    import java.util.Iterator;

    /**
    * A ConcordanceValue object will serve as the <value>
    * in the <key><value> pair of the Concordance class
    * A ConcordanceValue holds a list of line numbers for
    * a word along with a frequency count of the line numbers
    *
    * @author Denise Byrnes
    * @version 2/21/03
    */
    public class ConcordanceValue
    {
    //Holds a list of line numbers
    private ArrayList lineNumbers;
    //Holds the line count for a word
    private int frequencyCount;
    /**
    * Constructor for objects of class ConcordanceValue
    */
    public ConcordanceValue()
    {
    // initialise instance variables
    lineNumbers = new ArrayList();
    frequencyCount = 0;
    }

    /**
    * Returns a list of line numbers
    *
    * @return an Arraylist of line numbers stored as Strings
    */
    public ArrayList getLineNumbers()
    {
    return lineNumbers;
    }

    /**
    * Adds a line number to the list of numbers
    *
    * @ line String representation of line number
    */
    public void addLine(String line)
    {
    lineNumbers.add(line);
    frequencyCount++;
    }

    /**
    * Prints the line numbers to terminal
    *
    */
    public void print()
    {
    System.out.println("The frequency count = " + frequencyCount + ".");
    Iterator iter = lineNumbers.iterator();
    while(iter.hasNext())
    {
    System.out.print(iter.next() + ", ");
    }
    }

    }


    and the input reader class...code:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
    import java.util.HashSet;

    /**
    * Class InputReader reads typed text input from the standard text terminal. The text
    * typed by a user is then chopped into words, and a set of words is provided.
    *
    * @version 1.0 (21.Jan.2002)
    * @author Michael Kolling and David J. Barnes
    */
    public class InputReader
    {
    private BufferedReader reader;

    /**
    * Create a new InputReader that reads text from the text terminal.
    */
    public InputReader()
    {
    reader = new BufferedReader(new InputStreamReader(System.in));;
    }

    /**
    * Read a line of text from standard input (the text terminal), and
    * return it as a set of words.
    *
    * @return A set of Strings, where each String is one of the words
    * typed by the user
    */
    public HashSet getInput()
    {
    System.out.print("> "); // print prompt
    String inputLine = readInputLine().trim().toLowerCase();

    StringTokenizer tokenizer = new StringTokenizer(inputLine);
    HashSet words = new HashSet();

    while(tokenizer.hasMoreTokens())
    words.add(tokenizer.nextToken()); // add each word to the set

    return words;
    }

    /**
    * Read one line of input and return it as a String.
    *
    * @return A Strings representing the input, or an empty String if an error occurs.
    */
    private String readInputLine()
    {
    String line = "";

    try {
    line = reader.readLine();
    }
    catch(java.io.IOException exc) {
    System.out.println ("There was an error during reading: " + exc.getMessage());
    }
    return line;
    }
    }



    but we only need to modify the Concordance class
     
  7. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    haha yes, i know the screen name is gay/lame/retarded/fill in the blank with any negative word you can think of

    basically the screen name != cool in any way, shape, or form

    i sold my maxima about 2 years ago...just have so many people that know this screen name and it would be such a pain in the ass to switch...i probably will soon though since i feel stupid telling people the screen name...anyways, back on topic
     
  8. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Ok, I fixed your compile error. I'll look over the rest in a few.

    Code:
    if(i==null) //this says if i is not true
    {
    concord.put(i, Integer.toString(lineNumber)); //add concordance value to hashmap
    
    }
    
     
  9. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I haven't really looked through your code yet, but try and think this out first. It sounds like you just need to load up an array with the results of calling keySet(). Then use a for loop with the array.size() as the condition. In your loop, call the printConcordance() method and get your values. I'm not really sure where you are stuck at the moment.
     
  10. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    i'm stuck because we haven't really discussed this much in class and this is our first hashmaps assignment

    basically so far in this class i have a lot of trouble understanding something until i go in for help and i actually have the professor sit there and show me (i am a very visual learner) and then i pretty much understand it

    like i don't get it when its just on paper like above, but when i actually see the code done i understand it completely
     
  11. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Gotcha. I'm a visual learner myself.

    This was the first time I've done anything with HashMaps. I learned a lot tonight myself. Here is what I came up with. This should show you how you can add and extract values. I hope this helps in some way.

    Code:
    import java.util.HashSet;
    import java.util.Set;
    import java.util.HashMap;
    import java.util.Iterator;
    
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */
    
    public class Control {
    
      public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        HashSet hashSet = new HashSet();
        Set set;
    
        hashSet.add("chris");
        hashSet.add("hello");
        hashSet.add("um");
        hashSet.add("next");
        hashSet.add("yeah");
    
        Iterator it = hashSet.iterator();
        int i = 0;
    
        while(it.hasNext()) {
          hashMap.put(Integer.toString(i), (String)it.next());
          i++;
        }
    
        set = hashMap.keySet();
    
        Object[] array = set.toArray();
    
        for(int x = 0; x < array.length; x++) {
          System.out.println("" + x + " " + hashMap.get(Integer.toString(x)));
        }
      }
    }
    
     
  12. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    ok so if this is my code for number 5

    /**
    * Print concordance to the screen.
    */
    private void printConcordance()
    {

    set = concord.keySet();

    Object[] array = set.toArray();

    for(int x = 0; x < array.length; x++)
    {
    System.out.println("" + x + " " + hashMap.get(Integer.toString(x)));
    }

    }

    how do i implement "set"...i know you put "Set set;" but we haven't learned any "Set" command yet

    so for this part, how would i declare set?
     
  13. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    The command:

    Code:
    Set set;
    
    is just creating a reference to the Set class. You are doing the same here:

    Code:
    HashMap hashMap = new HashMap();
    HashSet hashSet = new HashSet();
    
     
  14. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    yeah but in this part

    private void printConcordance() //was slightly confused on this part so i had my friend Adam from home help me with it...just trying to be on the safe side :)
    {

    set = concord.keySet();

    Object[] array = set.toArray();

    for(int x = 0; x < array.length; x++)
    {
    System.out.println("" + x + " " + hashMap.get(Integer.toString(x)));
    }

    }

    in the line

    set = concord.keySet();

    i get a "cannot resolve symbol"...what should i declare set to be? is it a string?
     
  15. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Did you add "Set set;" at the top of your class along with HashMap and HashSet?
     
  16. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    well thats what i'm saying...we never did that in class, she might think its odd that i put that in since we've never covered it...i guess i'll put more comments there about "my friend adam" ;) helping me on that part
     
  17. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    still giving me a cannot resolve symbol error on the set = concord.keySet(); line

    here is my code in its entirity

    import java.util.HashSet;
    import java.util.Set;
    import java.util.HashMap;
    import java.util.Iterator;

    /**
    * This class implements a Concordance. It is the top level class in this
    * project. The Concordance communicates via text input/output in the text terminal.
    * A Condordance holds a <word><ConcordanceValue> pair.
    *
    * This class uses an object of class ConcordanceValue as the
    * <value> stored in the Concordance. A Concordance consists of a list of
    * line numbers and a count of the line numbers.
    *
    * This class uses an object of class InputReader to read input from the user
    * until all text is consummed.
    *
    * It contains a loop that repeatedly reads input and places each word in a
    * concordance. The concordance is then printed to the screen.
    *
    * @version 2/28/03
    * @author Zach Leeds
    */
    public class Concordance
    {
    //reads text from keyboard
    private InputReader reader;

    //holds <word> <lines> for concordance
    private HashMap concord;

    /**
    * Creates a concordance.
    */
    public Concordance()
    {
    reader = new InputReader();
    HashSet hashSet = new HashSet();
    Set set; //adam (explained below) told me to add this in and I will go over this with you after spring break during office hours if thats ok with you
    concord = new HashMap();
    }

    /**
    * Start the concordance loop. This will print a directions message and read
    * text entered by the user, until the user exits. Each word is added to a
    * concordance. The concordance is printed when the user exits.
    */
    public void start()
    {
    boolean finished = false;
    int lineNumber = 1;
    printDirections();

    while(!finished) {

    //read the next line of text, individual words of text
    //placed in Set input object
    HashSet input = reader.getInput();

    if(input.contains("exit")) {
    finished = true;
    }
    else {
    //add words to concordance
    add(input, lineNumber);

    }

    lineNumber++;
    }
    printDone();
    printConcordance();
    concord.clear();
    }

    /**
    * Print directions for entering test lines to the screen.
    */
    private void printDirections()
    {
    System.out.println("Please type in your questions and if you would like to exit, type 'exit' at any time.");
    }

    /**
    * Print a good-bye message to the screen.
    */
    private void printDone()
    {
    System.out.println("Thank you for using this automated technical support system. Good bye.");
    }

    /**
    * Print concordance to the screen.
    */
    private void printConcordance() //was slightly confused on this part so i had my friend Adam from home help me with it...just trying to be on the safe side :)
    {

    set = concord.keySet();

    Object[] array = set.toArray();

    for(int x = 0; x < array.length; x++)
    {
    System.out.println("" + x + " " + hashMap.get(Integer.toString(x)));
    }

    }

    /**
    * Add words to concordance.
    */
    private void add(HashSet words, int lineNumber) //method to add word to concordance if the word isn't in it or to update line number if the word is in the concordance
    {
    Iterator it = words.iterator();

    while(it.hasNext()) //begins while loop
    {
    String bob = (String)it.next();
    ConcordanceValue i = (ConcordanceValue)concord.get(bob); //setting the concordance value equal to another concordance value rather than a string or integer

    if(i==null) //this says if i is not true
    {
    concord.put(i, Integer.toString(lineNumber)); //add concordance value to hashmap


    }
    else //if i is not true
    {
    i.addLine(String.valueOf(lineNumber)); //updates the line number
    }
    }

    }
    }
     
  18. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    CompiledMonkey mentioned this above. You have to first declare the object. You cannot assign an object a value unless Java knows what type the object it.

    Change your line to:

    Set set = concord.keySet();

    Haven't they taught you this in class?
     
  19. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    nope, its just programming 1 right now so we are just starting with java...we haven't learned the Set command...but i talked to my teacher about it today and redid it with her...here is the completed/working/compiled code if anyones interested

    import java.util.HashSet;
    import java.util.Set;
    import java.util.HashMap;
    import java.util.Iterator;

    /**
    * This class implements a Concordance. It is the top level class in this
    * project. The Concordance communicates via text input/output in the text terminal.
    * A Condordance holds a <word><ConcordanceValue> pair.
    *
    * This class uses an object of class ConcordanceValue as the
    * <value> stored in the Concordance. A Concordance consists of a list of
    * line numbers and a count of the line numbers.
    *
    * This class uses an object of class InputReader to read input from the user
    * until all text is consummed.
    *
    * It contains a loop that repeatedly reads input and places each word in a
    * concordance. The concordance is then printed to the screen.
    *
    * @version 2/28/03
    * @author Zach Leeds
    */
    public class Concordance
    {
    //reads text from keyboard
    private InputReader reader;

    //holds <word> <lines> for concordance
    private HashMap concord;

    /**
    * Creates a concordance.
    */
    public Concordance()
    {
    reader = new InputReader();
    concord = new HashMap();
    }

    /**
    * Start the concordance loop. This will print a directions message and read
    * text entered by the user, until the user exits. Each word is added to a
    * concordance. The concordance is printed when the user exits.
    */
    public void start()
    {
    boolean finished = false;
    int lineNumber = 1;
    printDirections();

    while(!finished) {

    //read the next line of text, individual words of text
    //placed in Set input object
    HashSet input = reader.getInput();

    if(input.contains("exit")) {
    finished = true;
    }
    else {
    //add words to concordance
    add(input, lineNumber);

    }

    lineNumber++;
    }
    printDone();
    printConcordance();
    concord.clear();
    }

    /**
    * Print directions for entering test lines to the screen.
    */
    private void printDirections()
    {
    System.out.println("Please type in your questions and if you would like to exit, type 'exit' at any time.");
    }

    /**
    * Print a good-bye message to the screen.
    */
    private void printDone()
    {
    System.out.println("Thank you for using this automated technical support system. Good bye.");
    }

    /**
    * Print concordance to the screen.
    */
    private void printConcordance() //was slightly confused on this part so i had my friend Adam from home help me with it...just trying to be on the safe side :)
    {

    Set set = concord.keySet();

    Iterator iter = set.iterator();
    while(iter.hasNext())
    {
    String wd = (String)iter.next();
    ConcordanceValue temp = (ConcordanceValue)concord.get(wd);
    temp.print();
    }

    }

    /**
    * Add words to concordance.
    */
    private void add(HashSet words, int lineNumber) //method to add word to concordance if the word isn't in it or to update line number if the word is in the concordance
    {
    Iterator it = words.iterator();

    while(it.hasNext()) //begins while loop
    {
    String bob = (String)it.next();
    ConcordanceValue i = (ConcordanceValue)concord.get(bob); //setting the concordance value equal to another concordance value rather than a string or integer

    if(i==null) //this says if i is not true
    {
    concord.put(i, Integer.toString(lineNumber)); //add concordance value to hashmap


    }
    else //if i is not true
    {
    i.addLine(String.valueOf(lineNumber)); //updates the line number
    }
    }

    }
    }
     

Share This Page