Force Close

Put your problem here if it does not fit any of the other categories.

Force Close

Postby sync0s » Sun Dec 13, 2009 8:27 am

Hi. I'm trying to split up a filename using the Split() function and have the result be the actual file name without the path and the extension. It works fine getting rid of the path but I can't get it to cut off the extension.. when I try to log the result the app force closes. I narrowed it down to it not splitting the string as str1[0] returns the full string unsplit. I can't figure out why.. and I know there's probably an easier way.. here is the code:

Code: Select all
        g.setOnItemClickListener(new OnItemClickListener(){

         public void onItemClick(AdapterView parent, View v, int position, long arg3) {
            Toast.makeText(Friends.this, mFiles[position], Toast.LENGTH_SHORT).show();   
            String[] str = mFiles[position].split("/");
            Log.v("FriendsList", str[3]);
            TextView contactname = (TextView) findViewById(R.id.contact_name);
            String[] str1 = str[3].split(".");
             contactname.setText(str[3]);
             Log.v("FriendsList", str1[0]);
         }           
         });


Any help would be appreciated!
sync0s
Freshman
Freshman
 
Posts: 5
Joined: Thu Nov 19, 2009 8:13 am

Top

Postby hardcoras » Sun Dec 13, 2009 1:03 pm

Try not to write a non-defensive style code. Your code can potentially throw a ArrayIndexOutOfBoundsException.

To strip a path use javo.io.File(http://developer.android.com/reference/ ... /File.html).
To remove extension use something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. int index = name.lastIndexOf('.');
  3.  
  4. String noExt = name;
  5.  
  6. if (index > 0) {
  7.  
  8.   noExt = name.substring(0, index);
  9.  
  10. }
  11.  
  12.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby Droid123 » Mon Dec 14, 2009 1:13 am

thats very useful to know, thanks!
Droid123
Experienced Developer
Experienced Developer
 
Posts: 77
Joined: Sat Nov 21, 2009 1:21 pm

Postby sync0s » Mon Dec 14, 2009 5:33 am

That works. Thank you very much!
sync0s
Freshman
Freshman
 
Posts: 5
Joined: Thu Nov 19, 2009 8:13 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 13 guests