Display Words in Ascending Order from text file

using Collections HashMap, TreeMap

Page 1 of 1

5 Replies - 8113 Views - Last Post: 23 September 2009 - 04:21 PM Rate Topic: -----

#1 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Display Words in Ascending Order from text file

Posted 23 September 2009 - 08:34 AM

I need to write a program that will read words from a text file and then display them in ascending order. I am to use the java collections framework to do the ascending sort (i.e. alphabetical order). Just to test that I got the concept I made a file with four words (separated by spaces) and I want to put them into a hashmap then create a treemap and pass the hashmap into it and then display the treemap which ought to output it all in ascending order.

If someone would be so kind as to fix my code for me and explain where I'm going wrong, I'd greatly appreciate it.

thanks,
irongleet

import java.io.*;
import java.util.*;

public class AscendingOrder {
	public static void main(String[] args) throws IOException {




		ObjectInputStream input = new ObjectInputStream(new FileInputStream("words.txt"));

		Map<String, Integer> hashMap = new HashMap<String, Integer>();

		// code to read the words in the text file into the hashMap


		hashMap.put(input.readObject(), 1);
		hashMap.put(input.readUTF(), 2);
		hashMap.put(input.readUTF(), 3);
		hashMap.put(input.readUTF(), 4);
		input.close();
		Map<String, Integer> treeMap = new TreeMap<String,  Integer>(hashMap);
		System.out.println("Display entries in ascending order of key");
		System.out.println(treeMap + "\n");

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Display Words in Ascending Order from text file

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Display Words in Ascending Order from text file

Posted 23 September 2009 - 08:52 AM

You could use a priority queue object, with priority based on characters starting at the left hand side going to the right. You could also add them to a LinkedList<String> and use the Collections.sort() method or implement Radix Sort. Radix Sort does basically what I described with the priority queue.

Priority Queue: http://java.sun.com/...orityQueue.html

Radix Sort: http://en.wikipedia....wiki/Radix_sort
Was This Post Helpful? 0
  • +
  • -

#3 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Display Words in Ascending Order from text file

Posted 23 September 2009 - 09:25 AM

View Postmacosxnerd101, on 23 Sep, 2009 - 07:52 AM, said:

You could use a priority queue object, with priority based on characters starting at the left hand side going to the right. You could also add them to a LinkedList<String> and use the Collections.sort() method or implement Radix Sort. Radix Sort does basically what I described with the priority queue.

Priority Queue: http://java.sun.com/...orityQueue.html

Radix Sort: http://en.wikipedia....wiki/Radix_sort

\

Thanks for the advice... I will experiment with these classes.

One more question: why is this code throwing errors when i try to compile it?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Display Words in Ascending Order from text file

Posted 23 September 2009 - 11:47 AM

If I had to guess it would be b/c the File words.txt wasn't found. However, without seeing the error message, I can't tell you for sure.
Was This Post Helpful? 0
  • +
  • -

#5 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Display Words in Ascending Order from text file

Posted 23 September 2009 - 01:04 PM

I want this program to use scanner to read names from a text file ( which consists of first names., several of them, separated by spaces ) add the names to an arraylist then sort it and output the results.

Sounds simple, but I been working hours on this...

import java.io.*;

import java.util.*;



public class ScannerAscend {



	private String[] strings = new String[10];

	private int counter;



	public ScannerAscend() {

		ArrayList list = new ArrayList();

		String[] strings = new String[10];

		File file = new File("words.txt");

		Scanner scanner = new Scanner("file");



		try {

			while(scanner.hasNext()) {

				String name = scanner.next();

				//System.out.println(name);

				list.add(name);

				//System.out.println(scanner.next());

				

			}



		}



		finally {

			scanner.close();

		}

		Collections.sort(list);

		System.out.println(list.toString());

		



	}

	public static void main(String[] args) {

		new ScannerAscend();

	}



}

This post has been edited by Charlie IronGleet: 23 September 2009 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Display Words in Ascending Order from text file

Posted 23 September 2009 - 04:21 PM

Try this:

ArrayList<String> names = new ArrayList<String>();
Scanner scan = null;
File f = null;

try{
   f = new File("words.txt");
   scan = new Scanner(f);
   while(scan.hasNext()) names.add(scan.next());
}
finally{scan.close();}

Collections.sort(names);

//iterate through and print each individual name in the list
for(String s:names) System.out.println(s);




You really didn't need the background array or the counter field. Plus, cut down on the spacing a little bit. A little spacing is good to make it easier to read, but a lot of spacing forces a lot of scrolling, making it hard for any other coders looking at your code to figure out what's going on easily.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1