• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing Value from Double to Int (hours to mins)  RSS feed

 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:

I created this:

public int getTransferMin(double hours) {
return(int) Math.round(hours * 60);
}

this was to change a value of a field in a database field from hours to minutes. I am looking through the servlet for the controller and I am getting stuck. I don't know if I should just call getTransferMin as int in the following:



I am getting stuck on the commented out portion. Also i don't know if I should let the db name that was old for hours stay the same and just call getTransferMin to make the call to the database.

Thanks,
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carefully look at this line. It doesn't make much sense: it looks like you're trying to multiply a string "DriveHours" by the number 60.


You probably forgot to call the getAsDouble method, and ofcourse you shouldn't be multiplying a string by a number.


To make it more clear you can first put the result of the getAsDouble call in a variable:

 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your comment. I really appreciate it. When I hover above these lines:




and




I have this message in my Eclipse IDE: "Database type: Undefined, not connected, Commit Mode: Auto

The field name is TransferHours

Should I create a new field called TransferMin

or should I rename is to TransferMin?



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what those messages mean, but they are not Java compiler errors.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michele Smith wrote:I have this message in my Eclipse IDE: "Database type: Undefined, not connected, Commit Mode: Auto
The field name is TransferHours
Should I create a new field called TransferMin
or should I rename is to TransferMin?

Michele, could I make a suggestion?: StopCoding (←click).

You're worrying far too much about the nuts and bolts of what you're doing, and trying to come up with "Java workarounds" for things that aren't working as you hoped; and that says to me that you haven't spent enough time thinking about WHAT you need to do.

Programs should work first time (or very shortly thereafter); they shouldn't be "something that doesn't work" + "a bunch of changes that force it to work".

It takes a bit of practise, but it means that you should be absolutely clear in your mind about all the tasks that are needed to achieve your result before you write your first line of code. It sounds to me like you started coding long before that happened, and you're now relying on band-aids to get it to work.

HIH

Winston
 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have gone through my code and separated out the methods, and I am having problems with just one now, it is: getTransferMin()




is there a better way to state this?

Thanks,
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. You haven't said what that method is supposed to do, nor whether it actually does what you want.
You don't need the D after 60.0. Write 60d or 60.0
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And getTransferMin is a poor name for a method: does min mean minimum or minutes?
 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want it to take a field in a database and convert the time from hours to minutes.
 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Similarly, as in the above, I am having difficulty with this TODO Move to method:



as defined double hours and min are not working.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what exactly does "not working" mean in this case? What does it do? What do you expect it to do? You may want to read our FAQ on ItDoesntWorkIsUseless.
 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is erroring on the line:



however, there is no String input parameter in Math.round(double d);


I noted that the other strings are listed as follows:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michele Smith wrote:It is erroring on the line:



however, there is no String input parameter in Math.round(double d);


Can you tell us what this error is? Show us the error message?

Michele Smith wrote:
I noted that the other strings are listed as follows:



Also, can you tell us where you declared the "hours" variable? And whether it is in scope in the method?

Henry
 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you get an error, please always include the exact error message. Don't just say "there is an error" (or "it is erroring"). What do you mean by that - does the compiler give you an error message? Or does it compile successfully, but do you get an error when you run the program?

Error messages contain important information that gives you (and us) a clue about what is wrong. Therefore, it's important that you tell us exactly what the error message is - it will help us to find out what the problem is. We're not wizards who take one look at your code and immediately know what's wrong with it.

So, please explain exactly what you are doing, and at what step you get an error, and what exactly the error message is.
 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to be unclear. I am getting an error on the line in the Eclipse IDE at:




and




on one class which is used to calculate the minutes to display on the UI.

The other Class has an error in the Eclipse IDE at:

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michele Smith wrote:Sorry to be unclear. I am getting an error on the line in the Eclipse IDE at: ...

"An error"? What is the error message? Surely Eclipse is giving you more information than "there's an error in this line".

Do you see a red squiggly line that indicates that there's something wrong on that line? Put your mouse arrow on the line. A tooltip will appear with an error message. Tell us what that error message is.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that wad of code is the supposed location of where the variable is declared ... which by the way, is a horrible response to "show us the code" because it doesn't tell us anything about where the wad is defined.

The answer is your getTransferMin() method can't access that variable. Although, we can't tell where the variable is, we can tell that it is a local variable. And that local variable is definitely out of scope in the getTransferMin() method. In fact, any local variable is out of scope in any method that it is not declared in.

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!