Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Find the longest word in string - code has bugs

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Find the longest word in string - code has bugs

    I am writing the following code to display the longest word in a sentence and there are bugs which I have no idea how to solve this. Please help me.

    Input st = aaaa bbbb ccccc ddd eee ffff aaaa bbbb ccccc ddd eee ffff aaaa bbbb ccccc ddd eee ffff
    Output = remaining: ddd eee ffff

    If it is a shorter sentence, the code works fine.
    Input st = aaaa bbbb ccccc ddd eee ffff aaaa bbbb
    Output = longest: ccccc

    public static void charNum(String st) {
    		String first_word;
    		int word_length;
    		String longest_word = "";
    		int temp_wordcount = 0;
    		String remaining_words = "";
     
    		for (int i = 0; i < st.length(); i++) {
     
    			word_length = st.indexOf(' '); 
    			if (word_length<0) {
    				break;
    			}
    			first_word = st.substring(0, word_length); 
    			remaining_words = st.substring(word_length+1, st.length()); 
    			if (word_length > temp_wordcount) {
    				longest_word = first_word;
    			}
    			temp_wordcount = longest_word.length();
    			st = st.substring(word_length+1);
    		}
     
    		if (remaining_words.length() > longest_word.length()) {
    			System.out.println("remaining: " + remaining_words);
    		}
    		else {
    			System.out.println("longest: " +longest_word); 
    		}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Find the longest word in string - code has bugs

    Can you describe in English the steps the code needs to take to solve the problem?
    Normally I can read the comments in code to see what it is trying to do and how it is going to do it, but the posted code has no comments.

    What is the expected output from the code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    eugene3204 (October 17th, 2020)

  4. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Find the longest word in string - code has bugs

    Quote Originally Posted by Norm View Post
    Can you describe in English the steps the code needs to take to solve the problem?
    Normally I can read the comments in code to see what it is trying to do and how it is going to do it, but the posted code has no comments.

    What is the expected output from the code?
    Hi Norm,
    Thank you for your reply. I have added the comments below and hope it makes it more clear.
    The expected output from the code is to display the longest word in sentence. The problem is that it works fine for shorter sentence. But for longer sentence it leaves out the last few words. For example:

    Shorter sentence - it works fine to display the longet word in the sentence.
    Input: Returns the char value at the specified index.
    Output: longest: specified

    Longer sentence - it displays the last few words in the sentence
    Input: If the char value specified at the given index is in the high-surrogate range, the following index is less than the length of this String, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned.
    Output: longest: to this surrogate pair is returned.

    /** Method displays the longest word in a sentence**/
    public static void charNum(String st) { //takes in a string st as parameter
                    // define variables
    		String first_word;
    		int word_length;
    		String longest_word = "";
    		int temp_wordcount = 0;
    		String remaining_words = "";
     
                    // loop throgh the input string st
    		for (int i = 0; i < st.length(); i++) {
    			word_length = st.indexOf(' '); // find the index of the first space in string and store it in variable word_length 
    			if (word_length<0) { // the last word has no space at the end and index of space is -1
    				break;
    			}
    			first_word = st.substring(0, word_length);  // extract the first word of the input string from index 0 to the first space and store it in variable first_word
    			remaining_words = st.substring(word_length+1); // remove the first word from the string. The remaining words in string is stored in variable remaining_words
    			if (word_length > temp_wordcount) { // if number of characters of the first word is greater than the number of characters of the longest word , first word becomes the longest word stored in variable longest_word
    				longest_word = first_word;
    			}
    			temp_wordcount = longest_word.length(); // replace the number of characters of the longest word 
    			st = remaining_words; // first word of string is removed and stored in parameter st
    		}
                    // the loop does not account for the last word of the string because there is no space after the last word 
    		if (remaining_words.length() > longest_word.length()) { // if the number of characters in last word of string is greater than variable longest_word from the loop, display the last word. Otherwise, display longest_word 
    			System.out.println("longest: " + remaining_words);
    		}
    		else {
    			System.out.println("longest: " +longest_word); 
    		}

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Find the longest word in string - code has bugs

    Input: If the char value specified at the given index is in the high-surrogate range,
    the following index is less than the length of this String,
    and the char value at the following index is in the low-surrogate range,
    then the supplementary code point corresponding to this surrogate pair is returned.
    What is all that about? This description contains lots of terms that require some definitons:
    Can you explain what high-surrogate and low-surrogate range means?
    How is the index computed? What is in it?
    What is the following index? How is it computed?
    What is supplementary code point? Why is it returned?

    Why is the code that saves the current longest word separated from the code that saves the length of the current longest word? Either put them together or get rid of the separate length variable and just use the .length() method to get the length of the current longest word.

    How are you debugging the code? Add some print statements that show the values of the variables as they are used and changed.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Oct 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Find the longest word in string - code has bugs

    Quote Originally Posted by Norm View Post
    What is all that about? This description contains lots of terms that require some definitons:
    Can you explain what high-surrogate and low-surrogate range means?
    How is the index computed? What is in it?
    What is the following index? How is it computed?
    What is supplementary code point? Why is it returned?
    This is just the input sentence for the code to find the longest word. The meaning of it does not matter, which you can replace it with any other sentences. This is a sentence I copied from the oracle doc. My point is that the code works when the sentence is short but not long sentence.

    Quote Originally Posted by Norm View Post
    Why is the code that saves the current longest word separated from the code that saves the length of the current longest word? Either put them together or get rid of the separate length variable and just use the .length() method to get the length of the current longest word.
    Which line of code are you referring to please?

    Quote Originally Posted by Norm View Post
    How are you debugging the code? Add some print statements that show the values of the variables as they are used and changed.
    I have printed every single line of code but still do not understand why the for loop stops and leaves the last few words of a long sentence. Just hope I can get some help here.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Find the longest word in string - code has bugs

    I have printed every single line of code but still do not understand why the for loop stops
    Can you copy and paste here the contents of the printed out values of the variables used in the code?
    There will be a clue in the printed values that will help you solve the problem.

    Which line of code are you referring to please?
    Get rid of the variable: temp_wordcount. Use the .length() method of the current longest word.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Oct 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Find the longest word in string - code has bugs

    Quote Originally Posted by Norm View Post
    Can you copy and paste here the contents of the printed out values of the variables used in the code?
    There will be a clue in the printed values that will help you solve the problem.
    This is my revised code:
    public static void charNum(String st) {
    		// define variables
    		String first_word;
    		int word_length;
    		String longest_word = "";
    		int temp_wordcount = 0;
    		String remaining_words = "";
     
    		for (int i = 0; i < st.length(); i++) {
    			System.out.println("1. sentence: " + st);
     
    			word_length = st.indexOf(' '); // find the index of the first space in string and store it in variable word_length 
    			System.out.println("2. number of characters: " + word_length);
     
    			if (word_length<0) {// the last word has no space at the end and index of space is -1
    				break;
    			}
    			first_word = st.substring(0, word_length); // extract the first word of the input string from index 0 to the first space and store it in variable first_word
    			System.out.println("3. first word: " + first_word);
     
    			remaining_words = st.substring(word_length+1); // remove the first word from the string. The remaining words in string is stored in variable remaining_words
    			System.out.println("4. remaining sentence: "+remaining_words);
     
    			if (word_length > longest_word.length()) { // if number of characters of the first word is greater than the number of characters of the longest word , first word becomes the longest word stored in variable longest_word
     
    				longest_word = first_word;
    			}
    			System.out.println("5. longest word: "+longest_word);
     
    			st = remaining_words; // first word of string is removed and stored in parameter st
    			System.out.println("message: " + st + "\n--------------------------");
    		}
                    // the loop does not account for the last word of the string because there is no space after the last word 
    		if (remaining_words.length() > longest_word.length()) { // if the number of characters in last word of string is greater than variable longest_word from the loop, display the last word. Otherwise, display longest_word 
    			System.out.println("remaining: " + remaining_words);
    		}
    		else {
    			System.out.println("longest: " +longest_word); 
    		}

    The output is very long. At the end of the output, the loop stops at the last 3 words.
    Output:

    1. sentence: A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 1
    3. first word: A
    4. remaining sentence: digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: A
    message: digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 7
    3. first word: digital
    4. remaining sentence: computer stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: digital
    message: computer stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: computer stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 8
    3. first word: computer
    4. remaining sentence: stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: computer
    message: stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: stores information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 6
    3. first word: stores
    4. remaining sentence: information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: computer
    message: information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: information as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 11
    3. first word: information
    4. remaining sentence: as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: as numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 2
    3. first word: as
    4. remaining sentence: numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: numbers (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 7
    3. first word: numbers
    4. remaining sentence: (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: (1234). A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 7
    3. first word: (1234).
    4. remaining sentence: A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: A digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 1
    3. first word: A
    4. remaining sentence: digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: digital computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 7
    3. first word: digital
    4. remaining sentence: computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: computer stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 8
    3. first word: computer
    4. remaining sentence: stores information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: stores information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: stores information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 6
    3. first word: stores
    4. remaining sentence: information as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: information as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: information as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 11
    3. first word: information
    4. remaining sentence: as numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: as numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: as numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 2
    3. first word: as
    4. remaining sentence: numbers (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: numbers (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: numbers (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 7
    3. first word: numbers
    4. remaining sentence: (1234). A digital computer stores information as numbers (1234).
    5. longest word: information
    message: (1234). A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: (1234). A digital computer stores information as numbers (1234).
    2. number of characters: 7
    3. first word: (1234).
    4. remaining sentence: A digital computer stores information as numbers (1234).
    5. longest word: information
    message: A digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: A digital computer stores information as numbers (1234).
    2. number of characters: 1
    3. first word: A
    4. remaining sentence: digital computer stores information as numbers (1234).
    5. longest word: information
    message: digital computer stores information as numbers (1234).
    --------------------------
    1. sentence: digital computer stores information as numbers (1234).
    2. number of characters: 7
    3. first word: digital
    4. remaining sentence: computer stores information as numbers (1234).
    5. longest word: information
    message: computer stores information as numbers (1234).
    --------------------------
    1. sentence: computer stores information as numbers (1234).
    2. number of characters: 8
    3. first word: computer
    4. remaining sentence: stores information as numbers (1234).
    5. longest word: information
    message: stores information as numbers (1234).
    --------------------------
    1. sentence: stores information as numbers (1234).
    2. number of characters: 6
    3. first word: stores
    4. remaining sentence: information as numbers (1234).
    5. longest word: information
    message: information as numbers (1234).
    --------------------------
    1. sentence: information as numbers (1234).
    2. number of characters: 11
    3. first word: information
    4. remaining sentence: as numbers (1234).
    5. longest word: information
    message: as numbers (1234).
    --------------------------
    remaining: as numbers (1234).

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Find the longest word in string - code has bugs

    he loop stops at the last 3 words.
    Why did the loop stop? Look at the values control the looping: i and st.length() What were their values at the end of each loop?

    The output would be easier to understand if the name of the variable was printed with its value:
          System.out.println("1. st: " + st + "<");
     
    			System.out.println("st: " + st + "<\n--------------------------");

    Why is the input changing? It is better to use the same input until the problem is solved.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    eugene3204 (October 17th, 2020)

  11. #9
    Junior Member
    Join Date
    Oct 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Find the longest word in string - code has bugs

    Quote Originally Posted by Norm View Post
    Why did the loop stop? Look at the values control the looping: i and st.length() What were their values at the end of each loop?
    I found the problem now. This is the output of i and st.length()
    i: 0
    st.length(): 168
    =================
    i: 1
    st.length(): 160
    =================
    i: 2
    st.length(): 151
    =================
    i: 3
    st.length(): 144
    =================
    i: 4
    st.length(): 132
    =================
    i: 5
    st.length(): 129
    =================
    i: 6
    st.length(): 121
    =================
    i: 7
    st.length(): 113
    =================
    i: 8
    st.length(): 111
    =================
    i: 9
    st.length(): 103
    =================
    i: 10
    st.length(): 94
    =================
    i: 11
    st.length(): 87
    =================
    i: 12
    st.length(): 75
    =================
    i: 13
    st.length(): 72
    =================
    i: 14
    st.length(): 64
    =================
    i: 15
    st.length(): 56
    =================
    i: 16
    st.length(): 54
    =================
    i: 17
    st.length(): 46
    =================
    i: 18
    st.length(): 37
    =================
    i: 19
    st.length(): 30
    =================
    i: 20
    st.length(): 18
    =================
    remaining: as numbers (1234).

    At the end of the loop,
    i: 20
    st.length(): 18

    Now i > st.length() and that's why the loop stops looping. And there are a few words left in the sentence to be looped. That is why this code does not work for longer sentences.
    So is there any idea how I can solve this problem please?

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Find the longest word in string - code has bugs

    how I can solve this problem please?
    The value in i continues to increase as the length of st decreases.

    Don't use i and st.length to control the loop. The value in i is never used in the loop so it is not needed.
    What other condition can be used to end the loop?
    The code has a break statement that will end the loop. Can that be used?
    If so write an infinite loop with while(true) and get rid of the for statement.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #11
    Junior Member
    Join Date
    Oct 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Find the longest word in string - code has bugs

    Quote Originally Posted by Norm View Post
    The value in i continues to increase as the length of st decreases.

    Don't use i and st.length to control the loop. The value in i is never used in the loop so it is not needed.
    What other condition can be used to end the loop?
    The code has a break statement that will end the loop. Can that be used?
    If so write an infinite loop with while(true) and get rid of the for statement.
    Yes a while loop fixes the problem! Thank you so much Norm!

Similar Threads

  1. [SOLVED] Find the the longest decreasing row of numbers in a vector
    By einar123 in forum What's Wrong With My Code?
    Replies: 27
    Last Post: September 24th, 2013, 09:55 AM
  2. How to find Standalon word in the string
    By Shailendra.Shukla in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 21st, 2013, 05:48 PM
  3. [SOLVED] Display longest string on screen
    By mwardjava92 in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2012, 12:12 PM
  4. Methodology to find bugs
    By tarkal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 10th, 2011, 04:02 PM
  5. Replies: 5
    Last Post: January 30th, 2009, 09:31 PM