Results 1 to 4 of 4
- 12-12-2007, 11:31 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 70
- Rep Power
- 0
- 12-14-2007, 03:30 AM #2
Member
- Join Date
- Nov 2007
- Posts
- 11
- Rep Power
- 0
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()); } }
- 12-14-2007, 06:06 AM #3
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
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:
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 = "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);
What this does is simple: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);
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
- 12-14-2007, 05:45 PM #4
Member
- Join Date
- Nov 2007
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
Finding Largest Prime Factor
By perito in forum New To JavaReplies: 7Last Post: 11-08-2010, 08:25 PM -
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01-12-2008, 10:30 PM -
ArrayList problem (finding largest no)
By bugger in forum New To JavaReplies: 3Last Post: 12-12-2007, 12:47 PM -
Finding largest no
By bugger in forum New To JavaReplies: 11Last Post: 11-29-2007, 12:49 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks