How do I get the separator + a more general question

I have a textbox in which I enter numbers. I extract the individual strings and I would like to know how I can extract the specific separators. Also a more general question. If the processing of this text data would get more complicated how should I proceed So far as seen in the code example it's no problem doing everything inside the textbox change event function. For the moment the indata is supposed to be integers that are added up. But I would like to extend this so the specific arithmetic operations are performed and also that indata could be both integers and float numbers. What is the way to do it

Here is my code example

private void textBox1_TextChanged(Object sender, System.EventArgs e)

{

String str = textBox1.get_Text();

String individual[] = str.Split(new char[] { ' ', '+', '-', '*', '/' });

int i = 0;

int sum = 0;

for (i = 0; i < individual.get_Length(); i++)

{

String s = (String)individual.get_Item(i);

if (!(s.Trim().Equals("")))

sum += (int)Integer.parseInt(individualIdea);

}

str = System.Convert.ToString(sum);

if (i > 0)

label1.set_Text(str);

}




Answer this question

How do I get the separator + a more general question

  • manukahn

    What do you mean about creating a tree

    Also I have read that in JAVA you can't solve problems by thinking about how to solve the problem but only about what you wan't to do In my example it surely don't seem so.



  • Lord Kaos

    Hi,
    What I understand from your question is that you have a TextField or TextArea and the user enters arithmatic expression in it, and now you want to compute the that expression. In that case, the TextField or TextArea can only give you back the entered string and then you need to apply logic to parse that string.

    One way to do it could be to construct a tree from that string and then traverse through it and compute the result. Another way could be to construct a postfix expression from that string and then compute the result. You can find many such algos on the net,

    thanks
    Prem



  • How do I get the separator + a more general question