0

I have following code :

     for(String s:valuearray){
        String s9 = s;
        system.debug('startvalue!!'+s9+'******'+s9.remove(' '));
        List<String> li = s.split(';');
        system.debug('li----'+li);

        System.debug('test%%%%%%%%%'+li[0]+'**'+li[0].deleteWhitespace());


        system.debug('no of whitespace--'+li[0].countMatches(' '));



        Decimal d = Decimal.valueof(li[0].deleteWhitespace());
        if(li[1] != '')
            m.put(li[1],d); 
        singtotal = singtotal +d;          
    }

ValueArray is coming as an array of string from Javascript function in a Lightning component. This code removes whitespaces the first time loop runs(e.g first string in valueArray is '12 000;Q1'). The next time loop runs(eg second string in valueArray is '80 000 000 000;Q2'), it does not read the whitespace, neither does it remove it. Apart from deletewhitespace(),I tried to use replace(), replaceAll(). I tried to use containswhitespace() to check if it is reading the whitespaces or not, but everything works for the first run. What can be the issue.

1

Not exactly sure what is wrong with your code, the state it was in while you were testing, or how you were testing it....

The below seems to do what you are trying to (sans the appropriate error checking)

String[] valuearray = New String[]{'12 000;Q1','80 000 000 000;Q2'};
Map<String,Decimal> results = new Map<String,Decimal>();

for(String s : valuearray){
    String[] tmp = s.split(';');

    if(tmp.size() == 2){

        results.put(
                tmp[1],
                Decimal.valueOf(tmp[0].deleteWhitespace())
        );

    }
}

system.debug(results);

00:16:56.12 (15167472)|USER_DEBUG|[17]|DEBUG|{Q1=12000, Q2=80000000000}

And it does not really differ from what you are doing sans the structure. The above will show you that it is working as expected, then you can modify to meet your needs

I used your code and it also seems to be working like you want it to so not really sure where the issue you are having lies.

String[] valuearray = New String[]{'12 000;Q1','80 000 000 000;Q2'};
Map<String,Decimal> m = new Map<String,Decimal>();
decimal singtotal = 0;
for(String s:valuearray){
String s9 = s;
system.debug('startvalue!!'+s9+'******'+s9.remove(' '));
List<String> li = s.split(';');
system.debug('li----'+li);

System.debug('test%%%%%%%%%'+li[0]+'**'+li[0].deleteWhitespace());


system.debug('no of whitespace--'+li[0].countMatches(' '));



Decimal d = Decimal.valueof(li[0].deleteWhitespace());
if(li[1] != '')
    m.put(li[1],d);
singtotal = singtotal +d; }

system.debug(m);

00:14:11.11 (13573473)|USER_DEBUG|[6]|DEBUG|startvalue!!12 000;Q1******12000;Q1

00:14:11.11 (13749569)|USER_DEBUG|[8]|DEBUG|li----(12 000, Q1)

00:14:11.11 (13830067)|USER_DEBUG|[10]|DEBUG|test%%%%%%%%%12 000**12000

00:14:11.11 (13938712)|USER_DEBUG|[13]|DEBUG|no of whitespace--1

00:14:11.11 (14287288)|USER_DEBUG|[6]|DEBUG|startvalue!!80 000 000 000;Q2******80000000000;Q2

00:14:11.11 (14409004)|USER_DEBUG|[8]|DEBUG|li----(80 000 000 000, Q2)

00:14:11.11 (14459516)|USER_DEBUG|[10]|DEBUG|test%%%%%%%%%80 000 000 000**80000000000

00:14:11.11 (14545915)|USER_DEBUG|[13]|DEBUG|no of whitespace--3

00:14:11.11 (14769029)|USER_DEBUG|[22]|DEBUG|{Q1=12000, Q2=80000000000}

I suspect your issue may have been with the assumption that

system.debug('no of whitespace--'+li[0].countMatches(' '));

would return 0. but that is not the case since you never assigned the value of li[0].deleteWhitespace() to li[0]. Without assigning the results of this operation, the value of the variable is unchanged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.