Java 如何在给定字符串中查找最长的单词?
在给定的字符串中,我想找到最长的单词,然后在控制台中打印它 我得到的输出是第二长的单词,即Java 如何在给定字符串中查找最长的单词?,java,string,Java,String,在给定的字符串中,我想找到最长的单词,然后在控制台中打印它 我得到的输出是第二长的单词,即“Today”,但我应该得到“Happiest”。 我能知道我做错了什么吗?有没有更好/不同的方法来查找字符串中最长的单词 public class DemoString { public static void main(String[] args) { String s = "Today is the happiest day of my life";
“Today”
,但我应该得到“Happiest”
。我能知道我做错了什么吗?有没有更好/不同的方法来查找字符串中最长的单词
public class DemoString {
public static void main(String[] args) {
String s = "Today is the happiest day of my life";
String[] word = s.split(" ");
String longword = " ";
for (int i = 0; i < word.length; i++)
for (int j = 1 + i; j < word.length; j++)
if (word[i].length() >= word[j].length())
longword = word[i];
System.out.println(longword + " is the longest word with " + longword.length() + " characters.");
System.out.println(rts.length());
}
}
公共类DemoString{
公共静态void main(字符串[]args){
String s=“今天是我一生中最快乐的一天”;
字符串[]word=s.split(“”);
字符串longword=“”;
for(int i=0;i=word[j].length())
长单词=单词[i];
System.out.println(longword+”是最长的单词,带有“+longword.length()+”个字符);
System.out.println(rts.length());
}
}
相反,它应该是:
for(int i=0; i < word.length; i++)
{
if(word[i].length() >= rts.length())
{
rts = word[i];
}
}
for(int i=0;i=rts.length())
{
rts=字[i];
}
}
相反,它应该是:
for(int i=0; i < word.length; i++)
{
if(word[i].length() >= rts.length())
{
rts = word[i];
}
}
for(int i=0;i=rts.length())
{
rts=字[i];
}
}
你可以试试
String s="Today is the happiest day of my life";
String[] word=s.split(" ");
String rts=" ";
for(int i=0;i<word.length;i++){
if(word[i].length()>=rts.length()){
rts=word[i];
}
}
System.out.println(rts);
System.out.println(rts.length());
String s=“今天是我一生中最快乐的一天”;
字符串[]word=s.split(“”);
字符串rts=“”;
对于(int i=0;i=rts.length()){
rts=字[i];
}
}
系统输出打印LN(rts);
System.out.println(rts.length());
你可以试试
String s="Today is the happiest day of my life";
String[] word=s.split(" ");
String rts=" ";
for(int i=0;i<word.length;i++){
if(word[i].length()>=rts.length()){
rts=word[i];
}
}
System.out.println(rts);
System.out.println(rts.length());
String s=“今天是我一生中最快乐的一天”;
字符串[]word=s.split(“”);
字符串rts=“”;
对于(int i=0;i=rts.length()){
rts=字[i];
}
}
系统输出打印LN(rts);
System.out.println(rts.length());
for(int i=0;i=rts.length()){
rts=单词[j];
}
}else如果(字[i].length()>=rts.length()){
rts=字[i];
}
}
}
for(int i=0;i=rts.length()){
rts=单词[j];
}
}else如果(字[i].length()>=rts.length()){
rts=字[i];
}
}
}
以下是一个可用于Java 8的单行程序:
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
String s = "Today is the happiest day of my life";
String longest = Arrays.stream(s.split(" ")).max(Comparator.comparingInt(String::length)).orElse(null);
System.out.println(longest);
}
}
输出:
happiest
happiest
试试看这是一个可以与Java 8一起使用的单行程序:
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
String s = "Today is the happiest day of my life";
String longest = Arrays.stream(s.split(" ")).max(Comparator.comparingInt(String::length)).orElse(null);
System.out.println(longest);
}
}
输出:
happiest
happiest
试试看String s=“今天是我一生中最快乐的一天,作者:vijayakumar”;
字符串[]word=s.split(“”);
字符串maxlethWord=“”;
for(int i=0;i=maxlethWord.length()){
maxlethWord=单词[i];
}
}
System.out.println(maxlethWord);
String s=“今天是我一生中最快乐的一天”;
字符串[]word=s.split(“”);
字符串maxlethWord=“”;
for(int i=0;i=maxlethWord.length()){
maxlethWord=单词[i];
}
}
System.out.println(maxlethWord);
试试这个
public static void main( String[] args )
{
String s = "Today is the happiest day of my life";
String[] word = s.split( " " );
String rts = " ";
for ( int i = 0; i < word.length; i++ )
{
if ( word[i].length() > rts.length() )
rts = word[i];
}
System.out.println( rts );
}
publicstaticvoidmain(字符串[]args)
{
String s=“今天是我一生中最快乐的一天”;
字符串[]word=s.split(“”);
字符串rts=“”;
for(int i=0;irts.length())
rts=字[i];
}
系统输出打印LN(rts);
}
试试这个
public static void main( String[] args )
{
String s = "Today is the happiest day of my life";
String[] word = s.split( " " );
String rts = " ";
for ( int i = 0; i < word.length; i++ )
{
if ( word[i].length() > rts.length() )
rts = word[i];
}
System.out.println( rts );
}
publicstaticvoidmain(字符串[]args)
{
String s=“今天是我一生中最快乐的一天”;
字符串[]word=s.split(“”);
字符串rts=“”;
for(int i=0;irts.length())
rts=字[i];
}
系统输出打印LN(rts);
}
//下面的Java程序将在字符串中找到最小和最大的单词
class SmallestAndLargestWord
{
static String minWord = "", maxWord = "";
static void minMaxLengthWords(String input)
{
// minWord and maxWord are received by reference
// and not by value
// will be used to store and return output
int len = input.length();
int si = 0, ei = 0;
int min_length = len, min_start_index = 0,
max_length = 0, max_start_index = 0;
// Loop while input string is not empty
while (ei <= len)
{
if (ei < len && input.charAt(ei) != ' ')
{
ei++;
}
else
{
// end of a word
// find curr word length
int curr_length = ei - si;
if (curr_length < min_length)
{
min_length = curr_length;
min_start_index = si;
}
if (curr_length > max_length)
{
max_length = curr_length;
max_start_index = si;
}
ei++;
si = ei;
}
}
// store minimum and maximum length words
minWord = input.substring(min_start_index, min_start_index + min_length);
maxWord = input.substring(max_start_index, max_length);
}
// Driver code
public static void main(String[] args)
{
String a = "GeeksforGeeks A Computer Science portal for Geeks";
minMaxLengthWords(a);
// to take input in string use getline(cin, a);
System.out.print("Minimum length word: "
+ minWord
+ "\nMaximum length word: "
+ maxWord);
}
}
**//下面的Java程序将在字符串中找到最小和最大的单词
class SmallestAndLargestWord
{
static String minWord = "", maxWord = "";
static void minMaxLengthWords(String input)
{
// minWord and maxWord are received by reference
// and not by value
// will be used to store and return output
int len = input.length();
int si = 0, ei = 0;
int min_length = len, min_start_index = 0,
max_length = 0, max_start_index = 0;
// Loop while input string is not empty
while (ei <= len)
{
if (ei < len && input.charAt(ei) != ' ')
{
ei++;
}
else
{
// end of a word
// find curr word length
int curr_length = ei - si;
if (curr_length < min_length)
{
min_length = curr_length;
min_start_index = si;
}
if (curr_length > max_length)
{
max_length = curr_length;
max_start_index = si;
}
ei++;
si = ei;
}
}
// store minimum and maximum length words
minWord = input.substring(min_start_index, min_start_index + min_length);
maxWord = input.substring(max_start_index, max_length);
}
// Driver code
public static void main(String[] args)
{
String a = "GeeksforGeeks A Computer Science portal for Geeks";
minMaxLengthWords(a);
// to take input in string use getline(cin, a);
System.out.print("Minimum length word: "
+ minWord
+ "\nMaximum length word: "
+ maxWord);
}
}
**我还没有看到一个答案,您可以在其中创建一个单词列表。 因此,这里有另一种解决问题的方法:
String s = "Today is the happiest day of my life";;
List<String> strings = Arrays.asList(s.split(" "));
String biggestWord = Collections.max(strings, Comparator.comparing(String::length));
System.out.println(biggestWord);
我还并没有看到一个答案,你们可以创建一个单词列表。 因此,这里有另一种解决问题的方法:
String s = "Today is the happiest day of my life";;
List<String> strings = Arrays.asList(s.split(" "));
String biggestWord = Collections.max(strings, Comparator.comparing(String::length));
System.out.println(biggestWord);