Question

In: Computer Science

Write a java program to read a string from the keyboard, and count number of digits,...

Write a java program to read a string from the keyboard, and count number of digits, letters, and whitespaces on the entered string. You should name this project as Lab5B.

  • This program asks user to enter string which contains following characters: letters or digits, or whitespaces. The length of the string should be more than 8. You should use nextLine() method to read string from keyboard.

  • You need to extract each character, and check whether the character is a letter or digit or whitespace.

  • You can access character at a specific index using charAt() method. Once you have extracted the character, you can test the extracted character using character test methods.

  • You can look at google/web search to check the methods (isDigit, isLetter, and isSpaceChar) available to test a character. Use the syntax properly in your code.

  • Once you found a match, simply increase the value of the counter by 1. You need 3

    separate counters to count letters, digits, and spaces.

  • In this program, you do not need to worry about uppercase or lowercase letter.

       Sample Output 1
    
       Enter the string: EEEEEEE RRR LLLLLL UUUUUUUUUU
       Number of spaces: 3
       Number of letters: 26
       Number of digits: 0
    

Solutions

Expert Solution

import java.util.*;// util package is used for using Scanner class

class Lab5B {
        public static void main (String[] args) {
                String str;// variable to store string entered
                int d_count=0,l_count=0,s_count=0,len;//variables for digit count,letter count,space count and length 
                Scanner s= new Scanner(System.in);//used to read string from keyboard
                System.out.println("Enter the string");
                str=s.nextLine(); // Reading the string
                len=str.length(); // Calculating the length of the string
                if(len<=8)// if length of string is less than or equal to 8 then this message is printed
                System.out.println("Enter the string with length more than 8");
                else// if length of string is more than 8 else is executed
                {
                    for(int i=0;i<len;i++)// loop iterates from index 0 to length of string
                    {
                       char c=str.charAt(i); // extracting the character present at ith index
                        if(Character.isLetter(c))// isLetter is used to determine whether the character is alphabet or not 
                        l_count++;// incrementing letters count 
                        else if(Character.isDigit(c))//isDigit is used to determine whether the character is digit or not
                        d_count++;//if it is digit then digit count is incremented
                        else if(Character.isSpaceChar(c))//isSpaceChar is used to determine whether the character is space(" ") or not
                        s_count++;// if space then incrementing the space count
                    }
                    System.out.println("Number of spaces: "+s_count);
                    System.out.println("Number of letters: "+l_count);
                    System.out.println("Number of digits: "+d_count);
        }
        }
}

Please comment if you have any doubts.

Rate please!!!!!


Related Solutions

2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
Write a Java program that reads a whole number on the keyboard and obtains and displays...
Write a Java program that reads a whole number on the keyboard and obtains and displays twice and triple that number on the screen
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
Given a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
In this program: ================================================================== /* Program to count number of occurrences of a given string in...
In this program: ================================================================== /* Program to count number of occurrences of a given string in original string*/ #include <iostream> #include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() { const int SIZE = 40; char str[SIZE]; char str1[SIZE]; char searchString[SIZE]; int n; int l1, l2; int count = 0; printf("Enter a sentence: \n"); fgets(str,SIZE,stdin); printf("Enter a search word: \n"); fgets(searchString,SIZE,stdin); if (str1[strlen(str1) - 1] == '\n') { str1[strlen(str1)-1] = '\0'; } if (str[strlen(str) - 1] == '\n')...
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT