Länge von Strings im Array vergleichen

Status
Nicht offen für weitere Antworten.

Tarsuinn

Mitglied
Hey,

wie der Titel schon verrät habe ich ein problem mit dem Vergleichen der Länge von Strings in einem Array,

Also z.b. "a","bb","ccc" soll false ergeben "ccc","bb","a" true

hier mal der Code bis jetzt:

Code:
    public static boolean werdenWoerterKuerzer(String[] woerter) {
        
        boolean hilfe = false;
        
        for(int i = 0; i < woerter.length; i++) {

            if(woerter[i].length() > woerter[i].length())
                
                hilfe = true;
                        
            else {
                
                hilfe = false;
            }
            
        }
        
        return hilfe;
    
    }
    
    public static void main(String[] args) {
        
//        System.out.println(hatTeiler(2,2,3));
        String[] wort = {"ccc","bb","a"};
        System.out.println(werdenWoerterKuerzer(wort));
    }

Hab nun das Problem das er so immer nur die Länge des selben Wortes vergleicht und das ist natürlich immer false, habe auch schon

Code:
woerter[i+1].length())

probiert aber das gibt natürlich nen IndexoutofBounds...

Wie kann man das am besten lösen, denke der Ansatz sollte da sein?

grüße
 
so sollts funktionieren:


Code:
public class StringComparisonMain
{

	public static void main(String[] args)
	{
		System.out.println(werdenWoerterKuerzer(null));
		System.out.println(werdenWoerterKuerzer(new String[] {}));
		System.out.println(werdenWoerterKuerzer(new String[] { "hallo" }));
		System.out.println(werdenWoerterKuerzer(new String[] { "a", "bb" }));
		System.out.println(werdenWoerterKuerzer(new String[] { "bb", "a" }));
	}

	public static boolean werdenWoerterKuerzer(String[] strings)
	{
		// check if the array is null or empty
		if (strings == null || strings.length == 0)
			return false;

		// check if the length is 1
		if (strings.length == 1 && strings[0] != null)
			return true;

		// get the first entry of the array
		String previous = strings[0];
		
		// check if the first entry is null
		if (previous == null)
			return false;

		// iterate over all elements of array, except the first one
		for (int i = 1; i < strings.length; i++)
		{
			// get the current element and check if it is null
			String current = strings[i];
			if (current == null)
				return false;

			// compare the length of the previous element with the length of the
			// current element
			if (previous.length() <= current.length())
				return false;

			// set the current element as the previous
			previous = current;
		}
		return true;
	}

}
 
Dank dir, hab gerade noch eine andere und für mich günstigere Lösung gefunden (identische Strings liefern auch true):

Code:
    public static boolean werdenWoerterKuerzer(String[] woerter) {
        
        boolean hilfe = false;
        
        for(int i = 1; i <=woerter.length -1; i++) {
            
            if(woerter[i-1].length() > woerter[i].length() || woerter[i-1].equals(woerter[i])) {
                
                hilfe = true;
                
            } else
                
                hilfe = false;
        }
        return hilfe;
    }

MfG
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben