来自输入的Java最长单词

来自输入的Java最长单词,java,string,Java,String,我是一个初学者,我正在试图找出为什么查找最大单词的逻辑不起作用。 有时,输出将产生正确的最长单词、第一个单词或多个单词 谢谢 PS 我真的不关心两个相同长度的单词的情况,等我弄明白为什么这不起作用后,我会处理这个问题。再次请注意,我是一个初学者/新手。谢谢 import java.util.Scanner; 导入java.util.ArrayList; 公共类词 { 公共静态字符串字(字符串str) { int longestCount=0; 整数计数=0; int newWord=0; 字串

我是一个初学者,我正在试图找出为什么查找最大单词的逻辑不起作用。 有时,输出将产生正确的最长单词、第一个单词或多个单词

谢谢

PS 我真的不关心两个相同长度的单词的情况,等我弄明白为什么这不起作用后,我会处理这个问题。再次请注意,我是一个初学者/新手。谢谢

import java.util.Scanner;
导入java.util.ArrayList;
公共类词
{
公共静态字符串字(字符串str)
{
int longestCount=0;
整数计数=0;
int newWord=0;
字串=”;
ArrayList longestWord=新的ArrayList();

对于(inti=0;i来说,首先使用split将字符串切碎会更容易

我已经在下面的代码中尽可能多地进行了注释

public static List<String> word(String str)
{
    String[] choppedUpWord = str.split(" ");
    int longestWordLength = 0; //we reset the longestWord if this is updated.
    ArrayList <String> longestWord= new ArrayList <String>(); //the results

    for(int i=0; i < choppedUpWord.length; i ++){
       String theWord = choppedUpWord[i];
       if(longestWordLength < theWord.length()){
           //new longest word found !
           longestWord.clear(); //remove the old entries
           longestWord.add(theWord); // add the new word in
           longestWordLength = theWord.length(); update with new length
       }else if(longestWordLength == theWord.length()){
           //same length as the longest word, do an appending.
           longestWord.add(theWord); // add the new word in 
       }
    } 

    return longestWord;
}
公共静态列表字(String str)
{
字符串[]choppedpword=str.split(“”);
int longestWordLength=0;//如果更新,我们将重置longestWord。
ArrayList longestWord=new ArrayList();//结果
对于(int i=0;i
当多个单词长度相同时,它返回事件的列表而不是字符串

编辑或者也可以使用StringBuilder

public static String word(String str)
{
    String[] choppedUpWord = str.split(" ");
    int longestWordLength = 0; //we reset the longestWord if this is updated.
    StringBuilder longestWord= new StringBuilder(); //the results

    for(int i=0; i < choppedUpWord.length; i ++){
       String theWord = choppedUpWord[i];
       if(longestWordLength < theWord.length()){
           //new longest word found !
           longestWord.setLength(0); //remove the old entries
           longestWord.append(theWord); // add the new word in
           longestWordLength = theWord.length(); //update with new length
       }else if(longestWordLength == theWord.length()){
           //same length as the longest word, do an appending.
           longestWord.append(" "); //add a spacing between each word (or any delimiter that you like)
           longestWord.add(theWord); // add the new word in 
       }
    } 

    return longestWord.toString();
}
公共静态字符串字(String str)
{
字符串[]choppedpword=str.split(“”);
int longestWordLength=0;//如果更新,我们将重置longestWord。
StringBuilder longestWord=new StringBuilder();//结果
对于(int i=0;i
您想得太多了。只要在数组列表中循环一次,只要看到较长的单词,就存储该单词/或其索引:

ArrayList <String> words= new ArrayList <String>();
String currLongest = words.get(0);
for (String s : words)
    if(s.length() > currLongest.length())
        currLongest = s;

请注意,不需要将最长的单词存储到列表中,因为在任何时候,都应该只有一个最长的单词。

不清楚为什么需要子字符串……您不能先按空格分割单词吗?此外,您确实不需要列表来存储单词。只要存储最长的单词,当您看到较长的字符串时,它的长度就可以了n您找到一个单词,但它不是最长的,您仍然需要更新
count
newWord
(另外,如果最长的单词是行中的最后一个单词,并且没有尾随空格,会发生什么情况?)你应该在调试器中逐步检查你的代码,或者简单地阅读它并验证/验证你所做的每一个选择。我真的不知道你的代码如何找到一个最长的单词。谢谢,我只是不熟悉拆分方法。学习是件好事!@jamyooes如果我的解决方案回答了你的问题,你可以通过点击勾选b来接受我的解决方案eside it。每个问题只能接受一个解决方案。这样做可以提高您的声誉,最终为您在本网站提供更多的特权。我将您的问题作为一种鼓励进行了投票。清除了这些内容。我倾向于过度思考,我想学习如何简化代码。感谢您的帮助和努力。我从中学到了很多。
String[] words = str.split(" ");
String currLongest = words.[0];
for (String s : words)
    if(s.length() > currLongest.length())
        currLongest = s;