Results 1 to 7 of 7
  1. #1
    overcranked is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default User Defined Method

    Hey All Newbie here, I have a project due in a couple days for school that I need a little help with.
    The project is to take a user inputed string and print out how many Characters,Digits,Vowels,whitespace and letters are in the string. I was able to get the string and character length ok, but I need to create Used defined method for all the other parts.

    Here is my code so far the problem I am having is the method that I am trying to develop for whitespace counts. I keep compiling a "Illegal start of expression" fault. Can any one simply point me in the right direction please?
    This is an intro course so I can not use Regular expression yet.
    Thanks
    import java.util.*;
    public class p6
    {



    static Scanner kb = new Scanner(System.in);
    public static void main(String [] args)

    {
    String x; // declared string variables


    System.out.print("Enter a string for character classification: (EOF to end): ");
    while (kb.hasNext())//while loop needed to run EOF
    {
    x = kb.nextLine();// Reads input
    System.out.println("The inputLine is " + x.length() + " characters long and contains the following:.");

    System.out.print("Enter a string for character classification: (EOF to end): ");}


    // Method for counting white space
    public void whiteSpace(){

    int whiteSpaceCount=0;
    int i;

    for (int i=0;i<x.length();i++)
    {
    if (x.charAt(i) =' ')

    whiteSpaceCount++;

    }
    }



    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Count your curly brackets; they have to pair, in your code they don't.

    kind regards,

    Jos

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i made some small quick&dirty modifications to your code so that it runs now and counts the whitespace. note that whitespaces are not only blanks but other signs like linefeed etc.

    you can now add more methods. other suggestion: create a class for your tasks and call it from an other class where the main method is in.

    Java Code:
    package mypackage;
    
    import java.util.*;
    
    public class NewClass {
    
    	static Scanner kb = new Scanner(System.in);
    
    	public static void main(String[] args) {
    
    		String x; // declared string variables
    
    		System.out
    				.print("Enter a string for character classification: (EOF to end): ");
    
    		while (kb.hasNext())// while loop needed to run EOF
    		{
    			x = kb.nextLine();// Reads input
    			System.out.println("The inputLine is " + x.length()
    					+ " characters long and contains the following:.");
    			System.out.println("Number of whitespaces: " + whiteSpace(x));
    			// add here other method calls for digits etc.
    			
    
    			System.out
    					.print("Enter a string for character classification: (EOF to end): ");
    		}
    	}
    
    	// Method for counting white space
    	public static int whiteSpace(String x) {
    
    		int whiteSpaceCount = 0;
    		int i;
    
    		for (int j = 0; j < x.length(); j++) {
    			if (x.charAt(j) == ' ')
    
    				whiteSpaceCount++;
    
    		}
    		return whiteSpaceCount;
    	}
    
    }

  4. #4
    overcranked is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    my mistakes, I totally see what the problems were now with my brackets and, Apparently my declared method need and argument list "String X" that makes since now! you guys rock'ed it. As far as the class idea If I understand you correctly I should create my own class, that hold my methods in it, then call that class into the program? what makes that better, or beneficial as oppose to just writing my methods under my method main? Just wondering NEWBIE here. Thanks again

  5. #5
    overcranked is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    here are the changes that I made to my code I am getting a new error "Class, interface, or enum expected"
    Ill keep pluggin along. I got tell midnight wish me luck
    Thanks again

    // Method for counting white space
    public static int whiteSpace(String x){

    int whiteSpaceCount=0;


    for (int i = 0;i<x.length();i++)
    {
    if (x.charAt(i) ==' ')

    whiteSpaceCount++;
    }
    return whiteSpaceCount;



    }
    }

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by overcranked View Post
    here are the changes that I made to my code I am getting a new error "Class, interface, or enum expected"
    Ill keep pluggin along. I got tell midnight wish me luck
    You made your changes outside of your original class; your compiler expected another class or interface or enum definition, not another method. Move your method definition inside of your original class, i.e. not you have this:

    Java Code:
    public class YourClass { ... }
    public void foo() { ... }
    make it this:

    Java Code:
    public class YourClass {
       ...
       public void foo() { ... }
    }
    kind regards,

    Jos

  7. #7
    overcranked is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default final project

    import java.util.*;
    public class p6
    {

    /*method for counting vowels by passing the string "x" through the for loop by running possible vowels in the if statments
    returning the count to vowelCount*/
    public static int vowels(String x){

    int vowelCount = 0;

    for (int i = 0; i < x.length(); i++){
    if (x.charAt(i)=='a' ||x.charAt(i)=='e' ||x.charAt(i)=='i' ||x.charAt(i)=='o' ||x.charAt(i)=='u'||
    x.charAt(i)=='A' ||x.charAt(i)=='E' ||x.charAt(i)=='I' ||x.charAt(i)=='O' ||x.charAt(i)=='U')
    vowelCount++;
    }

    return vowelCount;
    }
    // Method for counting digits using isDigit and passing the "x" in the for loop returning the count to digitCount
    public static int digits(String x){

    int digitCount=0;

    for (int j=0;j<x.length();j++)
    {
    if (Character.isDigit(x.charAt(j)) == true)
    digitCount++;
    }
    return digitCount;
    }


    //Method for counting letters passes sting "x" into the for loop counts as an int returns the value to letterCount
    public static int letters(String x){

    int letterCount = 0;

    for (int i = 0; i < x.length(); i++){
    if (Character.isLetter(x.charAt(i)) == true)
    letterCount++;
    }
    return letterCount;
    }
    // Method for counting white space passes the "x" value into the for loop as an int returns the value to whiteSpaceCount
    public static int whiteSpace(String x){

    int whiteSpaceCount=0;

    for (int i = 0;i<x.length();i++)
    {
    if (Character.isWhitespace(x.charAt(i)) == true)
    whiteSpaceCount++;
    }
    return whiteSpaceCount;
    }

    static Scanner kb = new Scanner(System.in);
    //Main Method starts here
    public static void main(String [] args)

    {
    String x; // declared string variables


    System.out.print("Enter a string for character classification: (EOF to end): ");
    while (kb.hasNext())//while loop needed to run EOF
    {
    x = kb.nextLine();// Reads input
    System.out.println("The inputLine is " + x.length() + " characters long and contains the following:."
    +"\nNumber of white spaces " + whiteSpace(x) + "."
    +"\nNumber of digits are " + digits(x) + "."
    +"\nThe numbers of vowels are " + letters(x) + "."
    +"\nThe amount of letters are " + vowels(x) + ".");
    System.out.print("Enter a string for character classification: (EOF to end): ");}
    }
    }

    Here it is in all its glory, thank you all for your help! Im not sure if it is perfect because the User Defined's call on a string in the Main but we will see next week! Thanks again.

Similar Threads

  1. JSP with user-defined java classes
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-05-2010, 06:13 PM
  2. RegardingSgould Enter in user defined inbox
    By Prashant.surwade in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-23-2009, 01:21 PM
  3. Replies: 2
    Last Post: 04-28-2009, 04:52 AM
  4. How to call defined java class in Jsp
    By asheeshiit in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-04-2009, 04:21 AM
  5. Replies: 0
    Last Post: 07-16-2007, 08:45 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
  •