This discussion is archived
9 Replies Latest reply: Mar 11, 2010 9:52 AM by 843807 RSS

how to remove the last new line in text area ?

843807 Newbie
Currently Being Moderated
I have a calculator Applet that shows the calculation in a text area.
if i enter 2+6, the text area shows
2+
6

and i have a clear last button, which deletes the last entry by user.
in the example above, one hit of clear last, deletes 6. another hit should delete +.

I can delete the numbers, but I don't know how to go back to the previous line and delete the operator.

I may have other calculations in that text area above the current calculation. so replaceAll("\n","") would not work here.

Please someone help me here.
  • 1. Re: how to remove the last new line in text area ?
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Is this Swing or AWT (JApplet or Applet)?

    To get better help sooner, post a [_SSCCE_|http://mindprod.com/jgloss/sscce.html] that clearly demonstrates your problem.

    Use code tags to post codes -- [code]CODE[/code] will display as
    CODE
    Or click the CODE button and paste your code between the {code} tags that appear.

    db
  • 2. Re: how to remove the last new line in text area ?
    843807 Newbie
    Currently Being Moderated
    it's an applet, i am developing in Eclipse ( mac environment)

    so for deleting a number i used this code, and works fine
    String temp = t.getText();
                             String lastChar = temp.substring(temp.length()-1);
                             for (int i=0; i<numbers.length; i++) // deleting a number
                                  if (lastChar.equals(numbers)){
                                       temp = temp.substring(0,temp.length()-1);
                                       number = number.substring(0,number.length()-1);
                                       t.setText(temp);
                                  }
    which t is the text area, and numbers is the string array of 0 to 9, and number is a string where i store the entered number
    so i thought for deleting an operator i should use something similar, where i need to go back to the previous line and delete it,
    String temp = t.getText();
                             String lastChar = temp.substring(temp.length()-1);
                             if(lastChar.equals("n")){
                                                            temp = temp.substring(0,temp.length()-3);// deleting n , \ , and the operator
                                       t.setText(temp);
                             }
    but it doesn't work
  • 3. This Thread is now moved
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Note: This thread was originally posted in the [New To Java|http://forums.sun.com/forum.jspa?forumID=54] forum, but moved to this forum for closer topic alignment.
  • 4. Re: how to remove the last new line in text area ?
    800560 Newbie
    Currently Being Moderated
    elmira84 wrote:
    but it doesn't work
    The newline character is represented as "\n" but it is not the characters '\' and 'n' (I think it is a single character.) So you won't find "n"

    It looks like the last character will always be "\n" so why not always remove the last 2 characters (the operator and the newline)?
  • 5. Re: how to remove the last new line in text area ?
    843807 Newbie
    Currently Being Moderated
    As Darryl suggested, please post an SSCCE demonstrating your problem, not just code snippets. Also note that an SSCCE doesn't mean your current application, as it is likely too large to qualify.
  • 6. Re: how to remove the last new line in text area ?
    843807 Newbie
    Currently Being Moderated
    So you are saying \n counts as one character?
  • 7. Re: how to remove the last new line in text area ?
    800560 Newbie
    Currently Being Moderated
    elmira84 wrote:
    So you are saying \n counts as one character?
    Yes
  • 8. Re: how to remove the last new line in text area ?
    843807 Newbie
    Currently Being Moderated
    I tried the one I was thinking (n and \ as 2 separate character), and it worked.
    but mayber you are right and so how both works the same, I'm not sure.

    But thanks for your time and help.
  • 9. Re: how to remove the last new line in text area ?
    843807 Newbie
    Currently Being Moderated
    elmira84 wrote:
    but mayber you are right and so how both works the same, I'm not sure.
    '\n' is an escape sequence for a line feed (end of line char). In Swing, all Documents use \n as the newline character. It is indeed a single character, so for example, you can do this in Java:
    char ch = '\n'; // Valid
    char ch = "\n"; // Invalid, a string with a single char '\n' is not the same as just the char
    Other character escapes include '\r' (carriage returns), '\f' (form feeds) and '\t' (a tab).