-1

I want to read and print the numbers from the Textfile (WLTP.txt) in the assets folder as an array, but the output of my code is everytime null. I wonder why it is skipping the try part and jumping to the catch part.

public class WLTPc {

    public static void main(String[] args) {
        int [] data = readFiles("WLTP.txt");
        System.out.println(Arrays.toString(data));
    }

    public static int [] readFiles(String file) {
        try {

            File f = new File(file);
            Scanner s = new Scanner (f);
            int ctr = 0;
            while (s.hasNextInt()) {
                ctr ++;
                s.nextInt();
            }
            int[] arr = new int[ctr];

            Scanner s1= new Scanner(f);

            for (int i=0; i< arr.length; i++){
                arr[i] = s1.nextInt();
            }

            System.out.println(Arrays.toString(arr));
            return arr;
        }
        catch (Exception e){
            return null;
        }
    }
}
SonhnLab
  • 321
  • 1
  • 11

1 Answers1

0

You are passing file name directly and accessing it in File() object.

As you are accessing file from asset you need to use getAssets().open(file))

It will return InputStream. Pass that InputStream to Scanner() and read you file as you want.

import android.content.Context;

import java.io.IOException;
import java.util.Scanner;

public class WLTPc {

    private Context context;

    public WLTPc(Context context) {
        this.context = context;
    }

    public int[] readAssetFiles(String file) {
        try {
            Scanner s = new Scanner(context.getAssets().open(file));
            int ctr = 0;
            while (s.hasNextInt()) {
                ctr++;
                s.nextInt();
            }
            s.close();
            int[] arr = new int[ctr];

            Scanner s1 = new Scanner(context.getAssets().open(file));

            for (int i = 0; i < arr.length; i++) {
                arr[i] = s1.nextInt();
            }
            s1.close();
            return arr;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
adityakamble49
  • 1,971
  • 18
  • 27
  • Unfortunately not. There are 2 Errors. The bufferedReader in the last if cannot be resolved and there is missing a return in the last catch. – Uğur Güneş Jul 20 '18 at 09:24
  • @UğurGüneş Updated code with errors fixed. Checkout and let me know – adityakamble49 Jul 20 '18 at 09:26
  • @adityakamble49 Wouldn't it be more efficient to use a collection, e.g. `ArrayList`, and then returning `ArrayList.toArray` instead of reading through the whole file twice? – pawrick Jul 20 '18 at 09:33
  • @UğurGüneş Yeah. It would. As Its upto you how much you want to optimize it as per your requirement. I was just focusing on getting file from assets and putting into `Scanner` so you can perform your operation. – adityakamble49 Jul 20 '18 at 09:34
  • @UğurGüneş consider upvoting and approving answer if this works for you :) – adityakamble49 Jul 20 '18 at 09:47