- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在哪里编辑这段代码以从我的输入文件中找到每个单词的第一个字母,保持频率和百分比,而不是每个字符?例如我在哪里可以实现 charAt(0)
或者我需要更改/添加什么?
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class FirstWordLetters
{
public static void main(String[] args) throws FileNotFoundException
{
char[] capital = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] small = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
//Input Scanner into the system
Scanner scan;
//2. Locate file using the scanner class - search to computer data location
try {
scan = new Scanner(new File("F:/programming principles/Programming Principles - PART B/enciphered.txt"));
} catch (Exception e) { //throw exception e (meaning prevent any runtime errors)
System.out.println("File not found");
return;
}
//3. Set up int's (to count, and for the complete count.)
//the aplhabet has 26 characters so the new int will be 26. The mem. space of the array.
int[] count = new int[26];
int completeTotal = 0;
//4. Start scanning the system
//Scan every line and notify user that each line has been read properly.
//each time line has been read store value in array and increment by one
while(scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println("Line read: " + line);
char[] digit = line.toCharArray();
for(int i = 0; i < digit.length; i++) {
for(int j = 0; j < 26; j++) {
if(digit[i] == capital[j] || digit[i] == small[j]) {
count[j]++;
completeTotal = completeTotal + 1;
break;
}
}
}
}
//5. Display results
//Print the overall data - letter, frequency and percentage.
System.out.println("");
System.out.println("First Word Count"); //notify user of what has been counted? (full count)
for (int i = 0; i < 26; i++) //increment each letter frequency by one each time
{
System.out.print(" " + small[i]);
System.out.print(" " + count[i]);
//calculate and display percentage for the full count
if (count[i] > 0)
System.out.println(" " + (((float) count[i]/completeTotal)*100) + "%");
else
System.out.println(" 0%");
}
}
} //end of source code.
最佳答案
这段代码可以做到。 inputString
是将整个文件存储为单个字符串。另请注意,如果任何单词以非字母字符开头,它将崩溃。此外,它假定每个单词都用空格或换行符分隔。
int[] charCounts = new int[26];
//Separate the string into individual words
//The string " /n" tells it to look for spaces and newline characters "/n"
StringTokenizer st = new StringTokenizer(inputString, " /n");
//Loop until all the words are processed
while (st.hasMoreTokens()) {
//Select the next word
String word = st.nextToken();
//Convert the string to upper case so that lower case and upper case characters are represented the same
word = word.toUpperCase();
//Get the first character from the word
char firstChar = word.charAt(0);
//Convert the character to an integer representing it as an ASCII code
int charCode = (int) firstChar;
//Increment the count for that character by 1
//(We subtract 65 from the ASCII code because the array starts at 0 but 'A' is at 65)
charCounts[charCode - 65]++;
}
//Obviously replace this section with whatever you like. It's just to show you how to get the values out again.
for (int i = 0; i < charCounts.length; i++){
System.out.println((char)(i + 65) + ": " + charCounts[i]);
}
对于字符串“asduhasa ioajsdu ijqwjfoscn kopeurfc eqiwdfjs/nijoasdij iohwefscd aosd8ch”,输出:
A: 2
B: 0
C: 0
D: 0
E: 1
F: 0
G: 0
H: 0
I: 4
J: 0
K: 1
L: 0
M: 0
N: 0
O: 0
P: 0
Q: 0
R: 0
S: 0
T: 0
U: 0
V: 0
W: 0
X: 0
Y: 0
Z: 0
如果有什么不明白的,我会尽力解释。
关于java - 如何找到每个单词的第一个字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10639668/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!