计数字数以字符串java中的UpperCase字母开头 [英] Counting number of words start with UpperCase letter in strings, java

查看:227
本文介绍了计数字数以字符串java中的UpperCase字母开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一个Java程序,分别计算单行开头的每个行中的单词数量,就像在txt文件中一样,打印在该行中以UpperCase开头的单词数旁边的行号。

I have tried to write a Java program that count number of words start with UpperCase in each line separately, like in a txt file, and print the line number next to the number of words start with UpperCase in that line.

我只讨论如何使用以下方法计算单行数:

I have only come out with how to count the number for a single line using:

Scanner in = new Scanner(System.in);
String s = new String();
System.out.println("Enter a line:");
s = " " + in .nextLine();
char ch;
int count = 0;
for (int i = 1; i < s.length(); i++) {
    ch = s.charAt(i);
    if (Character.isUpperCase(ch) && (i == 0 || Character.isWhitespace(s.charAt(i - 1)))) {
        count++;
    }
}
System.out.println("total  number of words start with capital letters are :" + count);

我试图按照我想要的方式进行,但它一直显示文件是空的 :

I tried to do it on the way I want, but it keep showing me "File is empty":

FileInputStream in = new FileInputStream("io-02.txt");
Scanner inScanner = new Scanner(in);
FileOutputStream out = new FileOutputStream("io-02-out.txt");
PrintWriter pwr = new PrintWriter(out);

int linenumb=0;
String s="";
char c;
int count = 0;
inScanner.useDelimiter("");

for (int i = 1; i < s.length(); i++) {
    s = " " + inScanner.nextLine().trim();
    c = s.charAt(i);
    if (Character.isUpperCase(c) && (i == 0 || Character.isWhitespace(s.charAt(i - 1)))) {
        count++;
    } else if(s == "\n"){
        if(linenumb == 0)
            pwr.printf("%6s%35s%n", "Line#", "Number of Uppercase characters");

        linenumb++;
        pwr.printf("%5d.%35d%n", linenumb, count);
        count = 0;
    }
}

if(linenumb == 0)
    System.out.println("Error: The input file is empty");
else{
    linenumb++;
    pwr.printf("%5d.%35d%n", linenumb, count);
    System.out.println("The file output.txt has been created . . . ");
}

请帮忙。

推荐答案

Java 8解决方案:

Java 8 solution:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;




final public class UppercaseWordCounter { // https://stackoverflow.com/questions/49193228/counting-number-of-words-start-with-uppercase-letter-in-strings-java


    final private static File FILE_WORDS = new File("io-02.txt");
    final private static File FILE_RESULTS = new File("io-02-out.txt");


    public static void main(final String[] args) {

        if (!FILE_WORDS.exists()) {
            System.err.println("Input file does not exist: " + FILE_WORDS);
            System.exit(1);
        }
        if (FILE_RESULTS.exists()) {
            if (!FILE_RESULTS.delete()) {
                System.err.println("Intended output file exists already and can't be deleted: " + FILE_RESULTS);
                System.exit(2);
            }
        }

        try (final BufferedReader br = Files.newBufferedReader(FILE_WORDS.toPath(), StandardCharsets.UTF_8);
             final BufferedWriter bw = Files.newBufferedWriter(FILE_RESULTS.toPath(), StandardCharsets.UTF_8)) {

            int lineCounter = 1;
            String line;
            while ((line = br.readLine()) != null) {
                final int upperCaseWordsInThisLine = countUpperCaseWords(line);
                bw.write("Line " + lineCounter + " has " + upperCaseWordsInThisLine + " upper case word" + (upperCaseWordsInThisLine == 1 ? "" : "s") + ".\n");
                lineCounter++;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.exit(0);
    }


    private static int countUpperCaseWords(final String line) {

        int ret = 0;

        final int length = line.length();
        boolean newWord = true;
        for (int i = 0; i < length; i++) {
            final char c = line.charAt(i);
            if (" .,;/".indexOf(c) >= 0) {
                newWord = true;
            } else if (newWord) {
                newWord = false;
                if (Character.isUpperCase(c)) {
                    ret++;
                }
            }
        }

        return ret;
    }


}

这篇关于计数字数以字符串java中的UpperCase字母开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆