The Artima Developer Community
Sponsored Link

Java Answers Forum
String arrays

2 replies on 1 page. Most recent reply: Jan 10, 2003 10:12 AM by Ugo Posada

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
zeus7

Posts: 1
Nickname: gsquared
Registered: Jan, 2003

String arrays Posted: Jan 9, 2003 12:12 PM
Reply to this message Reply
Advertisement
How do I find the maximum/minimum value in a string array whose elements are concatenated strings. The maximum/minimum value is to be based upon the first few characters of the concatenated string (that were declared initially as doubles). I am very new to Java so simplistic answers would be appreciated. Thanks.


Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: String arrays Posted: Jan 10, 2003 10:11 AM
Reply to this message Reply
I couldn't quite understand your question but I wrote a program that gives the lower and higher strings based on the lexicographical order, I hope it is what you need.

import java.util.Vector;
import java.util.Collections;
 
public class MinMax {
	
    private Vector stringVector;
    
    public MinMax(String[] input) {
        
        stringVector = new Vector();
        
        for (int i = 0; i < input.length; i++) {
            stringVector.addElement( input[i].toLowerCase() );	
        }
        
        Collections.sort(stringVector);		
    }	
    
    public String getMin() {
        return (String) stringVector.firstElement(); 	
    }	
    
    public String getMax() {
    	return (String) stringVector.lastElement(); 
    }
    
    public static void main(String[] args) {
    	String[] input = {"Pear", "airplane", "Zippo", "deer"};
    	
    	MinMax myMinMax = new MinMax(input);
    	
    	System.out.println( "Minimum value is: "+ myMinMax.getMin() );
    	System.out.println( "Maximum value is: "+     myMinMax.getMax() );
    }		
}
 

Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: String arrays Posted: Jan 10, 2003 10:12 AM
Reply to this message Reply
Look how easy is to port the minMax solution to doubles

import java.util.Vector;
import java.util.Collections;
 
public class MinMaxDouble {
	
    private Vector doubleVector;
    
    public MinMaxDouble(Double[] input) {
        
        doubleVector = new Vector();
        
        for (int i = 0; i < input.length; i++) {
            doubleVector.addElement( input[i] );	
        }
        
        Collections.sort(doubleVector);		
    }	
    
    public Double getMin() {
        return (Double) doubleVector.firstElement(); 	
    }	
    
    public Double getMax() {
    	return (Double) doubleVector.lastElement(); 
    }
    
    public static void main(String[] args) {
    	Double[] input = { new Double(1), new Double(200000), 
    	                   new Double(5), new Double(3456)};
    	
    	MinMaxDouble myMinMax = new MinMaxDouble(input);
    	
    	System.out.println( "Minimum value is: "+ myMinMax.getMin() );
    	System.out.println( "Maximum value is: "+ myMinMax.getMax() );
    }		
}
 
 

Flat View: This topic has 2 replies on 1 page
Topic: problem in download servlet Previous Topic   Next Topic Topic: Sound: sequencing wav/au files

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use