WordCounter的设计与实现

本代码段实现了统计字母,空格,数字,其他字符,以及单词数等操作。目前仅能够通过键盘输入,预留了从文件导入的接口,后续会实现。

代码写的很LOW,有些BUG到现在还没有解决。

来几张图:

这里写图片描述

这里写图片描述
附上代码:

package wordCounter;

import java.util.Scanner;

public class WordCounter {

    static int welcome() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("HI!You can input a string and you can import from your file!And we will give you some message that you want konw!");
        System.out.println("1、input a string \n2、implort from file");
        int choice=scanner.nextInt();
        return choice;  
    }

    private static String getUserString(int choice) {
        String s = null ;
        if (choice==1) {
            //input string
            System.out.println("Please input your string!");
            Scanner scanner=new Scanner(System.in);
            s = scanner.nextLine();
        }else if (choice==2) {
            //get string from file

        }else {
            welcome();
        }

        return s;

    }
    /**
     * get number\character\black\other
     * @param s
     */
    private static void getMessageOne (String s) {

        char[] myChar = s.toCharArray();
        for (int i = 0; i < myChar.length; i++) {
            char temp = myChar[i];
            if (temp >= '0' && temp <= '9') {
                num[0]++;
            } else if (temp >= 'a' && temp <= 'z' || temp >= 'A' && temp <= 'Z') {
                num[1]++;
            } else if (temp == ' ') {
                num[2]++;
            } else {
                num[3]++;
            }
        }
    }

    private static void showMessage() {
        System.out.println("**************************");
        System.out.println("Your counter at here:");
        System.out.println("number:"+num[0]);
        System.out.println("character:"+num[1]);
        System.out.println("blank:"+num[2]);
        System.out.println("other:"+num[3]);
        System.out.println("words:"+num[4]);
        System.out.println("**************************");
        System.out.println("Thank you for your using!");
    }
    static int num[] = { 0, 0, 0, 0 ,0};// 数字、字母、空格、其他、单词



    public static void main(String[] args) throws InterruptedException {

        String s=getUserString(welcome());


        System.out.println("Now,we doing some work!Please wait!");
        //System sleep!
        //Thread.currentThread().sleep(5000);

        getMessageOne(s);
        getMessageTwo(s);
        showMessage();


    }

    private static void getMessageTwo(String s) {
        // TODO Auto-generated method stub
        System.out.println("There are the word!");
         String s1[] = s.split(" ");//
         for(int i=0;i<s1.length;i++) {
             // System.out.println(s1[i]);
              num[4]++;//计数变量
         } 


    }


}

最后附上GitHub地址:
点我点我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值