Java 我如何找到每个单词的第一个字母?
我在哪里编辑此代码以从输入文件中查找每个单词的第一个字母,保留频率和百分比,而不是每个字符?e、 g.在哪里可以实现Java 我如何找到每个单词的第一个字母?,java,Java,我在哪里编辑此代码以从输入文件中查找每个单词的第一个字母,保留频率和百分比,而不是每个字符?e、 g.在哪里可以实现charAt(0)或需要更改/添加什么 import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class FirstWordLetters { public static void main(String[] args) throws File
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.
import java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
公共类首字母
{
公共静态void main(字符串[]args)引发FileNotFoundException
{
char[]大写={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’};
//将扫描仪输入系统
扫描仪扫描;
//2.使用scanner类查找文件-搜索到计算机数据位置
试一试{
扫描=新扫描仪(新文件(“F:/programming principles/Programmings-PART B/encyphered.txt”);
}catch(异常e){//抛出异常e(意味着防止任何运行时错误)
System.out.println(“未找到文件”);
返回;
}
//3.设置整数(用于计数和完整计数。)
//aplhabet有26个字符,因此新的int将是数组的mem.space。
int[]计数=新的int[26];
int completeTotal=0;
//4.开始扫描系统
//扫描每一行,并通知用户每一行都已正确读取。
//读取每个时间行后,将值存储在数组中,并按1递增
while(scan.hasNextLine()){
String line=scan.nextLine();
System.out.println(“行读取:”+行);
char[]digit=line.toCharArray();
对于(int i=0;i0)
System.out.println(“+((浮点)计数[i]/completeTotal)*100)+“%”;
其他的
System.out.println(“0%”);
}
}
}//源代码结束。
只是过度使用子字符串()
或者您可以使用charAt()使用正则表达式怎么样?这是一个样品
String line = scan.nextLine();
System.out.println("Line read: " + line);
for(String s : line.split("\\s+")) {
System.out.println(s.charAt(0));
break;
}
这段代码就可以了
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]);
}
如果你有什么不明白的地方,我会尽力解释。你能给我一个在代码中使用子字符串的例子吗?是的,CharAt(0)是我想的,但我不确定在代码中放在哪里让它工作……非常简单。您获取一个字符串(我假设您正在使用该字符串),然后调用substring。例如字符串s=“Lol”;s、 子串(0,1);你能发布你的输入数据和你想从中得到的输出吗?我不完全清楚你想在这里做什么。你把它全部删除了。。。尽管我不确定,但我还是要看一看,不用nextLine,可以使用next()并继续使用charAtI,因为我对regex了解很少。当然,在代码中有一个位置,我可以在逐行和数组读取时使用字符(0)或其他东西?我上面的代码取自问题中的代码片段。我刚刚添加了正则表达式。由于您需要一个单词的第一个字母,因此必须有一个代码通过标记器或正则表达式将两个单词分开。至于数组的读取,我个人认为我们可以使用map或hashtable来计算计数和百分比。
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