0

I'm working on an assignment that requires a file to be read, and the frequency of letters to be returned. I currently have my return statement as my display method so that the letters in the file are shown in a table, and a counter for those letters is the next column over. Here's my problem: I have display method nested inside a for-loop that is nested in a while-loop, and currently returning nothing after the file has been read. The while loop is constructed so that it runs only as long as the file is being read (the end of the file being -1). The for loop is supposed to check to see if the index values of my inputValue is identical to the index values of the alphabet (and if so, display). As I've already mentioned, it's currently not displaying anything; and despite researching (and asking classmates) I've not been able to figure out what's wrong. Any insight that can be given would be greatly appreciated.

 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

}
alapham
  • 13
  • 1
  • 6
  • Did you try to [debug](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1) it? You should be able to break it down to pieces (open&read file, pick letters, return output) and see if they work one after the other. – Curiosa Globunznik Dec 11 '19 at 23:11
  • @gurioso Yes. The open&read file works, the pick letters does not. The return output works if I call it on its own, but the for-loop is where it breaks. I can't figure out why it's not working there. The purpose of the for-loop is to check if the integer value of ch is equal to the index values found in alphabet. – alapham Dec 11 '19 at 23:18
  • does it enter the loop ever? does `if(char2int(ch) == alphabet[i])` return true at least once? is `display(alphabet, counters);` called at least once? – Curiosa Globunznik Dec 11 '19 at 23:21
  • char2int returns 0 for 'A', 1 for 'B', etc. And you're comparing the result with 'A', 'B', etc. So all the comparisons fail. Use your debugger. Add println() statements in the code. And google for "Java IO tutorial" to know how to properly read characters from a file. – JB Nizet Dec 11 '19 at 23:23
  • does `char2int` work as expected? do you have a [unit test](https://stackoverflow.com/questions/8751553/how-to-write-a-unit-test) for it? – Curiosa Globunznik Dec 11 '19 at 23:23
  • @gurioso The char2int does work (it was supplied by the professor in the skeleton code). Checking again the if statement isn't returning true, so I'll have to figure out some adjustment there. – alapham Dec 11 '19 at 23:34
  • check out JBNizet's comment, there's an issue with the comparison – Curiosa Globunznik Dec 11 '19 at 23:35

0 Answers0