2

This code is from the below link. I tried to run it but got the following error:

Thelink

D:\Android_Dosyalar\Proje\TextToArray\app\src\main\java\chessactivetexttoarray\com\texttoarray\MainActivity.java Error:(40, 21) error: unreported exception IOException; must be caught or declared to be thrown Error:(41, 22) error: unreported exception IOException; must be caught or declared to be thrown Error:(42, 21) error: unreported exception IOException; must be caught or declared to be thrown Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

What possibly is wrong. The following part is underlined in red as well. New to android. Novice level. Thank you all in advance. Regards

            br.close();
            isr.close();
            is.close();

Read and split text file into an array - Android

import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AssetManager manager;
        String line = null;
        List<String[]> xyzList = new ArrayList<String[]>();
        String[][] xyz;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            manager = getAssets();
            is = manager.open("C:\\Users\\serhat\\Copy\\satranc\\Akdag_Reportaj\\dan_akdag.pgn");
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
               while ((line = br.readLine()) != null) {
                xyzList.add(line.split(" "));
            }
            xyz = (String[][]) xyzList.toArray();
        } catch (IOException e1) {
             Toast.makeText(getBaseContext(), "Problem!",     Toast.LENGTH_SHORT).show();
        } finally {

            br.close();
            isr.close();
            is.close();
        }




    }


}
Community
  • 1
  • 1
Aziz S. Kural
  • 193
  • 1
  • 3
  • 12
  • The [`close()`-Methods](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#close--) can throw exceptions. Therefore, you are forced to either catch them or throw them. See the [Oracle Tutorial about Exceptions](https://docs.oracle.com/javase/tutorial/essential/exceptions/) for more details. – Turing85 Nov 22 '15 at 15:39
  • Possible duplicate of [How do I close a file after catching an IOException in java?](http://stackoverflow.com/questions/2649322/how-do-i-close-a-file-after-catching-an-ioexception-in-java) – Turing85 Nov 22 '15 at 15:42

3 Answers3

2

The close methods can throw an exception. So they need to catch them.

} finally {
    try {
        br.close();
        isr.close();
        is.close();
    } catch(IOException e) {
        e.printStackTrace();
    }
}

You also have one other issue in your code that will cause it to fail at run time. You are trying to use the AssetManager to open a file on your windows computer and not the phone. Move the file into your your projects' assets folder and then change

is = manager.open("C:\\Users\\serhat\\Copy\\satranc\\Akdag_Reportaj\\dan_akdag.pgn");

to

is = manager.open("dan_akdag.pgn");
phxhawke
  • 2,581
  • 23
  • 17
1

You need to catch the IOException that the close methods may throw:

e.g.

finally {
    try {
        br.close();
        isr.close();
        is.close();
    } catch (IOException e) {
        // dosomething
    }
}
Shloim
  • 5,281
  • 21
  • 36
  • 2
    Code-only answers are discouraged. You should explain what the problem is, as well as how to solve it. – Turing85 Nov 22 '15 at 15:40
0

The close method throws an IOException. You don't have a try/catch block surrounding the close methods. Your code should look like:

...
} finally {
    try {
        br.close();
        isr.close();
        is.close();
    } catch(IOException ex) {
        ex.printStackTrace();
    }
}
Lucas Baizer
  • 305
  • 2
  • 5
  • 13