This discussion is archived
8 Replies Latest reply: Oct 25, 2007 5:51 AM by 807603

String Parsing

Currently Being Moderated
I have the following set of Strings

String1 - 1000.0, 949-331-2873, 13.27
String2 - 1000.0, 949-394-1805, 79.67
String3 - 1000.0, 949-394-4476, 98.30
String4 - 1010.0, 949-468-8982, 85.59
String5 - 1010.0, 949-394-7009, 22.52

1000.0 -> is Dept Number
949-331-2873 -> phone #
13.27 -> value

and all strings are in the same order...

I want to get sum of values of according to Dept wise, i.e. 1000.0 -> 13.27 + 79.67 + 98.30 = 191.24 and for 1010.0 -> 85.59 + 22.52 = 108.11.

• 1. Re: String Parsing
Currently Being Moderated
loop over the strings, then do a call
``String [] values = yourString.split(",")``
to get the three entries in the string. You may store the values or directly convert them into doubles if needed. Then build the sum of the values.
• 2. Re: String Parsing
Currently Being Moderated
here is some code doing the job:
``````
...
String [] data = {"1000.0, 949-331-2873, 13.27", "1000.0, 949-394-1805, 79.67", "1000.0, 949-394-4476, 98.30", "1010.0, 949-468-8982, 85.59", "1010.0, 949-394-7009, 22.52"};
Map<String, Double> results = new HashMap<String, Double>();
for(String val : data)
{
String [] allStuff = val.split(",");
// I leave any error handling to you!

double value = Double.parseDouble(allStuff[2]);
Double oldVal = results.get(allStuff[0]);
results.put(allStuff[0], oldVal == null ? value : value + oldVal);
}

// now results will contain the sum for each depot
...``````
• 3. Re: String Parsing
Currently Being Moderated
Can u post the exact code, I am in an urgency of submitting the project.
Thanks for the replies..
• 4. Re: String Parsing
Currently Being Moderated
Can somebody post the exact code for the above...
Its a bit urgent...
• 5. Re: String Parsing
Currently Being Moderated
Getting a Null Pointer Exception...
please post me the exact code, i have only this one day today...
• 6. Re: String Parsing
Currently Being Moderated
Hello Mezler (dont knw ur name)

This is my String Array...

*String[] data = { "1000.0, 949-331-2873, 1.1",*
*"1111.0, 949-331-2873, 11.1", "1010.0, 949-331-2873, 111.1",*
*"1000.0, 949-331-2873, 1.1", "1010.0, 949-331-2873, 111.1",*
*"1111.0, 949-331-2873, 11.1", "1010.0, 949-331-2873, 111.1",*
*"1000.0, 949-331-2873, 1.1", "1111.0, 949-331-2873, 11.1" };*

now for 1000.0 -> 1.1 + 1.1 + 1.1 = 3.3, 1111.0 -> 11.1 + 11.1 + 11.1 = 33.3, 1010.0 -> 111.1 + 111.1 + 111.1 = 333.3.

double value = Double.parseDouble(allStuff[2]);
Double oldVal = results.get(allStuff[0])

results.put(allStuff[0], oldVal == null ? value : value + oldVal);

Here why are u addding (value + oldVal), 'oldVal' is the Dept number and 'value' is the amount value.
if u add this two u get 1000.0 + 1.1 and so the code is wrong...

Please can you post me the exact code...
I have to submit the project code and have less time, so post me the code if possible..
• 7. Re: String Parsing
Currently Being Moderated
...
Please can you post me the exact code...
...
\\
Why don't you write the exact code yourself?
\\
\\
...
I have to submit the project code and have less time, so post me the code if possible..
\\
Well, that's your problem, isn't it? Perhaps you should have started earlier, eh?
• 8. Re: String Parsing
Currently Being Moderated
sudheerrnvs wrote:
Hello Mezler (dont knw ur name)

This is my String Array...

*String[] data = { "1000.0, 949-331-2873, 1.1",*
*"1111.0, 949-331-2873, 11.1", "1010.0, 949-331-2873, 111.1",*
*"1000.0, 949-331-2873, 1.1", "1010.0, 949-331-2873, 111.1",*
*"1111.0, 949-331-2873, 11.1", "1010.0, 949-331-2873, 111.1",*
*"1000.0, 949-331-2873, 1.1", "1111.0, 949-331-2873, 11.1" };*

now for 1000.0 -> 1.1 + 1.1 + 1.1 = 3.3, 1111.0 -> 11.1 + 11.1 + 11.1 = 33.3, 1010.0 -> 111.1 + 111.1 + 111.1 = 333.3.

double value = Double.parseDouble(allStuff[2]);
Double oldVal = results.get(allStuff[0])

results.put(allStuff[0], oldVal == null ? value : value + oldVal);

Here why are u addding (value + oldVal), 'oldVal' is the Dept number and 'value' is the amount value.
if u add this two u get 1000.0 + 1.1 and so the code is wrong...

Please can you post me the exact code...
I have to submit the project code and have less time, so post me the code if possible..
Why don't you just paste the code I send into a main method and try it? You will not get a null pointer exception (I used java 5) and the sums will be correct.

You will need to adapt the code to your needs though, but this is your task (you want to learn something, do you?) - this was already written to you.