Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
New Contributor
Posts: 5
Registered: ‎06-19-2012
My Device: developer
My Carrier: none
Accepted Solution

Converting a String into an array of ints

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.

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Converting a String into an array of ints

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/net/rim/device/api/util/StringMatch.html


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/net/rim/device/api/io/LineReader.html

 

Hope that helps,

 

E.

 

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Converting a String into an array of ints

[ Edited ]

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

 
New Contributor
Posts: 5
Registered: ‎06-19-2012
My Device: developer
My Carrier: none

Re: Converting a String into an array of ints

@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.

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Converting a String into an array of ints

ignore this 

Utility.showExcpetion(e.toString());
And call directly
Split(_str, delimiter);
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Converting a String into an array of ints

"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());

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Converting a String into an array of ints

Hi Peter

Very good explanation....

Pawan
Thanks
New Contributor
Posts: 5
Registered: ‎06-19-2012
My Device: developer
My Carrier: none

Re: Converting a String into an array of ints

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. 

New Contributor
Posts: 5
Registered: ‎06-19-2012
My Device: developer
My Carrier: none

Re: Converting a String into an array of ints

EDIT: I tried testing by just printing out the length of the vector but it turns out when i run the method StringToInteger i get App Error 104
NullPointerException.
Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Converting a String into an array of ints

http://www.blackberry.com/developers/docs/7.1.0api/java/util/Vector.html#copyInto(java.lang.Object[]... is the correct method. initialize the array with the size of the vector.
Also make sure the code actually returns Integer objects, not just strings with the number in it.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter