Results 1 to 3 of 3

Thread: HashMap Help

  1. #1
    ScaryJello is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default HashMap Help

    in the following program, i need to time some steps. I need to time how long it takes to search, sort, and print. Right now, the program is designed for an ArrayList. What i need to do is make all of these steps happen using a HashMap. Its gonna read the file, take the word, count it, and add it to the list. the print should print the word and the amount of times it appears. can someone help me with this?

    Thank You

    import java.util.*;
    import java.util.Scanner;
    import java.io.FileReader;
    import java.io.PrintWriter;
    import java.io.EOFException;
    import java.io.IOException;
    import java.io.FileNotFoundException;



    public class WordFrequencyAL
    {
    public static void main(String[] args)
    {

    Scanner console = new Scanner(System.in);
    System.out.println("Enter the input file name: ");
    String inputFileName = console.next();
    System.out.println("Enter the output file name: ");
    String outputFileName = console.next();

    WordListAL myList = new WordListAL();
    StopWatch myTimer = new StopWatch();

    try
    {
    FileReader reader = new FileReader(inputFileName);
    Scanner in = new Scanner(reader);
    PrintWriter out = new PrintWriter(outputFileName);

    myTimer.start();
    while (in.hasNext())
    {
    //break line into words
    String token = in.next();
    //eliminate punctuation and make lowercase
    token = token.toLowerCase();
    token = areLettersOrDigits(token);
    //add the word if it's not there
    myList.addWord(token);

    // handleToken(words, token);

    }

    // get the number of words in myList
    int myListCount = myList.getSize();


    myTimer.stop();
    System.out.println("Time to add words (search)" + myTimer);

    myTimer.reset();
    myTimer.start();
    // sort the array
    myList.sort();
    myTimer.stop();
    System.out.println("Time to sort " + myTimer);

    myTimer.reset();
    myTimer.start();


    //print out each word and its count to a file
    for (int i = 0; i < myListCount; i++)
    {
    out.println(myList.toString(i));
    }
    myTimer.stop();
    System.out.println("Time to print " + myTimer);

    // close files
    in.close();
    out.close();

    }//end try

    catch(IOException e)
    { System.err.println("Problem reading input, program terminates");
    System.exit(0);
    }

    }


    /**Tests to see if a string is made up of letters or digits
    @param the string to be tested
    @return the string with any characters that are not letters removed
    */
    public static String areLettersOrDigits(String s)
    { int i = 0;
    while(i < s.length())
    { char ch = s.charAt(i);
    if(Character.isLetterOrDigit(ch) == true)
    i++;

    else
    { String s1 = s.substring(0, i);
    String s2 = s.substring(i+1);
    s = s1 + s2;
    return s;
    }
    }
    return s;
    }


    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'd help you, but you never replied to my reply to your last thread here.

    Best of luck.

    Edit: I see that you've got several threads where you don't respond to help given. You might want to change this as your responses / thanks etc. are what help motivate others to help you again. Most folks don't like giving advice only to be ignored.

    Again, best of luck!
    Last edited by Fubarable; 04-14-2009 at 11:33 PM.

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if you want to print those word in order, you may need to use ordered set, which will not be hashmap or use 1 array with your hashmap to store the words and sort the array

    use containsKey to check the word exist in the map or not, corresponding value increase by 1/ set to 1
    HashMap (Java Platform SE 6)
    Last edited by mtyoung; 04-15-2009 at 09:22 AM.

Similar Threads

  1. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-16-2011, 11:12 PM
  2. HashMap
    By koolhoney in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2009, 08:08 PM
  3. Hashmap - get key according to value
    By gtriant in forum New To Java
    Replies: 1
    Last Post: 12-15-2008, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •