1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by Emre Can AYDIN, Dec 29, 2018.

  1. Emre Can AYDIN

    Thread Starter

    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 (Text):
    1.  
    2. Ileri,1,1
    3. Sol,1,1
    4. Sag,1,1
    5. Geri,1,1
    6.  
    here is my code:
    Code (Text):
    1.  
    2. start_btn.setOnClickListener(new View.OnClickListener() {
    3.             @Override
    4.             public void onClick(View v) {
    5.                 ///data/data/com.example.emrecan.myapplication/files/komutlar.txt
    6.                 String yon;
    7.                 String saniye1;
    8.                 String tekrar;
    9.                 int i=0,j=0;
    10.                 ArrayList<String> listS=new ArrayList<String>();
    11.  
    12.                try {
    13.                     Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));
    14.  
    15.                         while(s.hasNextLine())
    16.                         {
    17.                             listS.add(s.nextLine());
    18.                         }
    19.  
    20.                 } catch (FileNotFoundException e) {
    21.                     e.printStackTrace();
    22.                 }
    23.  
    24.                     System.out.println(listS);
    25.          //The program is working properly here.Problems start after this line.
    26.  
    27.  
    28.                     String array=listS.toString();
    29.                     String [] row=array.split(",");
    30.                     while(i<(row.length-2))
    31.                     {
    32.                         yon=row[i];
    33.                         saniye1=row[i+1];
    34.                         tekrar=row[i+2];
    35.                         int diziboyutu=row.length;
    36.                         int tekrar1;
    37.                         long sure1;
    38.                         sure1=Long.parseLong(saniye1);
    39.                         tekrar1=Integer.parseInt(tekrar);
    40.                         sure1=sure1*1000;
    41.                         System.out.println(yon);
    42.                         System.out.println(saniye1);
    43.                         System.out.println(tekrar);
    44.                         System.out.println(sure1);
    45.                         System.out.println(tekrar1);
    46.                         System.out.println(diziboyutu);
    47.                         i=i+3;
    48.                     }
    49.  
    50.             }
    51.         });
    52.  
    here is my output:
    Code (Text):
    1.  
    2. I/System.out: [Ileri,1,1, Sol,1,1, Sag,1,1, Geri,1,1]
    3.     [Ileri
    4. I/System.out: 1
    5.     1
    6.     1000
    7.     1
    8.     12
    9.      Sol
    10.     1
    11.     1
    12.     1000
    13.     1
    14.     12
    15.      Sag
    16.     1
    17.     1
    18.     1000
    19.     1
    20.     12
    21. //the program does not stop until here.
    22. D/AndroidRuntime: Shutting down VM
    23. E/AndroidRuntime: FATAL EXCEPTION: main
    24.     Process: com.example.emrecan.myapplication, PID: 24123
    25.     java.lang.NumberFormatException: For input string: "1]" //i think this the problem but i can't find solution.
    26.         at java.lang.Integer.parseInt(Integer.java:521)
    27.         at java.lang.Integer.parseInt(Integer.java:556)
    28.         at com.example.emrecan.myapplication.MainActivity$7.onClick(MainActivity.java:322)
    29.         at android.view.View.performClick(View.java:5610)
    30.         at android.view.View$PerformClick.run(View.java:22265)
    31.         at android.os.Handler.handleCallback(Handler.java:751)
    32.         at android.os.Handler.dispatchMessage(Handler.java:95)
    33.         at android.os.Looper.loop(Looper.java:154)
    34.         at android.app.ActivityThread.main(ActivityThread.java:6077)
    35.         at java.lang.reflect.Method.invoke(Native Method)
    36.         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    37.         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
    38. Application terminated.
    39.  
     


  2. Deleted User

    Deleted User Guest

    The error tells you exactly what the problem is

    Code (Text):
    1.  
    2. java.lang.NumberFormatException: For input string: "1]"
    3.  
    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.
     
    Emre Can AYDIN likes this.
  3. Emre Can AYDIN

    Thread Starter

    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?
     
  4. Deleted User

    Deleted User Guest

    This line is causing the problem, and you don't need to do it.

    Code (Text):
    1.  
    2. String array=listS.toString();
    3.  
    The String.split() method returns an array. I would iterate over the listS array, and process each line, something like this:

    Code (Text):
    1.  
    2. for (String str in listS) {
    3.   String[] line = str.split(",");
    4.   // Process the line
    5.  ...
    6. }
    7.  
     
    Emre Can AYDIN likes this.
  5. Emre Can AYDIN

    Thread Starter

    Thank you so much.
     
Loading...
Similar Threads - items txt file
  1. Samsung
    Replies:
    0
    Views:
    96
  2. LibbyChai
    Replies:
    1
    Views:
    398
  3. RioLeal
    Replies:
    0
    Views:
    337
  4. Dan Tudor Morar
    Replies:
    0
    Views:
    485
  5. ElChief
    Replies:
    1
    Views:
    236
  6. egsimo
    Replies:
    0
    Views:
    250
  7. brynkichy98
    Replies:
    3
    Views:
    457
  8. kk63852
    Replies:
    0
    Views:
    440
  9. Mingi
    Replies:
    0
    Views:
    1,834
  10. Monny513
    Replies:
    3
    Views:
    3,683

Share This Page

Loading...