Jump to content

How to delete the last digit entered?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
organizedchaos

organizedchaos

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Hi,

How can I delete the last digit entered. I have a button named "clear" and the code for the display is
display = " ";
and it removes/clears the entire text. Now my problem is, how do I delete it one character at a time?


Thanks,
Haydee

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Here is an example of how to trim one character off the end using substring and string.length()

        String var = "Entered Text.";
        while(var.length() > 1) {
            var = var.substring(0, var.length()-1);
            System.out.println("Text: "+var);
        }


#3
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
If you need to do it on massive amounts, then for efficiency reasons, i would recommend using StringBuilder instead of String. Something you might want to look up in the future..

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts

Quote

If you need to do it on massive amounts

I've read it's allready better to do if you use 4-5 concatenations using ' + '

#5
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Ofcourse, but in case the part of the code isn't frequently called, it doesn't matter much, as modern computers are fast enough.

But for example, if you try the following code:

    public static void main(String[] args) {
        long start = System.currentTimeMillis();        
        stringConcat();
        System.out.println(System.currentTimeMillis() - start);
        
        start = System.currentTimeMillis();
        stringBuilder();        
        System.out.println(System.currentTimeMillis() - start);
    }

    private static void stringConcat() {
        String s = "";
        for (int i = 0; i < 100000; i++) {
            s += "a";
        }
    }
    
    private static void stringBuilder() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 100000; i++) {
            sb.append("a");
        }
        sb.toString();
    }

You will see a HUGE difference. The '+' concatenation took 13000 ms on my computer, while the sb concatenation took only 0 - 14 ms.

#6
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
Not quite sure I understood the problem completely, but it seems that you want to display some numbers and than have a method to delete a last digit entered from display, and I guess all text and numbers are handeled as String. Here are two codes one is to remove the last digit if the string contains only digits, the other one is if it's mixed with letter or other signs.


public void removeLastDig()

{

    display.trim(); // In case of spaces that could have come after the digits

    display = display.substring(0, display.length()-1);

}




public void removeLastDig()

{

    for(int i = display.length()-1; i >= 0; i--)

    {

        if(Character.isDigit(display.charAt(i)))

        {

            display = display.subSequence(0, i) + display.substring(i+1);

            break;

        }

    }

}