Andriod and String Arrays

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

Andriod and String Arrays

Postby g33k499 » Fri Dec 18, 2009 10:40 pm

I'm new to developing for Andriod and hit a little snag.

I'm reading data from a text file and using the split function to generate my ListView. That much is working.

What I need it to do is split each item in my array again and place each segment into its own array.

Here is an example of the string I am dealing with.

"234-456-200-Data, 234-456-200-Data , 234-456-200-Data"

My first array is generated by:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String[] mStrings = ReadSettings(this).split(",");
  3.  
  4.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Now I need another split but use "-" and place them into their own string arrays.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String[] strNumberSet1 = mStrings (x)
  3.  
  4. String[] strNumberSet2 = mStrings (x)
  5.  
  6. String[] strNumberSet3 = mStrings (x)
  7.  
  8. String[] strDataSet = mStrings (x)
  9.  
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


I know the last block of code isn't right at all. I'm just trying to show how I want the data handled.

Also, what is the best way to read the first character of a string and then strip it off?
g33k499
Freshman
Freshman
 
Posts: 9
Joined: Fri Dec 18, 2009 10:23 pm

Top

Re: Andriod and String Arrays

Postby SteffenL » Sat Dec 19, 2009 2:07 am

Hello,

Made an example for you... Or pretty much everything (whoops). If you just want to get rid of those whitespaces, I took care of it in my example with String.trim().
If that's not it, you can still read one or more characters with String.substr(). For removing one character, you can use substr() as well like they do in the example here (you can easily modify it to remove as many characters as needed).

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try
  2. {
  3.         String[] rawDataSplit = mStrings.split(",");
  4.         String[] rawValuesSplit;
  5.         for (String rawValues : rawDataSplit)
  6.         {
  7.                 // get rid of whitespaces
  8.                 rawValues = rawValues.trim();
  9.                 rawValuesSplit = rawValues.split("-");
  10.  
  11.                 // if you need to, you can loop through rawValuesSplit and strip the whitespaces away here instead
  12.  
  13.                 if (rawValuesSplit.length < 4)
  14.                 {
  15.                         // something wrong?
  16.                         throw new Exception("Not enough arguments.");
  17.                 }
  18.  
  19.                 String strNumberSet1 = rawValuesSplit[0];
  20.                 String strNumberSet2 = rawValuesSplit[1];
  21.                 String strNumberSet3 = rawValuesSplit[2];
  22.                 String strDataSet = rawValuesSplit[3];
  23.  
  24.                 Log.d("DEBUG", String.format(
  25.                         " "%s" -> { "%s", "%s", "%s", "%s" }",
  26.                         rawValues,
  27.                         strNumberSet1,
  28.                         strNumberSet2,
  29.                         strNumberSet3,
  30.                         strDataSet));
  31.         }
  32. }
  33. catch (Exception ex)
  34. {
  35.         ex.printStackTrace();
  36. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Result:
Code: Select all
"234-456-200-Data" -> { "234", "456", "200", "Data" }


I hope you can use it. :)
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Postby g33k499 » Mon Dec 21, 2009 6:30 pm

Thanks that helps a lot!
g33k499
Freshman
Freshman
 
Posts: 9
Joined: Fri Dec 18, 2009 10:23 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 22 guests