Question

Can I combine two CharSequence variables like this?

if (status == 1) {
    for (int i = 0; i < get.length(); i++) {
        if (get.charAt(i) == ')') {
        } else {
            temp = temp.toString() + get.charAt(i);     
            // temp and get are charSequence VARIABLES
        }
    }
}         

Syntax is looking OK as no errors from compiler, but the app is crashing.

Also I don't want to get in List and ArrayList items at this beginner stage. Any idea what I am doing wrong?

Also if you say that I shouldn't use .toString() method then I understand but what should I do then to make it correct?

Was it helpful?

Solution 3

are you sure get is not null ?

i tried your code and it's working.

debug and provide values for which your app is crashing.

and you can achieve the same result with :

temp = temp.toString() + get.toString().replaceAll("\\)", "");

OTHER TIPS

Well first you can think about refactoring code a little:

if (status == 1) {
    for (int i = 0; i < get.length(); i++) {
        if (!(get.charAt(i) == ')')) {
            temp += get.charAt(i);
        }                
    }
}

The error you are encountering is in the different place of your code. This one cant throw an exception.

Also for parsing, you should think about regular expressions:

String tryThis = temp.replaceAll("\\)", "");

Yes, it's fine, but what is the main objective that are you looking for?

Generally for such things we use String because it has various methods. Also, you can go with StringBuffer but the first thing should be what is the main objective.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top