简体   繁体   English

如何在不使用数组的情况下在java字符串中找到最短的单词?

[英]How to find shortest word in a java string without using arrays?

I'm trying to use the same format of this code which I followed through a tutorial.我正在尝试使用与我在教程中遵循的相同格式的代码。 It will print the longest word as well as the length of the longest word.它将打印最长的单词以及最长单词的长度。 The tutor told me that to find the shortest word I have to flip the if statement to be less than ('<') instead of greater than... however, after I enter any string, when I run the program it returns:导师告诉我,要找到最短的单词,我必须将 if 语句翻转为小于 ('<') 而不是大于...但是,在我输入任何字符串后,当我运行程序时,它返回:

"Shortest Word: "", length: 0" "最短单词:"",长度:0"

I'm not sure how to fix this so it looks for an actual word and not an empty character.. I'd like to follow the same logic here without using arrays as well.我不知道如何解决这个问题,所以它寻找一个实际的单词而不是一个空字符..我想在这里遵循相同的逻辑而不使用数组。

Scanner in = new Scanner(System.in);

System.out.println("Please enter a phrase: ");
String phrase = in.nextLine();


String w = "";
String lw = "";       
int l;
char ch;


phrase = phrase + " ";
l = phrase.length();
int i;
for (i=0; i < l; i++) {
    ch = phrase.charAt(i);
    if(ch != ' ') {
        w = w + ch;
    } else {
        if(w.length() > lw.length()) {
            lw = w;
        }
        w = "";
    }
}

System.out.println("Longest Word: \"" + lw + "\", length: "+ lw.length());

One thing you have to do is change你必须做的一件事就是改变

if(w.length() > lw.length())

to

if(w.length() < lw.length())

However, that's not enough, since lw in initialized to an empty String , so the condition will always be false ( w.length() will never be < 0).然而,这还不够,因为lw在初始化为空String ,所以条件将始终为falsew.length()永远不会 < 0)。

Therefore you also have to check whether lw is still empty:因此,您还必须检查lw是否仍然为空:

if(w.length() < lw.length() || lw.isEmpty ()) {
    lw = w;
}

The full code:完整代码:

Scanner in = new Scanner(System.in);

System.out.println("Please enter a phrase: ");
String phrase = in.nextLine();

String w = "";
String lw = "";       
int l;
char ch;

phrase = phrase + " ";
l = phrase.length();
int i;
for (i=0; i < l; i++) {
    ch = phrase.charAt(i);
    if(ch != ' ') {
        w = w + ch;
    } else {
        if(w.length() < lw.length() || lw.isEmpty ()) {
            lw = w;
        }
        w = "";
    }
}

System.out.println("Shortest Word: \"" + lw + "\", length: "+ lw.length());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用数组且仅在 Java 中循环的情况下打印出字符串中最长的单词 - How to print out longest word in string WITHOUT using arrays and only loops in Java 使用字符串数组(JAVA)的文件的字数统计 - Word Count of a file using String Arrays (JAVA) 这个 Java 代码在不使用 arrays 的情况下按字母顺序对字符串进行排序。但是它在给定字符串的第一个单词中失败 - This Java code alphabetically sorts a string without using arrays. But it fails in the first word of the given string Java:查找字符串中的最短单词并将其打印出来 - Java: Finding the shortest word in a string and printing it out 如何在不使用数组、split() 或 StringBuilder 的情况下逐字反转字符串 - How can I reverse a string word by word without using arrays, split() or StringBuilder 如何在没有数组或为您解析单词的方法的情况下查找字符串中最长的单词 - How to find longest word in string WITHOUT arrays or methods that parse words for you 使用java正则表达式如何在字符串中的任何位置找到特定的单词? - Using java regex how to find particular word any where in the string? 如何使用 java 在给定字符串中查找最后一个字的长度? - how to find Length of Last Word in the given string using java? 如何在不使用循环或 java 的任何内置方法的情况下找到三词短语中第二个单词的长度? - How to find the length of the second word in 3-word phrase without using loops or any built-in methods of java? 如何在java中使用正则表达式提取最后一个最短的字符串 - How can i extract last shortest string using regex in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM