Question #63823

method shortestWord () that does not accept any parameter and return string these metode should find the shortest word in sentence
please help me in me in my homewoork

Expert's answer

package question63823;

import java.util.*;

public class Question63823 {

static String sentence;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sentence = sc.nextLine();
System.out.println(shortestWord());
}

public static String shortestWord() {
String[] arrayOfWord = sentence.split(" ");
int minLength = Integer.MAX_VALUE;
String result = null;
for (String word : arrayOfWord) {
int tempLength = word.length();
if (tempLength < minLength && word.length() > 0 && Character.isAlphabetic(word.charAt(0))) {
minLength = tempLength;
result = word;
}
}
return result;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS