首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用循环计算文本文件中的某些字符

如何使用循环计算文本文件中的某些字符
EN

Stack Overflow用户
提问于 2016-02-23 07:44:50
回答 2查看 84关注 0票数 1

我需要一个项目的帮助,该项目统计了尤利西斯文本中的所有元音,由詹姆斯乔伊斯。我不知道如何让程序读取我插入到项目根文件夹中的文本文件。我也不确定如何做一个循环来计算所有的元音。这就是我到目前为止所拥有的。

代码语言:javascript
复制
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();
        }


    }}

输出应如下所示(所有数字右对齐):

代码语言:javascript
复制
Vowel counts in Ulysses by James Joyce:  
a = 94126   
e = 143276  
i = 82512  
o = 92743  
u = 33786
EN

回答 2

Stack Overflow用户

发布于 2016-02-23 07:59:23

我不得不这样做一次,我想它是类似于这样的东西。

代码语言:javascript
复制
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++;

        }


    }
票数 0
EN

Stack Overflow用户

发布于 2016-02-23 09:29:53

导入java.util.Scanner;

公共类Smashcode {

代码语言:javascript
复制
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);

    }}

希望你喜欢我的work.Happy代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35566152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档