• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Get items from txt file to array list and split the array list into strings.

Hello everyone i try to get items from txt file using array list and i want to split this array list to strings and show them in system.I have succeeded get items from txt file to an array list,but when i try to split the array list and try to show them in a loop my application gets error.
here is my txt file:
Code:
Ileri,1,1
Sol,1,1
Sag,1,1
Geri,1,1

here is my code:
Code:
start_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ///data/data/com.example.emrecan.myapplication/files/komutlar.txt
                String yon;
                String saniye1;
                String tekrar;
                int i=0,j=0;
                ArrayList<String> listS=new ArrayList<String>();

               try {
                    Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

                        while(s.hasNextLine())
                        {
                            listS.add(s.nextLine());
                        }

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                    System.out.println(listS);
         //The program is working properly here.Problems start after this line.


                    String array=listS.toString();
                    String [] row=array.split(",");
                    while(i<(row.length-2))
                    {
                        yon=row[i];
                        saniye1=row[i+1];
                        tekrar=row[i+2];
                        int diziboyutu=row.length;
                        int tekrar1;
                        long sure1;
                        sure1=Long.parseLong(saniye1);
                        tekrar1=Integer.parseInt(tekrar);
                        sure1=sure1*1000;
                        System.out.println(yon);
                        System.out.println(saniye1);
                        System.out.println(tekrar);
                        System.out.println(sure1);
                        System.out.println(tekrar1);
                        System.out.println(diziboyutu);
                        i=i+3;
                    }

            }
        });

here is my output:
Code:
I/System.out: [Ileri,1,1, Sol,1,1, Sag,1,1, Geri,1,1]
    [Ileri
I/System.out: 1
    1
    1000
    1
    12
     Sol
    1
    1
    1000
    1
    12
     Sag
    1
    1
    1000
    1
    12
//the program does not stop until here.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.emrecan.myapplication, PID: 24123
    java.lang.NumberFormatException: For input string: "1]" //i think this the problem but i can't find solution.
        at java.lang.Integer.parseInt(Integer.java:521)
        at java.lang.Integer.parseInt(Integer.java:556)
        at com.example.emrecan.myapplication.MainActivity$7.onClick(MainActivity.java:322)
        at android.view.View.performClick(View.java:5610)
        at android.view.View$PerformClick.run(View.java:22265)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Application terminated.
 
The error tells you exactly what the problem is

Code:
java.lang.NumberFormatException: For input string: "1]"

Your parsing of the input data has included the trailing ']' character as part of the final element. So method parseInt can't make any sense of the value "1]". It's not a valid number.
 
The error tells you exactly what the problem is

Code:
java.lang.NumberFormatException: For input string: "1]"

Your parsing of the input data has included the trailing ']' character as part of the final element. So method parseInt can't make any sense of the value "1]". It's not a valid number.
Thank you for your answer but i can't find any solution.When i add items to array list it's looking like work fine but there is a problem in my array list.My first item of array list has '[' this char (my first item of array list:"[Ileri") and my last item of array list has ']' this char (my last item of array list:"Geri]"),but there is not these chars in my txt file why scanner add these chars,how can I prevent the scanner from add these chars to array list?
 
This line is causing the problem, and you don't need to do it.

Code:
String array=listS.toString();

The String.split() method returns an array. I would iterate over the listS array, and process each line, something like this:

Code:
for (String str in listS) {
  String[] line = str.split(",");
  // Process the line
 ...
}
 
Back
Top Bottom