Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2007, 05:09 AM
Member
 
Join Date: Nov 2007
Posts: 13
Camden is on a distinguished road
sort
i take from a file elements with readLine( ) and i want to sort them ignoring cases (they are words) and put them in a vector (not array).
what should I do? add them in the vector with .add() and then rearrange them and put them in a new vector? and how?
is this situation suitable for a stack use?
can anyone help plz

Last edited by Camden : 11-27-2007 at 06:33 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-27-2007, 05:31 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
You can read the words from a file like so, and put them in the array:
Code:
BufferedReader reader = new BufferedReader(new FileReader("test.txt")); String s; // edit2: changed to StringBuilder. StringBuilder.append(String s) is way faster than String concatenation StringBuilder tmp = new StringBuilder(); while ((s = reader.readLine()) !=null) { tmp.append(s.toLowerCase() + "\n"); } String[] arr = tmp.toString().split("\n");
Then sort:
Code:
java.util.Arrays.sort(arr);

Last edited by staykovmarin : 11-27-2007 at 05:37 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-27-2007, 06:06 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Then convert it to a Vector.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-27-2007, 06:18 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Code:
Vector<String> v = new Vector<String>(); for (int i = 0; i < arr.length; i++) { v.add(arr[i]); }
Java Forums » General Java » New To Java > spoonfeed
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-28-2007, 12:03 AM
Member
 
Join Date: Nov 2007
Posts: 13
Camden is on a distinguished road
you are very rude young man.
any serious answer please?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-28-2007, 01:08 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
i take from a file elements with readLine( ) and i want to sort them ignoring cases (they are words) and put them in a vector (not array).
From staykovmarin's posts:
Code:
BufferedReader reader = new BufferedReader(new FileReader("test.txt")); Vector<String> v = new Vector<String>(); String line; while ((line = reader.readLine()) != null) { v.add(line); }
what should I do? add them in the vector with .add() and then rearrange them and put them in a new vector? and how?
Vector implements the List interface which implements the Collection interface. Therefore you can use the Collections class to sort your Vector with a comparator that sorts the words with/in lower case. See the Comparator api. And see the "Sorting" section on this page Lesson: Algorithms in the Collections trail.
Code:
Collections.sort(v, your_Comparator);
is this situation suitable for a stack use
Maybe; depends on what you want to do.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-28-2007, 01:36 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
I would give up the fight if i were you. Reading is not one of Camden's strong points. Not to mention that if he put my two posts together, he had the exact same result, a Vector with all the information that he needs.

Lucky for him, at least this time he didnt get his posts deleted for refusing to put two and two together AND for attempting to insult me.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-28-2007, 03:11 AM
Member
 
Join Date: Nov 2007
Posts: 13
Camden is on a distinguished road
You insult yourself. And insulting is the easiest thing, especially when someone is hiding behind the screen.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 10:04 PM
need help with bubble sort lowpro New To Java 3 12-17-2007 07:27 PM
how to sort Feng New To Java 1 11-20-2007 08:56 AM
Heap Sort kesav2005 Advanced Java 1 11-13-2007 01:40 PM
how to sort 2 tables valery AWT / Swing 1 08-06-2007 10:30 PM


All times are GMT +3. The time now is 04:12 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org