简体   繁体   中英

How to print shortest String from ArrayList (Java)

Ok so the goal is to print the smallest length string ( for example, if input is "co", "college", "colleges", "university" I want it to print out co. I've tried the college.compareTo( ____ ); as well as a couple other things but I can't seem to get it to print it out. Also it isn't anything fancy (if you've read/have head first java 2nd edition then we are on chapters 5/6) I'd rather if you can link me to a video that will show the process of what needs to be done with an explanation but anything is helpful :s been staring at this coding for a couple weeks and kinda went brain dead on it... This is what I have so far (accepts strings from user);

ArrayList <String> colleges = new ArrayList <String> ( ) ;
    String input;
    Scanner scan = new Scanner(System.in) ;
    while (true) {
        System.out.println("Please enter your college (Press Enter twice to quit) ");
        input = scan.nextLine();
        if (input.equals ("")) {
            break;
        }
        else {
            colleges.add(input.toUpperCase () );
        }// end of if else statement
    }// end of while loop

    System.out.println("The following " + colleges.size() + " colleges have been entered:");

    for ( String college : colleges) {
         System.out.println("\n" + college );
         System.out.println("Character count: " +  college.length( ) );
    }// end of for each loop

These are the steps you need:

  1. Create a custom Comparator<String> to sort strings based on their length.
  2. Get the shortest string from your list by using your custom comparator with the Collections.min() method.

In its most compact version, your code could look something like this (assuming there are no null strings in the list):

String shortest = Collections.min(colleges, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        return s1.length() - s2.length();
    }
});

You can use the following logic to print the smallest string in given arrayList

string smallString = "";
for ( String college : colleges) {
    if(smallString.length() == 0 && college.length() != 0)
    {
        smallString = college ;
    }
    else if(college.length() < smallString.length() && college.length() != 0)
    {
        smallString = college;
    }
}
println("Smallest string is: " + smallString );
public static String SmallestString(ArrayList <String> collegeArray){
    String smallest = "";
    System.out.println("");
    for(String string :collegeArray){
        //if input-string is null or empty
        if(string == null || string.trim() == "" || string.trim().length()==0){
            System.out.println("Emtpy String Encountered");
            continue;
        }
        if(smallest == ""){
            smallest = string;
            continue;
        }
        if(string.length() < smallest.length()){
            smallest = string;
        }
    }
    return smallest;
}

Call it with: System.out.println("\\nSmallest String: " + SmallestString(colleges));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM