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(); } } } 
2

4 Answers 4

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"); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your detailed explanation. I tried an upvote for you but I think something prevented it. Regards
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 } } 

1 Comment

Code-only answers are discouraged. You should explain what the problem is, as well as how to solve it.
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(); } } 

Comments

0

For me the issue was that I was using a deprecated package instead of it's up to date newer counterpart. In my case it was @react-native-community/cameraroll (deprecated) instead of @react-native-camera-roll/camera-roll.

Obviously this question is specific to that but consider updating your package which is being affected.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.