Java 如何使用循环计算文本文件中的某些字符
我需要一个项目的帮助,该项目由詹姆斯·乔伊斯计算《尤利西斯》文本中的所有元音。我不知道如何让程序读取我插入到项目根文件夹中的文本文件。我也不知道如何做一个循环来计算所有的元音。这就是我目前所拥有的Java 如何使用循环计算文本文件中的某些字符,java,Java,我需要一个项目的帮助,该项目由詹姆斯·乔伊斯计算《尤利西斯》文本中的所有元音。我不知道如何让程序读取我插入到项目根文件夹中的文本文件。我也不知道如何做一个循环来计算所有的元音。这就是我目前所拥有的 public static void main(String[] args) throws FileNotFoundException { System.out.println("Vowel counts in Ulysses by James Joyce:"); int a =
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Vowel counts in Ulysses by James Joyce:");
int a = 0;
int e = 0;
int i = 0;
int o = 0;
int u = 0;
System.out.println("a = " + a);
System.out.println("e = " + e);
System.out.println("i = " + i);
System.out.println("o = " + o);
System.out.println("u = " + u);
FileReader reader = new FileReader("ulysses.txt");
Scanner input = new Scanner(reader);
while (input.hasNextLine()) {
String line = input.nextLine();
}
}}
输出应如下所示(所有数字均正确对齐):
我不得不这样做过一次,我认为这是类似的事情
while (input.hasNextLine()) {
String line = input.nextLine();
//toUpperCase() normalizes all the letters so you don't have to count upper and lower
line.toUpperCase();
char[] c = line.toCharArray();
for (int j = 0; j < c.length(); j++)
switch(c[i])
{
case "A";
a++;
case "E";
e++;
case "I";
i++;
case "O";
o++;
case "U";
u++;
}
}
while(input.hasNextLine()){
String line=input.nextLine();
//toUpperCase()规范化所有字母,因此您不必计算上限和下限
line.toUpperCase();
char[]c=line.toCharArray();
对于(int j=0;j
我不得不这样做过一次,我认为这与此类似
while (input.hasNextLine()) {
String line = input.nextLine();
//toUpperCase() normalizes all the letters so you don't have to count upper and lower
line.toUpperCase();
char[] c = line.toCharArray();
for (int j = 0; j < c.length(); j++)
switch(c[i])
{
case "A";
a++;
case "E";
e++;
case "I";
i++;
case "O";
o++;
case "U";
u++;
}
}
while(input.hasNextLine()){
String line=input.nextLine();
//toUpperCase()规范化所有字母,因此您不必计算上限和下限
line.toUpperCase();
char[]c=line.toCharArray();
对于(int j=0;j
导入java.util.Scanner
公共类代码{
public static void main(String[] args) throws Exception {
// Create a File instance
java.io.File file = new java.io.File("samplefile.txt");
// Create a Scanner for the file
Scanner input = new Scanner(file);
// Create the Content String
String fileContent = "";
// Read data from a file
while (input.hasNext()) {
fileContent += input.next() + " ";
}
// Close the file
input.close();
//Split the string into a character array
char[] charArr = fileContent.toCharArray();
//loop through every character to find the vowels
int counter = 0;
for(char c : charArr)
{
if(c == 'a')
counter_a++;
if(c == 'e')
counter_b++;
if(c == 'i')
counter_i++;
if(c == 'o')
counter_o++;
if(c == 'u')
counter_u++;
}
//number of vowels
System.out.println("Number of Vowels: " + (counter_a+counter_e+counter_o+counter_i+counter_u));
System.out.println(counter_a);
System.out.println(counter_e);
System.out.println(counter_i);
System.out.println(counter_o);
System.out.println(counter_u);
}}
希望你喜欢我的工作。快乐编码导入java.util.Scanner 公共类代码{
public static void main(String[] args) throws Exception {
// Create a File instance
java.io.File file = new java.io.File("samplefile.txt");
// Create a Scanner for the file
Scanner input = new Scanner(file);
// Create the Content String
String fileContent = "";
// Read data from a file
while (input.hasNext()) {
fileContent += input.next() + " ";
}
// Close the file
input.close();
//Split the string into a character array
char[] charArr = fileContent.toCharArray();
//loop through every character to find the vowels
int counter = 0;
for(char c : charArr)
{
if(c == 'a')
counter_a++;
if(c == 'e')
counter_b++;
if(c == 'i')
counter_i++;
if(c == 'o')
counter_o++;
if(c == 'u')
counter_u++;
}
//number of vowels
System.out.println("Number of Vowels: " + (counter_a+counter_e+counter_o+counter_i+counter_u));
System.out.println(counter_a);
System.out.println(counter_e);
System.out.println(counter_i);
System.out.println(counter_o);
System.out.println(counter_u);
}}
希望你喜欢我的工作。快乐编码第一件事:不要使用
FileReader
。这是2015年,使用路径。get()
和文件。newbuffereder()
。这可能会回答你的问题:第一件事:不要使用FileReader
。这是2015年,使用路径。get()
和文件。newbuffereder()
。这可能会回答您的问题:使用此代码,所有值的输出均为0。检查文件的读数是否正确,或确保您已使用此代码保持路径文件正确。使用此代码,所有值的输出均为0。检查文件的读数是否正确,或确保您已保持路径文件正确