06-19-2012 03:03 PM
I need ot make an app that will read in numbers, or data points, from a text file on an SD card and then output these in a line graph. Currently I am able to read in the contents of a text file and store it as a string with this code
private String readTextFile(String fName) {
String result = null;
FileConnection fconn = null;
DataInputStream is = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ);
is = fconn.openDataInputStream();
byte[] data = IOUtilities.streamToBytes(is);
result = new String(data);
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (null != is)
is.close();
if (null != fconn)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
return result;
}I am able to print out the string result from my test file so it prints out something like "1 2 3 12 14 2 42"
What i want to do it convert this string into an int array that will look like int[] reult = {1, 2, 3, 12, 14, 2, 42}.
I have tried using string.split(" ") as that seems to be generally what is done when i google the problem. When i try this however i get an error "The method split(String) is undefined for the type String"
I just want to know if there is a way to convert this string into an array of ints, or maybe is there a way to alter my readTextFile method so that it will output the data as an array or work with the byte array to get seperate integers.
Solved! Go to Solution.
06-19-2012 04:27 PM
Hi @curtisq
There are couple of options that come to mind:
1. Use the StringMatch API to search for pattern of numbers:
http://www.blackberry.com/developers/docs/5.0.0api
2. I don't know the structure of your text file but if you can change it to one number per row, you can read the file with LineReader API and then parse the numbers with Integer.parseInt(...).
http://www.blackberry.com/developers/docs/5.0.0api
Hope that helps,
E.
06-19-2012 04:52 PM - edited 06-19-2012 06:39 PM
Hi Try this
public static Vector StringToInteger(String _str) {
// TODO Auto-generated method stub
Vector _vector = new Vector();
String[] temp;
String delimiter = " ";
temp = Split(_str, delimiter);
for(int i=0;i<temp.length;i++)
{
_vector.addElement(temp[i]);
}
return _vector;
}
Split Function::
public static String[] Split(String splitStr, String delimiter)
{
String[] splitArray = null;
try
{
StringBuffer token = new StringBuffer();
Vector tokens = new Vector();
// split
char[] chars = splitStr.toCharArray();
for (int i=0; i < chars.length; i++)
{
if (delimiter.indexOf(chars[i]) != -1)
{
// we bumbed into a delimiter
if (token.length() > 0)
{
tokens.addElement(token.toString());
token.setLength(0);
}
else
{
tokens.addElement("");
}
}
else
{
token.append(chars[i]);
}
}
// don't forget the "tail"...
if (token.length() > 0)
{
tokens.addElement(token.toString());
}
else
{
tokens.addElement("");
}
// convert the vector into an array
splitArray = new String[tokens.size()];
for (int i=0; i < splitArray.length; i++)
{
splitArray[i] = (String)tokens.elementAt(i);
}
}
catch(Exception e)
{
}
return splitArray;
}
Thanks
Pawan
06-19-2012 06:24 PM
@maadani
thank you for the links. The second one is useful, if I can get nothing else to work I will use this and make sure the ints are all seperated by new lines.
@pankajace12
When i try to use your code i get "Utility cannot be resolved" for
Utility.showExcpetion(e.toString());
temp = Utility.Split(_str, delimiter);
those two lines. I expected this might be some sort of problem with importing (I am not entirely familiar with java so other ways to solve the issue im unaware of). Eclipse gives me no import option for them and i tried importing "import net.rim.device.api.util.*;" but that had no effect. So im not sure how to move forward with that code.
06-19-2012 06:37 PM
ignore this
Utility.showExcpetion(e.toString());
And call directly
Split(_str, delimiter);
06-19-2012 06:39 PM
"Utility.Split" is just calling the split method - the second set of supplied code. In the sample, it appears to be a static method in the Utility class. You can put whatever you like really - if you keep the two bits of code in the one class then you can just call the method...
"Utility.showExcpetion(e.toString());"
has not been supplied, but is clearly the authors standard reporting/collection for exceptions. In your case at this time, you can just replace this with:
System.out.println(e.toString());
06-19-2012 07:00 PM
06-19-2012 07:47 PM
Thank you for clarifying that.the code compiles without error now. This of course leads me to my next obstable which is converting the Vector the method returns to an int[].
I have tried this
Vector v = StringToInteger(result);
Integer[] sl = (Integer[])v.toArray(new Integer[0]); All my googling of converting a Vector to an int array or Integer array tells me to you toArray() however eclipse tells me that toArray is undefined for type Vector (It then tells me to cast it to type object but that still doesnt work).
Is there a method of converting the Vector to an int array and/or a way to check the contents of the vecor.
I'm sorry if these seem like low level questions, I have no real experience in Java. Again, thank you very much for the information you have already provided.
06-19-2012 07:58 PM
06-20-2012 04:39 AM