2

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?

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Muneeb
  • 63
  • 1
  • 9

3 Answers3

1

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("\\)", "");
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
libik
  • 22,239
  • 9
  • 44
  • 87
0

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.

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Scientist
  • 1,458
  • 2
  • 15
  • 31
  • main objective is to make a calculator like casio :). user will enter an algebric expression and i need to get its answer... so the expression is in character format... at this moment i m trying to solve the brackets first... thats why i need to break this expression to solve it... – Muneeb Oct 10 '13 at 15:59
-1

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("\\)", "");
Eugen Halca
  • 1,775
  • 2
  • 13
  • 26
  • yp i m sure get is not null... but your line code looks intersting... let me try. – Muneeb Oct 10 '13 at 16:07
  • what about `temp ` variable, can it be `null` ? – Eugen Halca Oct 10 '13 at 16:09
  • umm. actually i want to solve it in if statement when i encounter a closing braces.. so i dont want to just convert whole "get charSequence variable" in one line.... i need to got through all characters and respond as i receive characters.... – Muneeb Oct 10 '13 at 16:11
  • @Muneeb you are calling `.toString()` method, and it will throw a `NullPointerException` causing your app to crash – Eugen Halca Oct 10 '13 at 16:14
  • oh alright.... can i initiate temp cahrSequence varaible like that charSequence temp = ""; will tht be null now? if its still then how can i initiate it so tht it wont matter on the first iteration of loop? cause what u r suggesting it loooks like this problem wil only arouse in first iteration of the loop.... – Muneeb Oct 10 '13 at 16:17
  • @Muneeb yes, use `temp = "";` – Eugen Halca Oct 10 '13 at 16:24