Finding shortest word in java string array
im trying to write code to be taken in a group of words and will return the smallest word in character length. quite simply, for some reason, it returns "bye" when there is a shorter word like "no".
public class function2 {
public static void main(String [] args) {
String [] SA = {"hello", "goodbye", "jack", "bye", "yes", "no", "yoo"};
smallest(SA);
System.out.println("The shortest word is " + smallest(SA));
}
public static String smallest(String SA[]) {
String first = SA[0];
for (int i = 1 ; i < SA.length ; i++) {
if ((SA[i].compareTo(first)) < 0) {
first = SA[i];
} // if
} // for
return first;
}// smallest
}// lab1b
source to share
Your method is close, but your variable names are a bit hard to read and you only need to call your method once to print the shortest name (twice it looks twice twice and discards the first result).
public static void main(String[] args) {
String[] SA = { "hello", "goodbye", "jack", "bye", "yes", "no", "yoo" };
System.out.println("The shortest word is " + smallest(SA));
}
Next, it's usually a good idea to check that your input is valid (not null
at least one item). You also have to decide how you want to handle these cases, I have selected return "";
below. Finally, you need to check length()
String (s) to get the shortest word. Something like,
public static String smallest(String words[]) {
if (words == null || words.length < 1) {
return "";
}
String smallest = words[0];
for (int i = 1; i < words.length; i++) {
if (words[i].length() < smallest.length()) {
smallest = words[i];
}
}
return smallest;
}// smallest
source to share
import java.util.Comparator;
import java.util.function.Function;
import static java.util.Arrays.asList;
public class Foo {
public static void main(String[] args) {
String[] words =
{"hello", "goodbye", "jack", "bye", "yes", "no", "yoo"};
System.out.println(shortestWord(words));
}
static String shortestWord(String[] words) {
return asList(words).stream().min(compareBy(String::length)).get();
}
static <A, B extends Comparable<B>> Comparator<A> compareBy(
Function<A, B> f) {
return (A x, A y) -> f.apply(x).compareTo(f.apply(y));
}
}
source to share
I'm not sure what you are trying to do with the compareTo () method, but if you want to keep the length of the string, you use the length () method.
public class function 2
{
public static void main(String[] args) {
String [] SA = {"hello" , "goodbye" , "jack" , "bye" , "yes" , "no" , "yoo"};
System.out.println("The shortest word is " + smallest(SA));
}
public static String smallest(String SA[]) {
//Keep track of the shortest word by index and length
int index = 0, minLength = SA[0].length();
for (int i = 1; i < SA.length; i++){
//if the next string is smaller in length then we save that index and length in our variables
if(SA[i].length() < minLength){
index = i;
minLength = SA[i].length();
}
}
//returns the smallest word
return SA[index];
}
}
source to share
In Java 8 you create Comparator
that will check the length of a string that will make the array grow, convert that list to a stream and use it sorted
to sort the array, eventually use findFirst
to return the first element and get()
to convert it to String
.
String[] words = new String[]{"Hello", "aadsads", "adssadsadads", "aaa"};
String shortest = Arrays.asList(words).stream()
.sorted((e2, e1) -> e1.length() > e2.length() ? -1 : 1)
.findFirst().get();
source to share