Hi guys!

i need some help with my program and i cant determine what to do next...i'm making a program that will output the longest word in a sentence..

here is my code...

import java.util.Scanner;

class LongestWord4
{
	 static Scanner console = new Scanner(System.in);
	public static void main(String args[])
	{
		 String word, longestWord = "";
		 System.out.print("Enter sentence: ");
		 Scanner console = new Scanner(System.in);
		 String sentense = console.nextLine();

		  Scanner console2 = new Scanner(sentense);
		  while ( console2.hasNext() )
		  {
		  	word=console2.next();
		  	if (word.length() > longestWord.length())
		    longestWord = word;
	  	  }
	  	  System.out.print("The longest word was: " + longestWord);
  	}
}

now my problem is whenever I try to input a sentence with a word with similar length, only the first longest word appears. Well supposedly all distinct words with similar number of characters should appear on the output.

second problem..the output must also disregard the special cases like .,/?:!, etc that is link to the longest word... e.g. get out of here!

the exclamation mark must be disregarded when outputting the word here...

so someone please help me how to figure out..cause i cant think of how to handle this problems..

THANKS!

Recommended Answers

All 12 Replies

Just a quick thought-- you could brute force this. While you are checking the lengths you could also run a check to verify that the string.length - 1's index is not equal to various punctuation marks. If it is then subtract one from the length before comparing?

when you take the sentence in, you can then you StringTokenizer class to tokenize your sentence using as a token the empty space ' '. this will break your sentence into words. next, make a method that removes punctuation from each word, in case there is any, and save your words, free of punctuation onto some array.
then, loop on this array to find the number of characters on the longest word.

with another loop, System.out each word that has the length you just found in the previous step. hope this helps.

Or, perhaps more elegantly, you could use a collection of some sort (an ArrayList, most likely) to store words of the currently longest length. So now there are three cases: less than max, longer than max, and equal to max. The first is going to remain the same. What do you do in the equal case, and how does the longer than case have to change?

This way you are still using the minimum memory footprint: you never store anything but what you need to store.

when you take the sentence in, you can then you StringTokenizer class to tokenize your sentence using as a token the empty space ' '. this will break your sentence into words. next, make a method that removes punctuation from each word, in case there is any, and save your words, free of punctuation onto some array.
then, loop on this array to find the number of characters on the longest word.

with another loop, System.out each word that has the length you just found in the previous step. hope this helps.

I believe the default delimiter is the space, so by calling string.next() he/she is already grabbing the next word.

The problem isn't with their approach-- it's with punctuation from what I understood. I offered a messy but workable solution to correctly identifying words with punctuation marks after them. This doesn't stop a punctuation mark like an apostrophe that is in the middle of a word though. Then again you could use the method I described but check for string.length() minus 1 and minus two index's for punctuation marks including the apostrophe since that comes as the second to last character in a word... taking it further you could check for index[0] as well for a starting punctuation mark like an opening quote.

Identifying the correct longest word in a sentence like this would be a nightmare:

"The fast, 'blue' mage subdued the 'purple' mage!"

By default it would probably pick up purple as being the longest when subdued is actually the longest. By brute force checking not only every word-- but the first, last and second to last index's character you could correctly gauge the real lengths. That is pretty brute force though... a better approach would probably be regex'ing the sucker but I don't know about Java regex expressions too much yet.

edit:

By the way-- I have only been learning Java for 4 days so don't take what I say too seriously. I think it's workable and valid, if it isn't let me know. :)

Oh, as for the punctuation problem, probably a regex approach is the best. I'd sub that out to a method, call it something like "trimPunctuation". Any complexity can live down there, you don't have to worry about it in your main logic. (ie, decisions like "do I count the ' in "don't"?") (<-- that's why you don't want to use an indexed solution for finding the punctuation - you'd end up scanning each letter in a loop!)

007, I think you're right. I included your suggestion for 'trimming' words, but suggested it should be done as a separate method, as kiparsky mentioned it. and yes, regex is I guess the best way to go, I can't see how I forgot about it. :)

hmm..can you please give me sample code of that method that you're talking about because unfortunately i really don't know what it is and really sounds unfamiliar...a sample code and where shall i insert it in my program would a big help and be appreciated.. THANKS!

007, I think you're right. I included your suggestion for 'trimming' words, but suggested it should be done as a separate method, as kiparsky mentioned it. and yes, regex is I guess the best way to go, I can't see how I forgot about it. :)

hmm..can you please give me sample code of that method that you're talking about because unfortunately i really don't know what it is and really sounds unfamiliar...a sample code and where shall i insert it in my program would a big help and be appreciated.. THANKS!

butz17, you should write that method yourself. it's really not difficult, you should be able to do it. here, I'll help you. Although, as other posters have suggested, using regular expressions is the best way, but not for a complete beginner, which I assume you are.

lets name the method trimming

public String trimming(String s){
   String rez = "";//to hold the result of the method

   //here, write a loop that checks each character of the string s if it is punctuation or not. if it's not, add it to your variable rez. however, be careful to make an exception for the ' character, since it is not punctuation, and may as well be part of a word

   return rez;
}

I hope this helps.

This may help you as well. It shows you the difference between a brute force method and a regex equivalent.

If you're doing it this way, use a StringBuilder.

Or look at String.replaceAll() - read the link to Regular Expressions, and see if you can figure it out.

butz17, you should write that method yourself. it's really not difficult, you should be able to do it. here, I'll help you. Although, as other posters have suggested, using regular expressions is the best way, but not for a complete beginner, which I assume you are.

lets name the method trimming

public String trimming(String s){
   String rez = "";//to hold the result of the method

   //here, write a loop that checks each character of the string s if it is punctuation or not. if it's not, add it to your variable rez. however, be careful to make an exception for the ' character, since it is not punctuation, and may as well be part of a word

   return rez;
}

I hope this helps.

OK! thanks alot! it really helps! ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.