-1

i am working on a project in which i have to read data from a file and store that data into a string array.

String array position "0" should have first 13 characters of the file, string array position "1" should have next 13 characters of the file. I tried to read only first 13 characters using getchar(), but i was having trouble using it, can anybody point out my error because when i try to run that on the app, the app crashes.

   InputStream is = getResources().openRawResource(R.raw.temp);
   BufferedReader br = new BufferedReader(new InputStreamReader(is));
            try {

                while((string = br.readLine()) != null){
                string.getChars(0,13, buffer, 0);
                    String str = new String(buffer);
                    text.setText(str);  

                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;

here, buffer is a char type array.

andrew
  • 9,313
  • 7
  • 30
  • 61
Asad
  • 49
  • 1
  • 5

2 Answers2

0

Use String.substring(startIndex, endIndex) to retrive a part of a string.

Gumbo
  • 1,716
  • 1
  • 15
  • 22
0

Why do ya use readLine when you are trying to split the string into chunks of 13 characters?

String str = "Some string that may or may not be longer than 13 characters";
int LENGTH = 13, off_multiplier = 0, read_size;    
char [] buffer = new char[LENGTH];

ArrayList<String> string_array = new ArrayList<String>();

InputStream is = new ByteArrayInputStream(str.getBytes());
try(BufferedReader reader = new BufferedReader(new InputStreamReader(is))){
    while( (read_size = reader.read(buffer, off_multiplier * LENGTH ,LENGTH)) != -1){
        string_array.add(new String(buffer,0,read_size));
    }
}
pikausp
  • 1,142
  • 11
  • 31