0
import java.awt.Graphics;
import java.awt.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundEsxception;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Benford {
    static Object            i    = null;
    static ArrayList<String> list = new ArrayList<String>();
    private static String    data;

    public static void BenfordPercents() {
        int one   = 0;
        int two   = 0;
        int three = 0;
        int four  = 0;
        int five  = 0;
        int six   = 0;
        int seven = 0;
        int eight = 0;
        int nine  = 0;

        return;
    }

    public static void main(String[] args) throws IOException {
        DrawingPanel g       = new DrawingPanel(500, 500);
        Graphics     brush   = g.getGraphics();
        String       popData = null;

        readCount(popData);
        BenfordPercents();
    }

    public static void readCount(String popdata) throws IOException {
        System.out.println("Please make sure the data file is name popData.txt");
        System.out.println("We are loading popData.txt...");

        Scanner console = new Scanner(System.in);

        // Scanner console = new Scanner((new File("popData.txt")));

        try {
            i = new FileInputStream("popData.txt");
        } catch (FileNotFoundException e) {
            System.out.println("We cannot locate popData.txt");
            System.out.println("Please make sure popData.txt is in the same location" + " as your code file!");

            return;
        }

        System.out.println("popData.txt has loaded!");
        System.out.println("");
        System.out.println("Please press enter to show data!");
        data = console.nextLine();

        File            file   = new File(popdata + ".txt");
        FileInputStream fis    = new FileInputStream("popdata.txt");
        byte[]          flush  = new byte[1024];
        int             length = 0;

        while ((length = fis.read(flush)) != -1) {
            list.add(new String(flush));

            // System.out.println(new String(flush));
            // System.out.println(list.toString());
        }

        // Scanner x = new Scanner((new File("popData.txt")));
        // while(x.hasNext()){
        // list.add(x.next());
        // }
        fis.close();
    }
}

I have a text document with all the numbers of populiation from this link here: https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population

The text document looks like this:

China   1369480000
India   1270250000
United  320865000
Indonesia   255461700
Brazil  204215000
Pakistan    189607000
..etc
..etc

the list is pretty long.

and when I try to store all of these numbers into an array list and I try to print the array list size it just returns 4?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Allen Tran
  • 49
  • 7
  • can you share the output of your list – Lalit Mehra May 11 '16 at 16:15
  • If the size is only four, then it only added for elements to the arraylist which means that your reader is failing before it gets to the end of the file. – Rabbit Guy May 11 '16 at 16:16
  • and your text file..may be there is a problem with encoding – Rohit Rehan May 11 '16 at 16:17
  • 1
    Possible duplicate of [How to read a large text file line by line using Java?](http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java) – cyroxis May 11 '16 at 16:18
  • 3
    You are not reading it line by line, but by chunks of 1024 bytes. You need to use a BufferedReader and ready by line. – cyroxis May 11 '16 at 16:19

1 Answers1

0

If you want a list-entry for each line, you should prefer a BufferedReader.

FileInputStream fis = new FileInputStream("popdata.txt")
BufferedReader br = new BufferedReader(new InputStreamReader(fis));

while((String line = br.readLine()) !=null) {
    list.add(line);
}

The size of 4 is only the size of your arraylist - each element of your array list was build from an byte-array with a length of 1024. This means that your list either contains about 3K until 4K of data.

Supahupe
  • 515
  • 2
  • 11