Reputation: 93
Currently, I am able to figure out how to display the longest word length. However, is there a way using loops to determine the longest word and also print it out?
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));
}
Upvotes: 1
Views: 954
Reputation: 2813
Can use split()
to split the string into a string array, then cycle through each element to check the length of the word. In this example, we're assuming there are no special characters and words are separated by white-space.
This function will find the first longest word, i.e. if there is a tie it will output the first word with the longest length. In the example below, you can see both looking
and longest
have the same number of characters, but it will only output 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());
}
Usage:
longestWordCalculator("Hello I am looking for the longest word");
Outputs
Longest word: looking
With length of: 7
EDIT:
Without using arrays:
public static void longestWordCalculator(String rawText)
{
int nextSpaceIndex = rawText.indexOf(" ") + 1;
String longestWord = "";
do {
String word = rawText.substring(0, nextSpaceIndex);
rawText = rawText.substring(nextSpaceIndex); // trim string
if (word.length() > longestWord.length()) {
longestWord = word;
}
int tempNextIndex = rawText.indexOf(" ") + 1;
nextSpaceIndex = tempNextIndex == 0 ? rawText.length() : tempNextIndex;
} while (rawText.length() > 0);
System.out.println("Longest word: " + longestWord);
System.out.println("With length of: " + longestWord.length());
}
Upvotes: 1
Reputation: 101
The two answers posted are good and I would use them, however if you wanted to keep your current implementation and avoid arrays etc, you can save the location of the start of the current longest string when you save new longest length with longestIndex = i - lengthCounter
. At the end, print out the substring in rawText
from longestIndex
to longestIndex + finalCounter
.
Edit - try something like this
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));
Upvotes: 1
Reputation: 56
This solution here is slick, and it will actually print out all the words with the longest length, instead of just one.
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);
}
}
I encourage understanding the code though as you stated this is an assignment.
Upvotes: 2
Reputation: 264
I keep your code, not using array. Here is updated code:
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");
}
}
Hope it can help.
Upvotes: 1
Reputation: 1166
Here is an solution without split:
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("");
}
}
Output:
Longest word is: amigo
Longest word is: quiero
Longest word is: bblablabla
No solution finded!
Upvotes: 2
Reputation: 201447
Use a Pattern
and a Matcher
and a while
loop. Something like,
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());
}
}
Upvotes: 1
Reputation: 264
Split the text by space char and find the longest word. Try this code.
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");
}
}
Input: "It looks good"
Output: Longest word is [Word=looks, WordLength=5, WordIndex=1]
Upvotes: 1