Answer on Question #63822- Programming & Computer Science - Java | JSP | JSF
Question
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 homework
Solution
public class Main {
String sentence = ""; //input sentence
String word = ""; // the word with the fewest of letters
public static void main(String[] args) {
Main main = new Main(); //
main.sentence = "Wikipedia is a free online encyclopedia, created and edited by volunteers around the world and hosted by the Wikimedia Foundation.";
main.shortestWord();
System.out.println(main.word);
}
public void shortestWord(){
String[] strings = sentence.split(" "); // split sentence on words by spaces
word = strings[0]; // assign a word with the fewest letters value of the first element of the array of words in the sentence
for (int i = 1; i < strings.length; i++) { // find word with fewer of letters
if (word.length() > strings[i].length()) { // if found then assign a new value
word = strings[i];
}
}
}
}Answer
Input search word and sentence are members of the class, and we can access them with method shortestWord().
http://www.AssignmentExpert.com