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

Thread: Delimiter function and number of words in string

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Delimiter function and number of words in string

    Up, working on my java skills.
    The isDelimiter function is suppose to return true if there is a delimiter in the string and false otherwise. The main is suppose to call this function and count the number of words in the string:

    Here is what I came up with:
    import java.util.*;
     
    public class NumberOfWordsInString {
    	public static void main (String[] args){
    		Scanner in= new Scanner (System.in);
    		String s;
    		int count =0;
    		char ch [] = new char [s.length];
    		System.out.printf("Enter string\n");
    		s= in.nextLine();
     
    		for (int i = 0; i <s.length(); i++)
    		{
    			ch[i] = s.charAt(i);
    			if (isDelimiter (ch))
    			{
    				count ++;
     
    			}
    		}
    		System.out.printf("%d\n", count);
     
    	}
     
     
     
    public static boolean isDelimiter (char c){
     
    	if (ch!= delimiter)
    	{
    		return false;
    	}
     
    	else return true;	
    	}
    }

    Somehow my method isn't being successfully called by my main and the following error msg was received:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    length cannot be resolved or is not a field
    The method isDelimiter(char) in the type NumberOfWordsInString is not applicable for the arguments (char[])

    at javaimprovements.NumberOfWordsInString.main(Number OfWordsInString.java:10)


    line 10:
    char ch [] = new char [s.length];

    and the main doesn't call the isDelimiter function


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,705
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Delimiter function and number of words in string

    and the main doesn't call the isDelimiter function
    You don't know what the main() method is or isn't doing, because you have compiler errors that prevent the code from executing. Compiler errors occur before execution and prevent execution. Runtime errors occur during execution.

    Fix your compiler errors and post the updated code with any remaining questions.

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

    Java girl (April 22nd, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Delimiter function and number of words in string

    Adding to GregBrannon's post, just so that it is completely clear to you, whenever you see "Unresolved compilation problems", 1 of 2 things will happen if you try to execute the program:
    1. the program will not run at all if previously there hasn't been a successful compilation (i.e., a corresponding .class file hasn't been generated)
    2. the program will run but will run against a .class file generated from a previous successful compilation (and so code added since the last successful compilation will not be executed)


    length cannot be resolved or is not a field
    The problem is at
    String s;
    int count = 0;
    char ch[] = new char[s.length];
    There are 2 problems here:
    1. length refers to a field, and the String class does not have such a field. (See String (Java Platform SE 7 ).) To call a method it should be length().
    2. The String s has been declared but not assigned anything.


    The method isDelimiter(char) in the type NumberOfWordsInString is not applicable for the arguments (char[])
    In the main method you have
    char ch[] = new char[s.length];
    ...
     
    for (int i = 0; i < s.length(); i++) {
        ch[i] = s.charAt(i);
        if (isDelimiter(ch)) {
            ...
    ch is declared as a char array, but the isDelimiter() method signature is
    public static boolean isDelimiter(char c)
    where its parameter c is a char (not an array).

    Note that there are further errors in the isDelimiter() method that prevent compilation, so you'll also have to fix those before you can execute this program.

  5. The Following User Says Thank You to jashburn For This Useful Post:

    Java girl (April 22nd, 2014)

  6. #4
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Delimiter function and number of words in string

    Thanks for the guidance.
    Here is the modified code:

    import java.util.*;
     
    public class NumberOfWordsInString {
    	public static void main (String[] args){
    		Scanner in= new Scanner (System.in);
    		String s;
    		int count =0;
    		char ch;
    		System.out.printf("Enter string\n");
    		s= in.nextLine();
     
    		for (int i = 0; i <s.length(); i++)
    		{
    			ch = s.charAt(i);
    			if (isDelimiter (ch))
    			{
    				count ++;
     
    			}
    		}
    		System.out.printf("%d\n", count);
     
    	}
     
     
    public static boolean isDelimiter (char ch){
    Scanner in = new Scanner (System.in);
     
    while (ch != '\n')
    	if (ch!= '.' ||ch != '!'|| ch != '?')
    	{
    		return false;
    	}
     
    	else return true;	
    	}
        ch= in.nextLine();
    }

    However, how do read a single character from the keyboard?
    My problem is the line:
    ch= in.nextLine();

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Delimiter function and number of words in string

    how do read a single character from the keyboard?
    Look at the Scanner class to see if it has any methods that read in a single character.
    If you can't find any, then you will have to read the input as a String.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Java girl (April 22nd, 2014)

Similar Threads

  1. Program to find the number of occurrences of words in the text file
    By Dinesh Raja in forum What's Wrong With My Code?
    Replies: 56
    Last Post: January 10th, 2014, 10:03 AM
  2. function to get the number of a .(period)
    By gvs048 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2013, 06:42 AM
  3. Replies: 2
    Last Post: September 29th, 2012, 07:42 PM
  4. Can't figure out how to print out number of lines and words in a file in c++
    By javapenguin in forum Other Programming Languages
    Replies: 1
    Last Post: January 29th, 2012, 06:53 PM
  5. Question: Converting number to words.
    By shamed in forum Java Theory & Questions
    Replies: 6
    Last Post: January 1st, 2010, 03:28 AM