I want to make the scanner give a random line from a .txt file in assets folder when pressed to the button, but it gives me the error:

no suitable constructor found for File(InputStream)
constructor File.File(String) is not applicable
(argument mismatch; InputStream cannot be converted to String)
constructor File.File(URI) is not applicable
(argument mismatch; InputStream cannot be converted to URI) 

I have tried using Inputstream but it didn't work.

Edit: the file name to get the random line of text is "scramble.txt" and it is already in the assets folder.

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try{

        File file = new File (getAssets().open("scramble.txt"));

        final Scanner sc = new Scanner(file);

        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //change the scramble length
                textView.setText(sc.nextLine());
            }
        });`

1 Answers

0
CommonsWare On Best Solutions

Replace:

File file = new File (getAssets().open("scramble.txt"));
final Scanner sc = new Scanner(file);

with:

final Scanner sc = new Scanner(getAssets().open("scramble.txt"));