[英]Java counting capital letters in a text file
I am creating a text analyzer using java, what I'm trying to do is to search through the text document, and count how many Capital letter there are. 我正在使用java创建一个文本分析器,我正在尝试的是搜索文本文档,并计算有多少大写字母。
public static void main(String[] args) {
Scanner sc = new Scanner(CapitalCount.class.getResourceAsStream("test.txt"));
String s = sc.nextLine();
int upperCaseCount = 0;
int linecount = 0;
while (sc.hasNextLine()) {
sc.nextLine();
linecount++;
}
for (int i = 0; i < s.length(); i++) {
for (char c = 'A'; c <= 'Z'; c++) {
if (s.charAt(i) == c) {
upperCaseCount++;
}
}
}
System.out.println(upperCaseCount + "");
}
}
I think that i will have to do some sort of count on how many lines there are, this is why i have added a line count at the top, although I am unsure on how to implement this with the capital count that only works on the first line. 我认为我必须要考虑有多少行,这就是为什么我在顶部添加了行数,虽然我不确定如何使用仅适用于第一行。
You don't need CapitalCount class to scan a file. 您不需要CapitalCount类来扫描文件。
Below is my working solution: 以下是我的工作解决方案:
public static void main(String[] args) {
Scanner sc = null;
try {
sc = new Scanner(new File("test.txt"));
int count = 0;
while(sc.hasNext()){
String line = sc.nextLine();
for(int i = 0 ; i < line.length(); i++){
if(line.charAt(i) >= 'A' && line.charAt(i) <= 'Z'){
count ++;
}
}
}
System.out.println("The number of capital letters are : "+count);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
finally{
sc.close();
}
}
It is not necessary the internal loop to check if a letter ia capital letter. 内部循环没有必要检查字母是否是大写字母。 Change 更改
for (char c = 'A'; c <= 'Z'; c++) {
if (s.charAt(i) == c) {
upperCaseCount++;
}
}
to 至
if (s.charAt(i) >= 'A' and s.charAt(i) <= 'Z') {
upperCaseCount++;
}
Try this code: 试试这段代码:
public static void main(String[] args) {
Scanner sc = new Scanner(CapitalCount.class.getResourceAsStream("test.txt"));
int upperCaseCount = 0;
int linecount = 0;
String lineInput = null;
while (sc.hasNextLine()) {
lineInput = sc.nextLine();
linecount = lineInput .length();
for(int i=0; i < linecount; i++)
if (Character.isUpperCase(lineInput.charAt(i))) upperCaseCount++;
}
System.out.println(upperCaseCount + "");
}
You should use Character.isUpperCase()
method instead of doing it manually. 您应该使用Character.isUpperCase()
方法而不是手动执行。
public static void main(String[] args) {
try {
Scanner sc = new Scanner(new File("test.txt"));
int capitalCount = 0;
int lineCount = 0;
while(sc.hasNext()){
lineCount++;
String line = sc.nextLine();
for(int i = 0 ; i < line.length(); i++){
if(Character.isUpperCase(line.charAt(i))) {
capitalCount++;
}
}
}
System.out.println("Number of lines: "+lineCount);
System.out.println("Number of capital letters: "+capitalCount);
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
Haven't you tried to call ".next(Pattern pattern)" method with pattern for capital letters? 你有没有尝试用大写字母的模式调用“.next(Pattern pattern)”方法? Than I think you should count capital letter for every single line, so before for cycle.. 比我认为你应该计算每一行的大写字母,所以在循环之前 ..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.