计数和显示UpperCase,LowerCase和Numbers

时间:2016-02-22 07:29:56

标签: java

我的任务是在我的java类中执行此操作。我不知道该怎么做。

使用Scanner或JOptionPane,它会要求用户输入一个单词。然后它将验证该单词是由大写字母,数字,空格组成,然后它将计算字符总数。

输入一个词:_____

输入字词:“在此处显示字词”

找到的字符:

大写:

大写字母总数:

小写:

小写字母总数:

数字:

总数:

空白数量:

找到的总字符数:

4 个答案:

答案 0 :(得分:1)

应该是基础课程中涵盖的内容...编辑为ScannerJOptionPane包含数字和相关代码的计数器。

public static void main(String[] args) {

    String input = (String) JOptionPane.showInputDialog(null, "Input a sentence.", "Dialogue", JOptionPane.PLAIN_MESSAGE, null, null, null);

    // System.out.println("Input a word.");
    // @SuppressWarnings("resource") Scanner scan = new Scanner(System.in);
    // String input = scan.nextLine();

    System.out.println("Input word was: " + input);

    int length = input.length();
    char[] charAnalysis = input.toCharArray();

    int whitespace = 0;
    int lowercase = 0;
    int uppercase = 0;
    int numberCount = 0;
    for (char element : charAnalysis) {
        if (Character.isWhitespace(element)) {
            whitespace++;
        } else if (Character.isUpperCase(element)) {
            uppercase++;
        } else if (Character.isLowerCase(element)) {
            lowercase++;
        } else if (Character.isDigit(element)) {
            numberCount++;
        }
    }

    System.out.println("Length: " + length);
    System.out.println("Uppercase letters: " + uppercase);
    System.out.println("Lowercase letters: " + lowercase);
    System.out.println("Digit count: " + numberCount);
    System.out.println("Whitespaces: " + whitespace);
}

答案 1 :(得分:0)

             String word = JOptionPane.showInputDialog(frame, "enter word");
for(int i =0;i<word.length();i++)
{
        if (Character.isUpperCase(word.charAt(i))){ upperCase++; }
                        else if (Character.isLowerCase(word.charAt(i))){ lowerCase++; }
                        else if (Character.isDigit(word.charAt(i)))    { numberCount++;}
                        else if(charAt(i)=' ')     {spaceCount++}
}
    //to display word count
    System.out.println(word.length);
    //to display uppercase count
    System.out.println(upperCase);
    //to disply digits count
    System.out.println(numberCount);
    //to display space count
    System.out.println(spaceCount);

答案 2 :(得分:0)

尝试此操作并阅读课程Character以便清楚地理解。

import java.util.*;
import java.lang.*;
import java.io.*;

public class HelloWorld{

    public static void main(String[] args) {
    System.out.println("Input a word.");
    Scanner scan = new Scanner(System.in);
    String input = scan.nextLine();
    System.out.println("Entered Word: " + input);
    int length = input.length();
    char[] chars = input.toCharArray();
    int whitespaceLength = 0;
    String upercase = "";
    String lowercase = "";
    String numbers = "";
    for (char element : chars) {
        if (Character.isWhitespace(element)) {
            whitespaceLength++;
        } else if (Character.isUpperCase(element)) {
            upercase+=element;
        } else if (Character.isLowerCase(element)) {
            lowercase+=element;
        }else if (Character.isDigit(element)) {
            numbers+=element;
        }
    }
    System.out.println("Uppercase: " + upercase );
    System.out.println("total number of uppercase letters: " + upercase.length());
    System.out.println("Lowercase: " + lowercase);
    System.out.println("total number of lowercase letters: " + lowercase.length());
    System.out.println("Numbers: " + numbers);
    System.out.println("total number of Numbers: " + numbers.length());
    System.out.println("Number of Whitespaces: " + whitespaceLength);
    System.out.println("Total number of characters found: " + input.length());



    }
}

答案 3 :(得分:0)

public class Count {
public static void main(String[] args) {
    int upperCase = 0;
    int lowerCase = 0;
    int spaceCount = 0;
    int numberCount = 0;
    Scanner sc = new Scanner(System.in);
    System.out.println("enter a string ");
    String word = sc.nextLine();
    sc.close();
    for (int i = 0; i < word.length(); i++) {
        if (Character.isUpperCase(word.charAt(i))) {
            upperCase++;
        } else if (Character.isLowerCase(word.charAt(i))) {
            lowerCase++;
        } else if (Character.isDigit(word.charAt(i))) {
            numberCount++;
        } else if (word.charAt(i) == ' ') {
            spaceCount++;
        }
    }

    System.out.println("total number of uppercase letters :"+upperCase);
    System.out.println("total number of lowerCase letters :"+lowerCase);
    System.out.println("total number of Numbers :"+numberCount);
    System.out.println("Total number of whitSpaces  :"+spaceCount);
}

}