Replace character in string?

I have homework in programming, and I would like some help with it.

I would like a text input box, where i could type in a single char either l, f, g or h and then the character would be inserted in a string.

My code:

  for (x = 0.001; x < 2; x += dx) {
    A+=h(x)*dx;
  }
  println(A);
  exit();

As you can see... A+=h(x)*dx; will calculate the function h(x) I would like a popup text box which changes the (h) with one of the other characters, so that it will calculate that instead.

Best regards

And sorry for my rusty english :D

Answers

  • Your English is okay, but I'm not sure what you're asking. Which part of this are you stuck on? What are you confused about?

    Try breaking your problem down into smaller steps. Can you create a small example program that simply shows a text input box and prints out the value the user enters? Can you create another separate example program that simple inserts a String into another String? Keep building example programs from the separate pieces, and when they all work great by themselves, then worry about combining them.

    If you get stuck on one of the example programs, post it here and we can help you. Don't forget to use the code button to preserve formatting. Good luck!

  • Answer ✓

    it's not as simple as changing one character though. from the description he wants it to use a whole different function (one of 4) based on what the user typed.

    so if the user types a g then the sum would be A += g(x) * dx; etc

    look at keyPressed() in the reference. have four conditions each containing a different loop, choose based on the key.

  • I am not sure to understand either. Perhaps show a string before and after, stuff like that.
    Not sure what the "popup text box" does there, too. Do you know text() function?

  • Given he is a beginner doing homework, I doubt it... :-)

  • edited April 2015

    koogs got me right. I'm a beginner in programming so I rather prefer a simple method if there is any.

  • you'll need to repeat the code, unfortunately. something like this:

    float a = 0;
    if (method == 'g') {
      for (...) {
        a += g(x) * dx...
      }
    else if (method == 'h') {
      for (...) {
        a += h(x) * dx...
      }
    } else...
    

    (^ not real processing code)

    (where 'method' is a char variable set according to the key pressed)

  • edited April 2015

    We can call function names as String via undocumented Processing function method("").
    For example, we can have these 3 functions:

    void g() {}
    void h() {}
    void z() {}
    void myOtherFunction() {}
    

    If we wish to call h(), we just issue: method("h");
    For myOtherFunction(): method("myOtherFunction");
    And so on and forth... :ar!

Sign In or Register to comment.