0

So I'm trying to understand the flow of data of a text File inside an Array in Java.

I'm relatively new to programming, I've looked over the java docs and they're somewhat hard to familiarize myself with. Ultimately I want to find the percentage of the distribution of characters inside this txt file.

I have a method that takes in a file and returns an Array. I'm thinking I have to somehow convert the File into a string Where i can then look at the file char by char counting each instance of. From there I can calculate the distribution percentage depending on how often they each appear. But how to return that as an Array?

What I'm not sure of is whether I should use a 'File' Array, or convert the textfile into A String (toString?) put it in an Array and then try to find a way to count each instance of each char. (A-Z).

In any case, understanding how An array looks at its contents might point me in the right direction in how to tackle the problem. Any help would be greatly appreciated.

Lighterletter
  • 209
  • 2
  • 13
  • 2
    what do you need exactly? want to store files' content in LIST? or what do you need exactly? – Danyal Sandeelo Apr 03 '15 at 20:52
  • Are you taking a programming course or trying to self-study? If a course, then you should check your class notes about reading files. If self-study, try the [Java I/O tutorials](https://docs.oracle.com/javase/tutorial/essential/io/). – RealSkeptic Apr 03 '15 at 20:55
  • Sorry, I meant to be clearer. I need to read a txt file and sort its distribution of characters by percentage, ie. the programs output will ultimately be something along the lines of a: .02%, b: .04% .. and so forth. – Lighterletter Apr 03 '15 at 20:56
  • You probably need to read the whole file (search for some pointer here if needed) and use a map (key = letter, value = count) for counting (there are also some questions that might help here). see http://stackoverflow.com/questions/27195070/how-to-count-occurrence-of-each-character-in-the-alphabet-with-a-file –  Apr 03 '15 at 21:11

2 Answers2

1

To read the file and convert that to a string

    final String path = "c:\\somefile.txt";
    final byte[] bytes = Files.readAllBytes(Paths.get(path));
    final String fileContents = new String(bytes, Charset.defaultCharset());

you can then work on the fileContents string to count the frequency of each character to achieve your character frequency.

You can refer this for the frequency part. Java: How do I count the number of occurrences of a char in a String?

Community
  • 1
  • 1
SDS
  • 828
  • 3
  • 17
  • 35
0

Read the file into the double dimension char array then apply logic on the double dimension array to find the distribution.

char [] [] buffer = new char [10][10];

Scanner fileScanner = new Scanner(new File("Path to file"));

int lineCount=0;
while (fileScanner.hasNextLine()){
        String line = fileScanner.nextLine();
        System.arraycopy(line.toCharArray(),0,buffer[lineCount++],0,line.length());
}

I have just used length 10 for array you can decide size base on the input file.

Himanshu Ahire
  • 677
  • 5
  • 18
  • Why a two-dimensional array? What purpose does it serve? – RealSkeptic Apr 03 '15 at 21:23
  • Yes there are multiple solutions based on the what type of calculation needs to done. If we just need the count then map will work. But if he needs to find out distance between the position of characters on multiple lines then array should work. – Himanshu Ahire Apr 03 '15 at 21:36