Results 1 to 3 of 3
  1. #1
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Count Words in a String...

    Hi All,

    I am writing a Java program and im having some trouble with it.

    What I need to do is Enter a string and then count the words but only words that contain A-Z and a-z and only if the word is more then 3 characters.
    I cant use Scanners or Buffered Readers.

    Java Code:
    class jlab51 {
    	public static void main(String[] args) {
    		int counter = 0;
    		String string;
    		boolean word = false;
    
    		System.out.print("Enter sentence -> ");
    		string = EasyIn.getString();
    		int endOfLine = string.length() - 1;
    
    		for (int index = 0; index < string.length(); index++) {
    
    			if (Character.isLetter(string.charAt(index)) == true
    					&& index != endOfLine) {
    				word = true;
    
    			} else if (Character.isLetter(string.charAt(index)) == false
    					&& word == true) {
    				counter++;
    				word = false;
    
    			} else if (Character.isLetter(string.charAt(index))
    					&& index == endOfLine) {
    				counter++;
    			}
    		}
    		System.out.println(counter);
    	}
    }
    This is what i have so far EasyIn is just a method to read in the string

    Thanks in Advance for all the help.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Count Words in a String...

    And your question is?

  3. #3
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: Count Words in a String...

    I cant get it to work properly...
    I've tried a few different things an am just not having any luck with it so far.
    Really just looking for some pointers to get me in the right direction.

Similar Threads

  1. Count Words in a String and Output First Four
    By RSH in forum New To Java
    Replies: 17
    Last Post: 07-26-2012, 11:28 AM
  2. count no of words in string
    By fakepics500 in forum New To Java
    Replies: 12
    Last Post: 07-19-2011, 10:49 AM
  3. Count Words
    By jjjkkk in forum New To Java
    Replies: 8
    Last Post: 07-18-2011, 01:24 PM
  4. Read text file and count words.
    By chdn202002 in forum New To Java
    Replies: 5
    Last Post: 07-17-2011, 02:44 PM
  5. Replies: 8
    Last Post: 02-04-2009, 08:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •