Search code examples
javadictionarycharacterfileinputstream

How to access the parameter of a parameter?


this is a work in progress, but I was wondering about how to access a specific file. My main method is supposed to construct a new FileInputStream with a new file as its parameter. Then, I am supposed to call getCounts method that accepts a FileInputStream object to return some kind of map. Inside the getCounts method, I am having trouble finishing this as I have to be able to access the file inside it, and FileInputStream doesn't seem to have a accessor for it. In other words, how do I access this file used to construct the FileInputStream object that goes into the getCounts Method as a parameter, inside the getCounts method? Ultimately, I am supposed to use the keys/values of the map to get the most reoccurring character of the text file. Thanks.

Here is my code:

import java.util.*;
import java.io.*;


public class test2 {
public static void main(String[] q) throws FileNotFoundException {

    // This is for the character mapping
    Scanner console = new Scanner(System.in);
    System.out.println("Count characters of which file? ");
    FileInputStream output = new FileInputStream(new File(console.nextLine()));
    Map<Character, Integer> results = getCounts(output); 
    // do stuff with this map later on...
}

 // character counting method (WIP)
 public static Map<Character, Integer> getCounts(FileInputStream input) {
     Map<Character, Integer> output = new TreeMap<Character, Integer>(); // treemap keeps keys in sorted order (chars alphabetized)
     // Problem here: need to access the file object that was instiantiated
     byte[] fileContent = new byte[(int) file.length()]; // puts all the bytes from file into byte[] to process
     input.read(fileContent); 
     String test = new String(fileContent);

     // missing stuff here; use map to map keys as characters and occurrences as values.

     return output;
 }
}

Solution

  • Ideally I would pass the the length as a parameter to getCounts() but since you're not allowed to do it, you can save the file-length into a class-static parameter:

    private static long length;
    
    public static void main(String[] q) throws IOException {
    
            // This is for the character mapping
            Scanner console = new Scanner(System.in);
            System.out.println("Count characters of which file? ");
            File file = new File(console.nextLine());
            FileInputStream output = new FileInputStream(file);
            length = file.length();
            Map<Character, Integer> results = getCounts(output);
            // do stuff with this map later on...
        }
    
        // character counting method (WIP)
        public static Map<Character, Integer> getCounts(FileInputStream input) throws IOException {
            Map<Character, Integer> output = new TreeMap<Character, Integer>(); // treemap keeps keys in sorted order (chars alphabetized)
            // Problem here: need to access the file object that was instantiated
            byte[] fileContent = new byte[(int) length]; // puts all the bytes from file into byte[] to process
            input.read(fileContent);
            String test = new String(fileContent);
            System.out.println("test = " + test);
    
            // missing stuff here; use map to map keys as characters and occurrences as values.
    
            return output;
        }