目前,我能够弄清楚如何显示最长的单词长度。但是,有没有一种使用循环的方法来确定最长的单词并打印出来呢?
public static void longestWordCalculator(String rawText)
{
int lengthCounter = 0;
int finalCounter = 0;
int textLength = rawText.length();
for (int i = 0; i < textLength ; i++)
{
String indexValue = Character.toString(rawText.charAt(i));
if(!" ".equals(indexValue))
{
lengthCounter++;
}
else
{
finalCounter = Math.max(finalCounter, lengthCounter);
lengthCounter = 0;
}
}
System.out.println("Final Value: " + Math.max(finalCounter, lengthCounter));
}
这里是不分割的解决方案:
public class LongestWord {
public static void longestWordCalculator(String rawText)
{
int lastSeparator = -1;
int maxLength = 0;
int solPosition = 0;
int textLength = rawText.length();
for (int i = 0; i < textLength ; i++)
{
//here you may check for other separators
if (rawText.charAt(i) == ' ') {
lastSeparator = i;
}
//assuming no separator is part of a word
else {
if (i - lastSeparator > maxLength) {
maxLength = i - lastSeparator;
solPosition = lastSeparator;
}
}
}
if (maxLength > 0) {
String solution = rawText.substring(solPosition+1, solPosition+maxLength+1);
System.out.println("Longest word is: " + solution);
}
else {
System.out.println("No solution finded!");
}
}
public static void main (String args[]) {
longestWordCalculator("la casa de mi amigo");
longestWordCalculator("quiero agua");
longestWordCalculator("bblablabla");
longestWordCalculator("");
}
}
输出:
Longest word is: amigo
Longest word is: quiero
Longest word is: bblablabla
No solution finded!
发布的两个答案很好,我会用它们,但是,如果您想保留当前的实现并避免使用数组等,则可以用[C0保存新的最长长度来保存当前最长字符串的开始位置。 ]。最后,将longestIndex = i - lengthCounter
中的子字符串从rawText
打印到longestIndex
。
编辑-尝试类似的事情
longestIndex + finalCounter
用空格字符分隔文本并找到最长的单词。尝试此代码。
int lengthCounter = 0;
int finalCounter = 0;
int textLength = rawText.length();
int longestIndex = 0;
for (int i = 0; i < textLength ; i++)
{
String indexValue = Character.toString(rawText.charAt(i));
if(!" ".equals(indexValue))
{
lengthCounter++;
}
else
{
if (lengthCounter > finalCounter) {
longestIndex = i - lengthCounter;
finalCounter = lengthCounter;
}
lengthCounter = 0;
}
}
System.out.println("Final Value: " + finalCounter);
System.out.println("Longest Word: " + rawText.substring(longestIndex, longestIndex + finalCounter));
输入:“看起来不错”
输出:最长的单词是[Word = looks,WordLength = 5,WordIndex = 1]
使用public class Test {
public static void longestWordCalculator(String rawText) {
String[] words = rawText.trim().replaceAll(" +", " ").split(" ");
int foundIndex = -1;
int maxLenght = 0;
String foundWord = "";
for (int i = 0; i < words.length; i++) {
if (words[i].length() > maxLenght) {
maxLenght = words[i].length();
foundWord = words[i];
foundIndex = i;
}
}
System.out.println(String.format("Longest word is [Word=%s, WordLength=%s, WordIndex=%s]", foundWord, maxLenght, foundIndex));
}
public static void main(String args[]) {
longestWordCalculator("It looks good");
}
}
和Pattern
和Pattern
循环。类似,
Matcher
我保留您的代码,不使用数组。这是更新的代码:
Matcher
希望它可以提供帮助。
这里的解决方案很巧妙,它实际上将打印出最长的单词,而不只是一个单词。
while
尽管您说这是一项作业,但我鼓励理解代码。
可以使用public static void longestWordCalculator(String rawText) {
Pattern p = Pattern.compile("(\\S+)\\b");
Matcher m = p.matcher(rawText);
String found = null;
while (m.find()) {
String s = m.group(1);
if (found == null || s.length() > found.length()) {
found = s;
}
}
if (found == null) {
System.out.println("No words found");
} else {
System.out.printf("The longest word in \"%s\" is %s which is %d characters.%n",
rawText, found, found.length());
}
}
将字符串拆分为字符串数组,然后循环遍历每个元素以检查单词的长度。在此示例中,我们假设没有特殊字符,并且单词之间用空格隔开。
此函数将找到第一个最长的单词,即,如果有平局,它将输出最长的第一个单词。在下面的示例中,您可以看到public class Test {
public static void longestWordCalculator(String rawText) {
int lengthCounter = 0;
int finalCounter = 0;
int textLength = rawText.length();
StringBuffer processingWord = new StringBuffer();
String foundWord = null;
for (int i = 0; i < textLength; i++) {
String indexValue = Character.toString(rawText.charAt(i));
if (!" ".equals(indexValue)) {
processingWord.append(rawText.charAt(i));
lengthCounter++;
} else {
if (finalCounter < lengthCounter) {
finalCounter = lengthCounter;
foundWord = processingWord.toString();
processingWord = new StringBuffer();
}
lengthCounter = 0;
}
}
System.out.println("Final Value: " + finalCounter + ", Word: " + foundWord);
}
public static void main(String args[]) {
longestWordCalculator("It looks good");
}
}
和public class Main {
public static void main(String[] args) {
String string = "one two three four five six seven eight nine";
String currentWord = "";
String largestWords = "";
int longestLength = 0;
for (int i = 0; i < string.length(); i++) {
char iteratedLetter = string.charAt(i);
if (iteratedLetter == ' ') { // previous word just ended.
if (currentWord.length() > longestLength) {
largestWords = "";
longestLength = currentWord.length();
}
else if (currentWord.length() == longestLength) {
largestWords += currentWord + " ";
}
currentWord = "";
}
else {
currentWord += iteratedLetter;
}
}
System.out.println("Largest words: " + largestWords);
}
}
的字符数相同,但仅输出split()
。
looking
用法:longest
输出
最长的字:看起来
长度:7
编辑:
不使用数组:
looking