[英]Write a program to find the second longest word without using array
I need a java program to find the second-longest word in a sentence (without using an array).我需要一个 Java 程序来查找句子中第二长的单词(不使用数组)。
This is the code I have so far:这是我到目前为止的代码:
import java.io.*;
class Second_longest_Trial {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence");
String s = in .readLine();
s = s.trim() + " ";
String longest = s.substring(0, s.indexOf(' '));
String sec = longest;
int l = s.length();
String temp = " ", str = " ";
for (int i = s.indexOf(' ') + 1; i < l; i++) {
char ch = s.charAt(i);
if (ch != ' ')
temp = temp + ch;
else {
if (temp.length() > longest.length()) {
sec = longest;
longest = temp;
} else if (temp.length() > sec.length()) {
sec = temp;
}
temp = " ";
}
}
System.out.println("Longest word is " + longest);
System.out.println("Second Longest word is " + sec);
}
}
When I am giving the input-当我给出输入时-
Sayan goes home.
萨扬回家了。
This outputs-这输出-
Longest word is Sayan Second Longest word is Sayan
最长的词是 Sayan 第二个最长的词是 Sayan
I should have got the output as follows-我应该得到如下输出-
Longest word is Sayan Second Longest word is goes
最长的词是 Sayan 第二最长的词是去
Drop your wonky initial setting of longest
and sec
.放弃你的
longest
和sec
不稳定初始设置。 Create them and temp
the following way:通过以下方式创建它们和
temp
:
String longest="";
String sec="";
String temp="";
for(int i = 0; i < l; i++) {
...
Why would you set longest
and sec
both to the first word - guess what happens if that first word is the longest in the sentence?为什么你将
longest
和sec
都设置为第一个单词 - 猜猜如果第一个单词是句子中最长的会发生什么?
Then your code produces the output:然后你的代码产生输出:
Longest word is Sayan
最长的词是Sayan
Second Longest word is home.第二长的词是家。
That is more correct than what you currently have but still not what you would expect ... because there is the .
这比您目前拥有的更正确,但仍然不是您所期望的......因为有
.
at the end of the sentence you have to take care of - maybe make the check for ch!=' '
a bit more complex and check against '.'
在句子的末尾你必须注意 - 也许检查
ch!=' '
有点复杂并检查'.'
as well.以及。 I leave it for you to figure out how to do that correctly.
我把它留给你弄清楚如何正确地做到这一点。
This is happening because you have initialized the longest element as first word and sec also as longest(which is Sayan itself).发生这种情况是因为您已将最长的元素初始化为第一个单词,将 sec 也初始化为最长的(即Sayan本身)。 Now longest = "Sayan" and sec = "Sayan", you went inside the array but you never found any word which is bigger than longest or sec.
现在longest = "Sayan" 和sec = "Sayan",你进入了数组,但你从未发现任何大于longest 或sec 的单词。
Here is a very basic code to find the second longest word:这是查找第二长单词的非常基本的代码:
class Second
{
public static void main(String str)//str has the required sentence
{
String m="",q="";
int lar=0;
str=str+" ";
for(int i=1;i<=2;i++)
{
for(int k=0;k<str.length();k++)
{
char ch=str.charAt(k);
if(ch!=' ')
m=m+ch;
else
{
if(m.length()>lar)
{
q=m;
lar=q.length();
}
m="";
}
}
if(i==1)
{
System.out.println("The longest word is: "+q);
str=str.replace(q," ");
lar=0;
}
}
System.out.println("The second longest word is: "+q);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.