Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Length Converter Calc

 
Vinay Berry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble with some code I'm building a length converter that converts length from 5 text fields if the user enters a value in one text field for say millimeter, then meters, yards, feet and inches should be filled out when the convert button is clicked. I wrote 5 if statements that get text from the field if the field has text in it then it should execute the code. The problem is it doesn't follow these statements it just runs through all the if statements and tries to perform all the operations.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use == or != for objects, which includes Strings. Use the equals method.
 
Vinay Berry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i implement that into the code?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinay Berry wrote:how can i implement that into the code?


Instead of something like this:

You would do this:


John.
 
Vinay Berry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks it works now!
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To check for empty Strings you can also use !MField.getText().length() == 0.
 
Vinay Berry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:To check for empty Strings you can also use !MField.getText().length() == 0.


haha we just learned about that method too it probably would've impressed the professor if I used it in my code
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be looking through the API documentation anyway, so you ought to have found that method.
 
Sharon nguyen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinay Berry wrote:I'm having trouble with some code I'm building a length converter that converts length from 5 text fields if the user enters a value in one text field for say millimeter, then meters, yards, feet and inches should be filled out when the convert button is clicked. I wrote 5 if statements that get text from the field if the field has text in it then it should execute the code. The problem is it doesn't follow these statements it just runs through all the if statements and tries to perform all the operations.

Thanks you for the post.
Hi guys, Im a newbie. Nice to join this forum.
__________________
Movies Online Free

 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch ( ), Sharon nguyen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic