开发者社区> 问答> 正文

如何读取文件,并从该文件返回字母

我正在做一个需要读取文件并返回字母频率的作业。目前,我以return语句作为显示方法,以便文件中的字母显示在表格中,而这些字母的计数器在下一行。这是我的问题:我将显示方法嵌套在for循环中,该循环嵌套在while循环中,并且在读取文件后当前不返回任何内容。while循环的构造使其仅在读取文件时才运行(文件的末尾为-1)。for循环应该检查一下我的inputValue的索引值是否与字母的索引值相同(如果是,则显示)。正如我已经提到的,它目前不显示任何内容。尽管正在研究(并问同学),我还是 一直无法找出问题所在。可以给出的任何见解将不胜感激。

final static int AlphabetSize = 26;
    final static Scanner cin = new Scanner(System.in);
    final static int MaxBarLength = 50;

    public static void main(String[] args) {
        String fileName;

        // sign-on
        out.print("CPS 150 Assignment 6 by Anthony Lapham  \n\n");

        // get the file name
        out.print("Enter the file name: ");
        fileName = cin.nextLine();

        // process the file
        try {
            processFile(fileName);
        } catch (FileNotFoundException e) {
            out.println(fileName + " was not found, program terminated");
        } catch (IOException e) {
            out.println("Unforeseen IO exception, stack trace follows");
            e.printStackTrace();
        } // end try

        // sign-off
        out.print("\nAssignment 6 complete\n\n");
    } // end main

    static void processFile(final String fileName)
            throws FileNotFoundException, IOException {
        FileInputStream inFile = new FileInputStream(fileName);
        int inputValue = 0; // character read as an integer
        char ch;            // variable to hold input value typecast to char
        int[] counters = new int[AlphabetSize];
        final char[] alphabet = {'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'};

        // declare other variables you need
        int i;

        // implement a standard sentinel control loop here
        int SENTINEL = -1;
        // read first input character as an integer
        inputValue = inFile.read();

        while (inputValue != SENTINEL) {
            ch = (char) inputValue;
            for(i = 0; i < counters.length; i++){
                if(char2int(ch) == alphabet[i]){
               display(alphabet, counters);
           }
            }




            // add code to process this character
            // read next input character
            inputValue = inFile.read();
        } // end loop

        inFile.close();

        // generate appropriate output
    } // end function

    static void display(final char[] alphabet, final int[] counters) {
        // write code for this function
        int counter = 0;
        int charCounter;
        int num;
        out.print("The file has " + alphabet + " alphabetic, and " + counters + " other characters.\n\n");
        out.println("Letter   Count    Bar");
        out.println("------   -----    ---");
//        while (counter < AlphabetSize) {
////            out.printf("%5s %-4s %-3s", alphabet, counters, printChars(counters, alphabet));
//            counter++;
//        }

    } // end function

    // char2int is complete
    static int char2int(final char arg) {
        if (!Character.isLetter(arg)) {
            return -1;
        } else {
            return (int) Character.toUpperCase(arg) - (int) 'A';
        }
    } // end function

    // function printChars writes n copies of the character c to the
    // standard output device
    static void printChars(final int n, final char c) {
        // write the code
        for (int i = 0; i < n; i++) {
            if (i == char2int(c)) {
                out.print("*");
            }
        }

    } // end printChars

    // this function returns the largest value stored in the array
    static int maxCount(final int[] arr) {
        // write the code
        return 0;
    } // end function

}

展开
收起
垚tutu 2019-12-12 09:35:44 708 0
1 条回答
写回答
取消 提交回答
  • 精于基础,广于工具,熟于业务。

    while (inputValue != SENTINEL) { ch = (char) inputValue; for(i = 0; i < counters.length; i++){ if(char2int(ch) == alphabet[i]){ display(alphabet, counters); }else // add code to process this character // read next input character inputValue = inFile.read(); } } // end loop

    改成这样试试

    2019-12-12 17:07:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载