By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,805 Members | 1,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,805 IT Pros & Developers. It's quick & easy.

How to parse a string to an array of integers?

P: 6
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!

Expand|Select|Wrap|Line Numbers
  1.  
  2. BufferedReader inFromClient = new BufferedReader
  3. (newInputStreamReader(connectionSocket.getInputStream());
  4. FromClient = inFromClient.readLine();
  5.  
  6. public double CalculateAnswer()
  7. {
  8. double m = Double.parseDouble( FromClient );
  9. double numbers[] =  new  double[m]; 
  10. }
  11.  
  12.  
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
Jul 24 '12 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,422
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.
Jul 24 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.