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 8 of 8

Thread: Begginer Java - Bug in my code?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Beginner Java - Bug in my code?

    Hi, I'm a beginner in java.

    I have to write a program that takes a string containing a sentence or a set of sentences, and counts the number of words in the sentence that meet or exceed a specified minimum length (in letters). For example, if the minimum length entered is 4, your program should only count words that are at least 4 letters long.

    Input the string and the minimum word length (integer), in that order, and output the word count (integer). Words will be separated by one or more spaces. Non-letter characters (spaces, punctuation, digits, etc.) may be present, but should not count towards the length of words.

    This is the code that I have written:

    public class WordCount {
    	public static void main(String [] args){	
     
    		System.out.println("Enter a sentence:");
    		String sentence = IO.readString();
     
    		System.out.println("Enter the word length limit");
    		int limit = IO.readInt();
     
     
    		int sentencelength = sentence.length();
    		System.out.println("The sentence length is:" + sentencelength);
    		int lettercount = 0;
    		int finalcount = 0;
     
    		for (int i=0; i < sentencelength; i++){
    			if (sentence.charAt(i) == ' '){
    				if (lettercount >= limit){
    				finalcount ++;
    				lettercount = 0;
    				}
    			}else{
    				if(Character.isLetter(sentence.charAt(i))){
    				lettercount ++;
    				}
    			}
    		}
     
    		System.out.println("The number of words with a length of" + limit);
    		IO.outputIntAnswer(finalcount);
     
    	}
    }



    If I input the string "The cow jumped over the barn and under the moon" and I input a limit of 2, my output should be 10. But my program is outputting 9. I figured out [by using system.out.println()]that my program does not count the letters of the last word in a sentence. How do I fix this bug?

    Sorry, I'm new to this forum. I didn't know how to post my code. Is this better?
    Attached Images Attached Images
    Last edited by rk2010; March 6th, 2012 at 12:20 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Begginer Java - Bug in my code?

    If you want help, you'll have to provide an SSCCE, not a screenshot. Don't forget the highlight tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member Aju_21's Avatar
    Join Date
    Mar 2012
    Location
    Mumbai
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Begginer Java - Bug in my code?

    The problem with ur code is it uses " " (space) to increment the finalcount . There is no space after last word, so it doesn;t gets count. Try appending a space to the string after u take as input or better modify ur else bracket code like this =>

    else{
    				if(Character.isLetter(sentence.charAt(i))){
    				lettercount ++;
                                    }
                                    if ((lettercount >=limit)&&(i == sentencelength-1))  // this will get u the last word
                                    {
                                        finalcount++;
    				}
    			}
    Last edited by Aju_21; March 6th, 2012 at 01:12 PM.

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Begginer Java - Bug in my code?

    Hi Aju,

    Thanks! Your solution works for the case I gave above (input string:"The cow jumped over the barn and under the moon" and input: limit = 2). But it doesn't work if i set the limit = 4 for the same string (I should get an output of 5, but the program will output 6).

    for (int i=0; i < sentencelength; i++){
    			if (sentence.charAt(i) == ' '){
    				if (lettercount >= limit){
    				finalcount ++;
    				lettercount = 0;
    				}
    			}else{
            		       if(Character.isLetter(sentence.charAt(i))){
            		       lettercount ++;
                                   }
    		               if ((lettercount >= limit)&&(i == sentencelength-1)){
                                   finalcount++;
    			       }
            	       }	
    		}

    How would i fix this?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Begginer Java - Bug in my code?

    Trace through your program with a debugger, or even some print lines, to figure out where the program flow differs from what you expect the program to do. Walk through it with a piece of paper and a pencil, comparing what you expect to happen to what actually happens. Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member Aju_21's Avatar
    Join Date
    Mar 2012
    Location
    Mumbai
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Begginer Java - Bug in my code?

    oops check this

    		for (int i=0; i < sentencelength; i++){
    			if (sentence.charAt(i) == ' '){
    				if (lettercount >= limit){
    				finalcount ++;
    				}
                                    lettercount = 0;
    			}else{
    				if(Character.isLetter(sentence.charAt(i))){
    				lettercount ++;
                                    }
                                    if ((lettercount >= limit)&&(i == sentencelength-1))
                                    {
                                        finalcount++;
    				}
    			}
    		}


    the thing to do is move lettercount = 0 out of the if (lettercount >= limit) condition loop
    because irrespective of the count greater or equal to limit,
    after every space, the counting of letters should begin with 0.

    Now it will work for all limits.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Begginer Java - Bug in my code?

    I actually figured it out, before I checked back on here. But thank you!!

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Begginer Java - Bug in my code?

    Aju, please read this: http://www.javaprogrammingforums.com...n-feeding.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  2. help im a begginer
    By ivankov2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 24th, 2011, 07:33 AM
  3. [SOLVED] Begginer Question please help ASAP! (Easy to Answer)
    By Bagzli in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2010, 10:00 PM
  4. begginer wondering why his guessing game won't work
    By Ligawulf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 8th, 2010, 12:22 AM
  5. Any ideas for begginer?
    By SwEeTAcTioN in forum Java Theory & Questions
    Replies: 11
    Last Post: October 27th, 2009, 03:28 AM

Tags for this Thread