Results 1 to 10 of 10
  1. #1
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Looks Simple But I Cant Get It Right. Help!

    I was doing code for a simple calculator when i came across this problem..

    i have a JTextField named textfield_1

    i used the code : textfield_1.setText(textfield_1.getText()+"9");
    output : 239 //23 was already present in the textfield

    the problem was when i used this code : textfield_1.setText(textfield_1.getText()+".");
    output : .239
    //Here is the problem //the decimal point should come after the number ie. 239.

    if i again use the 1st code ie. textfield_1.setText(textfield_1.getText()+"9");
    output : 239.9 //So mathematically there are no errors, but the display of 2nd code is the problem


    Can Any1 tell me the reason for that??
    And how to correct that display??
    Last edited by Kuttappu; 02-09-2012 at 12:54 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Looks Simple But I Cant Get It Right. Help!

    Maybe it's just a visibility problem; try to change the dot "." to a "X" for testing purposes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Looks Simple But I Cant Get It Right. Help!

    Quote Originally Posted by JosAH View Post
    Maybe it's just a visibility problem
    Yeah, that lime green hurts the eyes and is barely legible

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Looks Simple But I Cant Get It Right. Help!

    Quote Originally Posted by JosAH View Post
    Maybe it's just a visibility problem; try to change the dot "." to a "X" for testing purposes.

    kind regards,

    Jos
    Thanks a lot for replying

    The problem is with "." only. All other numbers come perfect.. I also tried concat().. same result..
    Is there any problem or bug when "." comes as last character in setText function?

  5. #5
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Looks Simple But I Cant Get It Right. Help!

    Quote Originally Posted by DarrylBurke View Post
    Yeah, that lime green hurts the eyes and is barely legible

    db
    ok ok.. i changed the color..

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Looks Simple But I Cant Get It Right. Help!

    That's not a great improvement...What's wrong with simply bolding it?

  7. #7
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Looks Simple But I Cant Get It Right. Help!

    Quote Originally Posted by Tolls View Post
    That's not a great improvement...What's wrong with simply bolding it?
    I never thought of that. Thank you..

  8. #8
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Looks Simple But I Cant Get It Right. Help!

    Moderator, should this be moved to Advanced? Though it looks simple many, including a teacher of mine couldn't answer this.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Looks Simple But I Cant Get It Right. Help!

    Print out the value of textField.getText() before appending.
    Then append the "." into a holding String and print that out.
    Then pass that holding String to the setText() method.

    Actually, prior to doing that, print out the value of getText() after doing your current code, so you can see what the code is actually holding.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Looks Simple But I Cant Get It Right. Help!

    Quote Originally Posted by Kuttappu View Post
    Moderator, should this be moved to Advanced? Though it looks simple many, including a teacher of mine couldn't answer this.
    Nothing advanced about it, but the question is related to Swing so I'll move it to a more appropriate forum.

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. And note that the first 'S' stands for short.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  2. 30 line simple gui, simple error, need help
    By cc11rocks in forum New To Java
    Replies: 6
    Last Post: 02-05-2011, 08:33 PM
  3. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 02:55 AM
  4. Replies: 2
    Last Post: 05-04-2008, 05:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •