Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two CharSequence variables

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?

like image 875
Muneeb Avatar asked Apr 23 '26 07:04

Muneeb


1 Answers

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("\\)", "");
like image 152
libik Avatar answered Apr 25 '26 21:04

libik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!