Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse single line values from a text file in Android

Tags:

java

android

Is it possible to parse a text file with a single line divided in values in Android? An example of file i would like to parse could be:

100,102,106,109,110

And the solution I'm looking for would be something like

Log.d(TAG, "value 1" + theFirstValue);    
Log.d(TAG, "value 2" + theSecondValue);

//etc...

Any suggestions? Thanks!

like image 214
iGio90 Avatar asked Nov 27 '25 12:11

iGio90


2 Answers

You could use the String.split() method.

File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "path/to/the/file");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {       
    String[] split = line.split(","); 
    for(int i = 0; i < split.length; i++) {
      Log.d(TAG, "value " + i + ": " + split[i]);
    }
}
br.close();
like image 136
mehmetseckin Avatar answered Nov 29 '25 00:11

mehmetseckin


String s="100,102,106,109,110";
String[] values=s.split(",");

for(int i=0;i<values.length;i++){
     int v=Integer.parseInt(values[i]);
     Log.d(TAG, "value "+i +": "+ v);   
}
like image 24
vipul mittal Avatar answered Nov 29 '25 00:11

vipul mittal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!