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: need help with sorting

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with sorting

    I got 99% done just need help with sorting code.

    It reads the text file that i added in the main class.





    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Scanner;
    import java.io.File;
     
    public class WordCounter {
     
    private HashMap<String, Integer> wc;
     
    public WordCounter(String filename) {
    		wc = new HashMap<String, Integer>();
    		countWords(filename);
    }
    private void countWords(String filename)  {
    		try {
    			Scanner scan = new Scanner(new File(filename));
     
    			while (scan.hasNextLine()) {
           String[] words = scan.nextLine().split("[^a-zA-Z]+");
           // track of the frequency
               for (String s :words){	
               String word= s.toLowerCase();
               if(wc.containsKey(word)){
               		int x= wc.get(word); x++;
               		wc.put(word, x);
               }
               else
               		wc.put(word, 1);
               }
     
           }
    		}
    		catch (Exception e) {
               System.err.println(e);
               System.out.println("Exc");
           }
    }
     
    public HashMap<String, Integer> getWc() {
    		return wc;
    }
     
        public void print() {
     
        System.out.println("Word:\t\t\tFrequency:");
        System.out.println("-------------------------------------------------");
        for (String key : wc.keySet())
        		System.out.println(key + "\t\t\t" + wc.get(key));
     
        }
     
    public static void main(String[] args) throws Exception {
    		String filename = "test-file.txt";
    		if (args.length >= 1)
    			filename = args[0];
    		WordCounter wdc = new WordCounter(filename);
    		wdc.print();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help with sorting

    need help with sorting code.
    Can you explain what problems you are having with more detail?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with sorting

    sure,

    it takes a txt file (in this case test-file.txt) and reads/prints it out also counts the freq. of the words in the txt (such as the word "bob" apperes 2 times). but i want it to be sorted in ABC

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help with sorting

    but i want it to be sorted in ABC
    That doesn't have any details on how the code is going to do the sort.
    What are your ideas about how to do that?
    Where is the data that you want to sort?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with sorting

    here what it prints out:

    Word: Frequency:
    -------------------------------------------------
    old 1
    is 1
    mars 1
    bob 2
    name 1
    years 1
    hi 1
    im 1
    in 1
    my 1
    live 1
    i 1

    I want it to be sorted by alphabetical order

    I cant figure out the correct code.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help with sorting

    To be able to sort a bunch of Strings, the String will need to be in a container. The code needs to add the Strings to a container so they can be sorted.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Sorting
    By rafia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2013, 10:39 AM
  2. Sorting/Search
    By dx8292 in forum Algorithms & Recursion
    Replies: 7
    Last Post: February 14th, 2012, 04:09 AM
  3. Sorting Arrays
    By Bryan29 in forum Collections and Generics
    Replies: 5
    Last Post: November 28th, 2011, 07:21 AM
  4. SORTING ALGORITHMS...
    By Medo Almasry in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 14th, 2011, 08:18 PM
  5. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM