wood burning stoves 2.0*
The moose likes Java in General and the fly likes strange String problem ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "strange String problem ?" Watch "strange String problem ?" New topic
Author

strange String problem ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 682

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 ?

The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Aug 25, 2007
Posts: 8
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

Joined: Mar 17, 2011
Posts: 7081
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 682

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
    Rancher

    Joined: Sep 21, 2011
    Posts: 2408
    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

    Joined: Aug 25, 2007
    Posts: 8
    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'.

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: strange String problem ?
     
    Similar Threads
    Streaming PDF files
    Regarding apply JOIN by using JPQL.
    Password Encryption
    tomcat session problem
    Passing parameter between pages and populating page with values.