Results 1 to 4 of 4
  1. #1
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default Largest string value (alphabetically)

    I want to find the largest string value (alphabetically) in the following ArrayList. How to do that?

    Java Code:
    		
    ArrayList <String> list = new ArrayList<String>();
    list.add("ABC");
    list.add("XYZ");
    list.add("KLM");

  2. #2
    rajiv_bang is offline Member
    Join Date
    Nov 2007
    Posts
    11
    Rep Power
    0

    Default

    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

    Java 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());
     }
    }

  3. #3
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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:
    Java 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:
    Java 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 06:12 AM. Reason: code error

  4. #4
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default

    Thanks to both of use. I have implemented your code and got the required results.

    I learned new stuff.
    Keep posting.

Similar Threads

  1. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 08:25 PM
  2. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM
  3. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM
  4. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 PM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 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
  •