Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 12-12-2007, 12:31 PM
mew mew is offline
Member
 
Join Date: Nov 2007
Posts: 70
mew is on a distinguished road
Largest string value (alphabetically)
I want to find the largest string value (alphabetically) in the following ArrayList. How to do that?

Code:
ArrayList <String> list = new ArrayList<String>(); list.add("ABC"); list.add("XYZ"); list.add("KLM");
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-14-2007, 04:30 AM
Member
 
Join Date: Nov 2007
Posts: 11
rajiv_bang is on a distinguished road
This might not be the best implementation, but this might give you an idea. I have used a StringBuffer in the code below because String is immutable. HTH

Code:
import java.text.DateFormat; import java.util.*; public class LargestString { public static void main(String args[]) { ArrayList <String> list = new ArrayList<String>(); list.add("ABC"); list.add("XYZ"); list.add("KLM"); StringBuffer largest = new StringBuffer(""); for(int i=0; i<list.size(); i++){ if(list.get(i).compareTo(largest.toString())>0){ largest.setLength(0); largest.append(list.get(i)); } } System.out.println(largest.toString()); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-14-2007, 07:06 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
When you say the largest String value do you mean as in size, or do you mean as in b > a < c meaning alphabetical order? For that you would need something like this:
Code:
String a = "aaaaa"; String b = "bb"; char[] aAr = a.toCharArray(); char[] bAr = b.toCharArray(); int lenA = 0, lenB = 0; for (int i = 0; i < aAr.length; i++) { lenA += aAr[i]; } for (int i = 0; i < bAr.length; i++) { lenB += bAr[i]; } System.out.println(lenA + " " + lenB);
Note that what this code does is first convert the String to a charcter array, then adds their ASCII codes together, meaning that a = 97, while b = 98, b > a. You are going to get problems when it comes to capitals though, since a = 97, while CAPITAL B = 66 making it a > B. One solution is to just convert everything to lowercase before you check it. The other one is a little more complicated, it involves changing the for loops:
Code:
String a = "a"; String b = "A"; char[] aAr = a.toCharArray(); char[] bAr = b.toCharArray(); int lenA = 0, lenB = 0; for (int i = 0; i < aAr.length; i++) { if (aAr[i] >= 90 || aAr[i] < 65) lenA += aAr[i]; else lenA += aAr[i] + 64; } for (int i = 0; i < bAr.length; i++) { if (bAr[i] >= 90 || bAr[i] < 65) lenB += bAr[i]; else lenB += bAr[i] + 64; } System.out.println(lenA + " " + lenB);
What this does is simple:
It evaluates the character, if the character value is greater than 54 (so we cover in case there are numbers in the string, 1-9 or other chars) but less then 90, we assume that the value is a capital letter. Then we add to the size the value of the letter + 64, (twice the difference between the lowercase characters, and the uppercase characters).

Last edited by staykovmarin : 12-14-2007 at 07:12 AM. Reason: code error
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-14-2007, 06:45 PM
mew mew is offline
Member
 
Join Date: Nov 2007
Posts: 70
mew is on a distinguished road
Thanks to both of use. I have implemented your code and got the required results.

I learned new stuff.
Keep posting.
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
Finding Largest Prime Factor perito New To Java 2 03-26-2008 07:04 AM
Finding largest and smallest integer mlhazan New To Java 2 01-12-2008 11:30 PM
ArrayList problem (finding largest no) bugger New To Java 3 12-12-2007 01:47 PM
Finding largest no bugger New To Java 11 11-29-2007 01:49 PM
Using java.util.Scanner to search for a String in a String Java Tip Java Tips 0 11-20-2007 05:59 PM


All times are GMT +3. The time now is 10:27 PM.


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