How to parse a string to an array of integers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lzMnelson
    New Member
    • Jun 2012
    • 6

    How to parse a string to an array of integers?

    Hi I have a basic client server program that I created. I'm confused on how the whole parseDouble() works when adding numbers.

    The data is in the form of whole numbers separated by spaces. Such as 1 2 3 4. I was thinking to store each number in an array because I want to add them together and return the result to my client. I'm always open to easier ways!

    Code:
    		
    BufferedReader inFromClient = new BufferedReader
    (newInputStreamReader(connectionSocket.getInputStream());
    FromClient = inFromClient.readLine();
    
    public double CalculateAnswer()
    {
    double m = Double.parseDouble( FromClient );
    double numbers[] =  new  double[m]; 
    }
    Im confused how to
    1) isolate each number in the string and converting it to a data type such as double
    2) adding those values together
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What you'll want to do is first split() the strings by your delimiter, in this case the space. Then loop through the array returned and add them up.

    Comment

    Working...