EditField substring

So I am making a TextField class to update an old one. However, some code that worked fine before isn’t working now. I was wondering if anybody could help me figure out what is wrong.

Issue:

  • Every character can be deleted excepted for the first one

Code the handles input and updates the text.

Variables not shown:

StringBuilder input;
int pointer;
inputHandler.attach(new InputAdapter() {
			@Override
			public void key(int keycode) {
				boolean pointerShift = (keycode == 203) || (keycode == 205); // left arrow and right arrow
				if (keycode == 14 && input.length() > 0) { // delete key
					if (input.length() > 1) {
						input.deleteCharAt(Math.min(input.length() - 1, pointer - 1));
						pointer -= 1;
					}
				}
				else if (keycode == 211) {  // backspace key
					if (input.length() > 0 || pointer == 0) {
						input.deleteCharAt(Math.min(input.length() - 1, pointer));
					} else {
						input = new StringBuilder();
					}
				}
				else if (pointerShift) {
					if (keycode == 203)  // left arrow
						pointer = Math.max(0, pointer - 1);
					else  // right arrow
						pointer = Math.min(input.length(), pointer + 1);
				}
				else {
					input.insert(Math.max(0, Math.min(input.length(), pointer)), Keys.getLetter(keycode, inputHandler));  // regular key
					pointer += 1;
				}
			}
	}

Any help would be appreciated :slight_smile:

input.length() > 0

in the if statement

This is useless btw:


Math.min(input.length() - 1, pointer - 1)

it could just be


 pointer - 1

it cant be past the length() I am assuming.

Line 6 doesn’t make sense to me, delete should work if there is more than 0 chars (already checked in line above), not more than 1.

I hope you plan on refactoring that when you have the bug sorted out.

I refactored the code a little bit now that it works :slight_smile:

@Override
			public void key(int keycode) {
				if (keycode == Keyboard.KEY_BACK && input.length() > 0) {
					input.deleteCharAt(Math.min(input.length() - 1, pointer - 1));
					pointer--;
				}
				else if (keycode == Keyboard.KEY_DELETE) {
					input.deleteCharAt(Math.max(0, Math.min(input.length() - 1, pointer)));
				}
				else if (keycode == Keyboard.KEY_LEFT) {
					pointer = Math.max(0, pointer - 1);
				}
				else if (keycode == Keyboard.KEY_RIGHT) {
					pointer = Math.min(input.length(), pointer + 1);
				}
				else {
					input.insert(Math.max(0, Math.min(input.length(), pointer)), Keys.getLetter(keycode, inputHandler));
					pointer++;
				}
			}

Could make them all one-liners and you can get rid of the brackets (if your formatter/style guide/conscience allows it):


@Override
public void key(int keycode) {
   if (keycode == Keyboard.KEY_BACK && input.length() > 0)
      input.deleteCharAt(Math.min(input.length() - 1, --pointer));
   else if (keycode == Keyboard.KEY_DELETE)
      input.deleteCharAt(Math.max(0, Math.min(input.length() - 1, pointer)));
   else if (keycode == Keyboard.KEY_LEFT)
      pointer = Math.max(0, pointer - 1);
   else if (keycode == Keyboard.KEY_RIGHT)
      pointer = Math.min(input.length(), pointer + 1);
   else
      input.insert(Math.max(0, Math.min(input.length(), pointer++)), Keys.getLetter(keycode, inputHandler));
}

Depends on what one considers more readable.

True :stuck_out_tongue: But I conceptualize it better with the brackets, because I see where everything starts/ends.

Fair enough. I’m doing more with Clojure these days and the sheer semantic density over a curly-brace langauge like Java was startling at first.