Java 句子中最短和最长的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25286690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shortest and Longest word in a sentence in Java
提问by Satyaki
I am new in Java Programming! So tried to solve a problem to find the shortest and longest word in a sentence. My program is shown below . I hope you people can show me the right direction about my program -
我是 Java 编程新手!所以试图解决一个问题,找出句子中最短和最长的单词。我的程序如下所示。我希望你们能告诉我关于我的程序的正确方向 -
public class StringProblems {
void shortAndLongWord(String str) {
String sw = "", lw = "";
int s = str.length(), l = 0;
String words[] = str.split(" ");
for(String word:words) {
if(word.length()<s)
sw = word;
else if(word.length()>l)
lw = word;
}
System.out.println("LONGEST WORD : "+lw);
System.out.println("SHORTEST WORD : "+sw);
}
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
StringProblems obj = new StringProblems();
System.out.printf("Enter a line to get shortest and longest word:");
String str = scr.nextLine();
str += " ";
obj.shortAndLongWord(str);
}
}
Output of this program is :*Enter a line to get shortest and logest word:This is Sentence
该程序的输出是:*输入一行以获得最短和最记录的单词:这是句子
LONGEST WORD : SHORTEST WORD : Sentence**
最长的单词:最短的单词:句子**
I dont know where my logic went wrong! Please help me to solve!
我不知道我的逻辑哪里出错了!请帮我解决!
回答by jh314
You have to keep updating the length of the current shortest and longest word:
您必须不断更新当前最短和最长单词的长度:
public class StringProblems {
void shortAndLongWord(String str)
{
if (str == null)
return;
String sw="",lw="";
int s=str.length(),l=0;
String words[]=str.split(" ");
for(String word:words)
{
if(word.length()<s)
{
sw=word;
s = word.length();
}
if(word.length()>l)
{
lw=word;
l = word.length();
}
}
System.out.println("LONGEST WORD : "+lw);
System.out.println("SHORTEST WORD : "+sw);
}
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
StringProblems obj=new StringProblems();
System.out.printf("Enter a line to get shortest and longest word:");
String str=scr.nextLine();
str+=" ";
obj.shortAndLongWord(str);
}
}
回答by TJ-
You need to keep updating l
(length of the longest word) and s
(length of the smallest word)
你需要不断更新l
(最长单词的s
长度)和(最小单词的长度)
if(word.length()<s)
{
sw=word;
s = word.length();
}
if(word.length()>l)
{
lw=word;
l = word.length();
}
Also, there are boundary conditions that you need to take care of. For example, what happens if the string is a single word. What happens when the input string is null etc.
此外,您还需要注意边界条件。例如,如果字符串是单个单词会发生什么。当输入字符串为空等时会发生什么?
回答by Prasad Khode
You are not updating values of l
and s
during iteration. You should try something like this:
您没有在迭代期间更新l
和 的值s
。你应该尝试这样的事情:
if (word.length() < s) {
sw = word;
s = word.length();
} else if (word.length() > l) {
lw = word;
l = word.length();
}
回答by Rajib Chaudhuri
import java.util.Scanner;
public class ShortestAndLongestWordInALine {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any sentence : ");
String line = sc.nextLine();
sc.close();
int shortestWordLength = 0;
int longestWordLength = 0;
String shortestWord = "";
String longestWord = "";
int tempLength = 0;
String[] eachWordArray = line.split(" ");
boolean firstTime = false;
for (String eachWord : eachWordArray) {
tempLength = eachWord.length();
if (firstTime == false) {
firstTime = true;
shortestWordLength = tempLength;
shortestWord = eachWord;
longestWordLength = tempLength;
longestWord = eachWord;
}
if (tempLength > 0) {
if (tempLength < shortestWordLength) {
shortestWordLength = tempLength;
shortestWord = eachWord;
} else if (tempLength > longestWordLength) {
longestWordLength = tempLength;
longestWord = eachWord;
}
}
}
System.out.println("shortestWordLength = " + shortestWordLength + " shortestWord = " + shortestWord);
System.out.println("longestWordLength = " + longestWordLength + " longestWord = " + longestWord);
}
}
回答by Ohm Brock
import java.util.*;
class lw
{
public static void main()
{
Scanner in=new Scanner(System.in);
String z=" ",lw="";
int q=0,o=0;
System.out.println("Enter string");
String str=in.nextLine()+" ";
int l=str.length();
for(int i=1;i<l;i++)
{
char ch=str.charAt(i);
if(ch!=' ')
z=z+ch;
else
{
q=z.length();
if(q>o)
lw=z;
o=q;
z="";
}
}
System.out.println("lw= "+lw);
System.out.println("length="+q);
}
}