java uppercase方法,计数字数以字符串java中的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 . . . ");

}

Please help.

解决方案

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值