-4

How to write a java program to get a string as a key board input. Then count the number of letters, digits and spaces available in that string and display the count of letters, digits and spaces.

Alok Gupta
  • 1,353
  • 1
  • 13
  • 29
Harshani
  • 5
  • 2
  • hopefully this [URL](http://stackoverflow.com/questions/9832195/keypress-how-to-detect-if-the-user-pressed-the-down-arrow-key-on-their-keyboa) can help you – Willie Cheng Feb 22 '16 at 06:53

2 Answers2

1

This is honestly... really, really easy and something that really should be taught to you in an introductory course or something.

Scanner scan = new Scanner(System.in);
String userInput = scan.nextLine();

You will need to put, right after package, import java.util.Scanner. To do the other thing, i.e. counting the number of characters in the String, simply invoke length().

ifly6
  • 5,003
  • 2
  • 24
  • 47
0

Pass a string as an argument in this method you get count of letter, space, and digit

public void count(String str) {
        int space = 0;
        int digit = 0;
        int letter =0;
        for (int f4 = 0; f4 < str.length(); f4++) {
            if (Character.isSpace(str.charAt(f4))) 
            space++;
            if(Character.isAlphabetic(str.charAt(f4)))
                letter++;
            if(Character.isDigit(str.charAt(f4)))
                digit++;
        }
        System.out.println("space = "+space+", digit = "+digit+", letter = "+letter); 
    }
sudhir
  • 11
  • 2