3

I have code for convert each 2 numbers to a specific character, this code works, but output is with an unwanted word "null". I want to remove it. I can't identify which part of code is wrong.

var encoded_numbers:String = "102030";
var sub:String;
var decode_string:String;

for (var i2:int = 0; i2 < encoded_numbers.length; i2 += 2) 
{
    sub = encoded_numbers.charAt(i2) + encoded_numbers.charAt(i2 + 1);
    //trace(sub);

    switch(sub) 
   { 
    case "10": 
        decode_string += "A";
        break; 
    case "20": 
        decode_string += "B";
        break; 
    case "30": 
        decode_string += "C";
        break; 

   }

}
trace(decode_string);//convert      

Output string:

nullABC

2 Answers 2

5

Initial value of your decode_string is null. Just assign the initial value as follows:

var decodeString:String = "";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your simple answer.
4

"...but output is with an unwanted word "null". I want to remove it. I can't identify which part of code is wrong."

When you say : var decode_string:String; you've declared a variable but it has no value so by default it gives result of "null". The problem later is when you use += operator to further append your ABC letters, the final string has now become nullABC.

To fix :

Make your strings empty/blank (but never value-less) by using String = "";...

var encoded_numbers:String = "102030";
var sub:String = "";
var decode_string:String = "";

Also consider substr for extracting parts of texts (instead of charAt)..

for (var i2:int = 0; i2 < encoded_numbers.length; i2 += 2) 
{
    sub = encoded_numbers.substr(i2, 2); //gets 2 letters from position of i2
    //trace(sub);

    switch(sub) 
    { 
        case "10": 
            decode_string += "A";
            break; 
        case "20": 
            decode_string += "B";
            break; 
        case "30": 
            decode_string += "C";
            break; 

    }

}
trace(decode_string);//convert     

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.