Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Longest word in a program...


    hi..

    I have here my incorrect codes for a program that finds the longest word inputted by the user separated by blank spaces...

    import java.util.*;

    public class longword
    {
    static Scanner console = new Scanner(System.in);
    public static void main(String[] args)
    {
    String word;
    String longest = "";

    System.out.print("Enter sentence: ");
    word = console.next();
    System.out.println();

    if (word.length() > longest.length())
    {
    longest = word;
    System.out.print("The longest word was: " + longest);
    }
    }
    }

    my problem is, it only displays the "FIRST" inputted string disregarding the rest of the strings and their size(number of characters)...

    appreciate your help...
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,940
    Rep Power
    3117
    That is because you are only checking the first word. There is no looping of any kind in the program.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0
    Use a while loop, until the user enters a condition which terminates the program.

    Comments on this post

    • jzd disagrees
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    245
    Rep Power
    239
    Hello and welcome to Devshed. First off, in the future please use code tags or the syntax highlighter . It makes any code you post much easier to read, which in turn will get you more help, and faster help. (shown below)

    With regards to your problem, you should be treating it like any other bug:

    1. Identify the bug. In this case, you correctly identified that the first word is always returned. The question then becomes, why. To most of us based on the responses the answer is obvious, however that does not help you for the rest of the semester (or career) if you can't understand how we see that. To properly identify a bug in a program this simple, grab a pen and paper (a debugger is the subject for a book, not a forum post ) and follow each step in your program writing out exactly what the code says to do, NOT what you know or think it should do. Some relevant places to get information about what is actually happening are the API docs and online tutorials. The API docs are the best source, especially in the long run since almost everything will have the same style documentation (you hope).

    2. Once the bug is identified, figure out what has to be changed for it to work and implement that change after backing up your current work. Otherwise you could introduce new bugs without an easy way to roll back the changes.

    3. Once you think the bug is fixed, test to confirm it is truly fixed by running several set of data through it to confirm it always works.

    Let us know when and how you solved it so we can offer additional advice if necessary or ask for more help if you need it.

    java Code:
    import java.util.*;
     
    public class longword
    {
    	static Scanner console = new Scanner(System.in);
    	public static void main(String[] args)
    	{
    		String word;
    		String longest = "";
     
    		System.out.print("Enter sentence: ");
    		word = console.next();
    		System.out.println();
     
    		if (word.length() > longest.length())
    		{
    			longest = word;
    			System.out.print("The longest word was: " + longest);
    		}
    	}
    }
    Last edited by spyder0101; August 20th, 2010 at 12:20 PM. Reason: speeling miss takes everwhere :(
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,940
    Rep Power
    3117
    Originally Posted by Jamesybaby11
    Use a while loop, until the user enters a condition which terminates the program.
    James, I disagree, actually the while loops should loop through the words in the sentence that the user enters. Ending the sentence is the condition that terminates further input.

    What is deceiving is that the programs store's the user's sentence in a variable named "word".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    245
    Rep Power
    239
    Not necessarily. It depends on if the OP thinks Scanner.next() only grabs a word or the whole sentence. The rest of his code implied to me that he knows it grabs a word only but that was part of why I asked for him to look at it and show us his (partial) solution. Either scanning the words individually or splitting the line are both valid approaches.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0
    thank you all for the replies...

    but honestly, I'm stuck with this problem and I really don't know what to do next.... and that is why I ended up registering here to seek some help...

    any related codes regarding this problem that I could use as a guide will help...

    thanks...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    245
    Rep Power
    239
    Reproduce your analysis of what the code is doing here (step 1 above) if the problem is a lack of understanding about why it isn't doing what you want it to so we can help with that or explain exactly what functionality you need added to your code to make it do what you want(ie, not just make it work) so we can guide you on how to achieve that. We do not do other people's homework here. One day you could be working with/for us and we want to make sure you learn how to do it instead of just learning how to ask for help.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    245
    Rep Power
    239
    I guess if you want some related code I can give you that, but I'm not sure if that will help.

    java Code:
    longest=(word=console.next()).length()<longest.length()?longest:word;
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0
    whew!!

    i know this problem is just easy for you, but for me its not...

    i guess i'll skip this one this time and read more...

    thank you spyder0101 for trying to help me...
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    245
    Rep Power
    239
    Learning to program is not easy. Perhaps I am misunderstanding what you know and want. My code snippet was just to illustrate that code by itself does nothing to foster understanding. That gibberish was basically exactly what you had, "bug" included (well, line 12-17 of my reproduction of your code). I'm not sure exactly where your confusion is. I assume from your variable name choice you know you are reading in exactly 1 word from a line that is being entered.

    Your code reads in that single word then compare it to the empty string to see if it is longer and print it when it is the longest word you have found. At that point the program ends

    That is step 1. Your bug is that it only reads 1 word when you want the whole sentence.

    Step 2 is up to you, as there are a few options. A good place to start is by checking out the earlier comments and the API docs forScanner. Perhaps you want to read in the next line and process from there, or maybe you'd rather have the scanner flag newlines or other certain character combinations to let you know when you are done.

    Also of interest may be loops

    Finally, I'm still not sure exactly what you don't understand. Are you confused why it only looks at the first word? Are you confused about how to make it do something multiple times? That's why you completing step 1 was important, we don't know what you are thinking it is doing or why. Writing out what you think each line is doing would still be to your benefit if you want us to be able to help more.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    103
    Originally Posted by hustlas4ever
    thank you all for the replies...

    but honestly, I'm stuck with this problem and I really don't know what to do next.... and that is why I ended up registering here to seek some help...

    any related codes regarding this problem that I could use as a guide will help...
    Here is a sample for you:
    Code:
    	  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);

    Comments on this post

    • spyder0101 disagrees : Teach a man to fish...
    • jzd disagrees : Agree with spyder
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0
    WOW!!

    that worked...!!

    unfortunately i can't pass these codes because we haven't discussed .hasNext() and my prof would obviously know that I'm not the one who wrote this...

    anyway..can you please explain this part sir:

    Code:
    Scanner console2 = new Scanner(sentence);
    because Im confused...thank you so much sir....
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    245
    Rep Power
    239
    So you not only want someone to do your homework for you, you want them to explain the solution? That's funny.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    103
    Originally Posted by hustlas4ever
    WOW!!
    anyway..can you please explain this part sir:

    Code:
    Scanner console2 = new Scanner(sentence);
    we are reading one line from console.

    Now we are trying to read the words from this line.

    Code:
    Scanner console2 = new Scanner(sentence);
    each time you do console2.next(), you would get the next word available in the sequence.

    Comments on this post

    • spyder0101 disagrees : you just don't learn, do you...
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo