• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange String problem ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m taking the user input like

but my problem is when user press "backspace"
for eg when user enter 2 its ok ,
but when user try to delete that 2 with backspace and if i print the empID then println prints a blank line , and if i check the length of that
String empID its 1 so i guess
if the length is 1 and there is nothing in the string that means the string has a blank space like this " ".
but when i check for the blank string like


how to solve this problem , and what according to you could be there in a string ?
 
Tim Moores
Bartender
Pie
Posts: 2488
4
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the one character that's in the string is not a space, then what is it? The String class has a method to access the individual characters.
 
Anirudh Jhinaa
Greenhorn
Posts: 8
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The unicode for backspace is \u0008 and for space is \u0020. You could check this, though I am not sure if this is your problem.
 
Winston Gutkowski
Bartender
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naved momin wrote:i m taking the user input like

but my problem is when user press "backspace"
for eg when user enter 2 its ok ,
but when user try to delete that 2 with backspace and if i print the empID then println prints a blank line , and if i check the length of that
String empID its 1

Well, depending on what empIdField is, you should look at the API documentation for its getText() method to see how it handles backspaces.

However, the underlying problem here I think is that empID itself is NOT a String. From what I can see, I suspect it's probably a number, but it could also be an EmployeeID class. Either way, it should be converted from the string entered by the user to the correct type.

Effective Java v2, Item 50: Avoid Strings when other types are more appropriate.

Winston
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
naved momin wrote:i m taking the user input like

but my problem is when user press "backspace"
for eg when user enter 2 its ok ,
but when user try to delete that 2 with backspace and if i print the empID then println prints a blank line , and if i check the length of that
String empID its 1

Well, depending on what empIdField is, you should look at the API documentation for its getText() method to see how it handles backspaces.

However, the underlying problem here I think is that empID itself is NOT a String. From what I can see, I suspect it's probably a number, but it could also be an EmployeeID class. Either way, it should be converted from the string entered by the user to the correct type.

Effective Java v2, Item 50: Avoid Strings when other types are more appropriate.

Winston


i agree , on your point , but now we should not concentrate on what string is holding whether a numbers or characters
what i m concern about is when i press backspace means when i type 2 in the empidfield and then if i want to clear that "2" then i need to press "backspace"
so after that
  • what is there is my string ? which returns me a length of 1 but it is not a space like this " " , then what is it ?
  •  
    Tim Moores
    Bartender
    Pie
    Posts: 2488
    4
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    naved momin wrote:what is there is my string ? which returns me a length of 1 but it is not a space like this " " , then what is it ?

    That's for you to find out. Did you read my earlier post?
     
    Anirudh Jhinaa
    Greenhorn
    Posts: 8
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try checking the value of empID string. I am not sure if you have already done this based on my previous post, but you can try this as a debug statement:

    I am assuming you are working with Swing\AWT. Though I have never worked on these technologies, what I can understand is that you are trying to capture a key event in e.getKeyChar(). With this as the context, the value for e.getKeyChar() will be '\u0008'.

    If this is true then the conditional check if(!empID.equals(" ")) wont help as this will be equivalent to if(!empID.equals("\u0020")), where as the value of empID is 'u0008'.

     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic