计数字数以字符串中的UpperCase字母开头,java

时间:2018-03-09 11:54:20

标签: java loops io

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

我刚才提到如何使用以下方法计算单行数:

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);

我试着按照我想要的方式去做,但它一直在向我显示&#34;文件是空的&#34;:

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 . . . ");
}

请帮忙。

2 个答案:

答案 0 :(得分:1)

Java 8解决方案:

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;
    }


}

答案 1 :(得分:0)

为什么不使用Files 1.7中的方法,该方法可以从java 1.7

获得
lst

然后你可以循环检查条件的{{1}}列表