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

Thread: Write a Java program to count the letters, spaces, numbers and other characters of an input string.

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a Java program to count the letters, spaces, numbers and other characters of an input string.

    import java.util.Scanner;
     
    public class RicMain 
    {    
    	public static void main(String[] args) 
    	{
    		String test = "Hello World, it is me";
    		count(test); //sets x = test?
    		             //char[] ch = test.toCharArray()?
    	}
    	public static void count(String x)
    	{
    		char[] ch = x.toCharArray(); //converts x into an array?
    		int letter = 0;
    		int space = 0;
    		int num = 0;
     
    		for(int i = 0; i < x.length(); i++) //i < test.length()?
    		{
    			if(Character.isLetter(ch[i])) //determines how many letters 'x' has?
    			{
    				letter++ ; //letter + 1 ----> 0+1
    			}
    			else if(Character.isDigit(ch[i])) //determines how many numbers 'x' has?
    			{
    				num++ ;
    			}
    			else if(Character.isSpaceChar(ch[i])) //determines the spaces 'x' has?
    			{
    				space++ ;
    			}
    		}
    		System.out.println("The string has: ");
    		System.out.println(letter + " letters" + ", " + space + " spaces" + ", " + num + " numbers");
    	}
    }

    I have another code (I got from online), but I'm trying to understand what's going on on this code (my questions are also inside the code itself). I understand the isLetter(), isDigit(), isSpaceChar(), but how does the for loop determine how many letters, numbers, spaces there are in the String test. Also, in the inner class, the count(test), that is basically setting x equal to test, right?

    --- Update ---

    The output is:

    The string has:
    16 letters, 4 spaces, 0 numbers

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,381
    Thanks
    53
    Thanked 2,397 Times in 2,350 Posts

    Default Re: Write a Java program to count the letters, spaces, numbers and other characters of an input string.

    how does the for loop determine how many...
    Look at how a for loop is constructed. The middle expression controls how many times the loop goes around.
    The array's .length field has the length of the array.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 10
    Last Post: July 1st, 2014, 02:41 PM
  2. Replies: 6
    Last Post: June 18th, 2014, 09:18 AM
  3. Replies: 2
    Last Post: April 5th, 2014, 11:05 AM
  4. [SOLVED] Write a java program to display even numbers between 2 and 200 using netbeans java
    By Shalom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 11th, 2012, 04:16 AM
  5. Replies: 13
    Last Post: December 6th, 2011, 01:17 PM