如何在Java中打印字符串中最长的单词而不使用数组和循环
目前,我能够找出如何显示最长的字长。但是,有没有一种方法可以使用循环来确定最长的单词并将其打印出来如何在Java中打印字符串中最长的单词而不使用数组和循环,java,loops,Java,Loops,目前,我能够找出如何显示最长的字长。但是,有没有一种方法可以使用循环来确定最长的单词并将其打印出来 public static void longestWordCalculator(String rawText) { int lengthCounter = 0; int finalCounter = 0; int textLength = rawText.length(); for (int i = 0; i < textLength ; i++)
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));
}
公共静态void longestWordCalculator(字符串rawText)
{
int长度计数器=0;
int finalCounter=0;
int textLength=rawText.length();
for(int i=0;i
可以使用split()
将字符串拆分为字符串数组,然后循环遍历每个元素以检查单词的长度。在本例中,我们假设没有特殊字符,单词之间用空格分隔
此函数将查找第一个最长的单词,即如果有一个tie,它将输出长度最长的第一个单词。在下面的示例中,您可以看到looking
和longest
具有相同的字符数,但它只输出looking
public static void longestWordCalculator(String rawText)
{
int textLength = rawText.length();
String longestWord = "";
String[] words = rawText.split("\\s");
for (int i = 0; i < words.length; i++)
{
if (words[i].length() > longestWord.length()) {
longestWord = words[i];
}
}
System.out.println("Longest word: " + longestWord);
System.out.println("With length of: " + longestWord.length());
}
可以使用split()。在本例中,我们假设没有特殊字符,单词之间用空格分隔
此函数将查找第一个最长的单词,即如果有一个tie,它将输出长度最长的第一个单词。在下面的示例中,您可以看到looking
和longest
具有相同的字符数,但它只输出looking
public static void longestWordCalculator(String rawText)
{
int textLength = rawText.length();
String longestWord = "";
String[] words = rawText.split("\\s");
for (int i = 0; i < words.length; i++)
{
if (words[i].length() > longestWord.length()) {
longestWord = words[i];
}
}
System.out.println("Longest word: " + longestWord);
System.out.println("With length of: " + longestWord.length());
}
按空格字符分割文本并查找最长的单词。试试这个代码
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");
}
}
公共类测试{
公共静态void longestWordCalculator(字符串文本){
String[]words=rawText.trim().replaceAll(“+”,”).split(“”);
int foundIndex=-1;
int maxLenght=0;
字符串foundWord=“”;
for(int i=0;iMaxLength){
maxLenght=words[i].length();
foundWord=单词[i];
foundIndex=i;
}
}
System.out.println(String.format(“最长的单词是[word=%s,WordLength=%s,WordIndex=%s]”,foundWord,maxLenght,foundIndex));
}
公共静态void main(字符串参数[]){
longestWordCalculator(“看起来不错”);
}
}
输入:“看起来不错”
输出:最长的单词是[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");
}
}
公共类测试{
公共静态void longestWordCalculator(字符串文本){
String[]words=rawText.trim().replaceAll(“+”,”).split(“”);
int foundIndex=-1;
int maxLenght=0;
字符串foundWord=“”;
for(int i=0;iMaxLength){
maxLenght=words[i].length();
foundWord=单词[i];
foundIndex=i;
}
}
System.out.println(String.format(“最长的单词是[word=%s,WordLength=%s,WordIndex=%s]”,foundWord,maxLenght,foundIndex));
}
公共静态void main(字符串参数[]){
longestWordCalculator(“看起来不错”);
}
}
输入:“看起来不错”
输出:最长的单词是[word=looks,WordLength=5,WordIndex=1]在循环时使用a和a以及a。大概
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());
}
}
使用a和a以及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());
}
}
发布的两个答案都很好,我会使用它们,但是如果您希望保留当前实现并避免数组等,则可以在使用longestIndex=I-lengthCounter
保存新的最长长度时保存当前最长字符串的起始位置。最后,将rawText
中的子字符串从longestinex
打印到longestinex+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));
int lengthCounter=0;
int finalCounter=0;
int textLength=rawText.length();
int longestIndex=0;
for(int i=0;i最终计数器){
longestIndex=i-长度计数器;
最终计数器=长度计数器;
}
长度计数器=0;
}
}
System.out.println(“最终值:+finalCounter”);
System.out.println(“最长单词:+rawText.substring(longestinex,longestinex+finalCounter));
发布的两个答案很好,我会使用它们,但是如果您想保留当前实现并避免数组等,可以在使用longestIndex=I-lengthCounter保存新的最长长度时保存当前最长字符串的起始位置。最后,将rawText
中的子字符串从longestinex
打印到longestinex+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));
int lengthCounter=0;
int finalCounter=0;
int textLength=rawText.length();
int longestIndex=0;
for(int i=0;i最终计数器){
longestIndex=i-长度计数器;
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);
}
}