Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

decimal rounding  RSS feed

 
Daniel valdez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


any one had an idea of what i'm doing wrong i am supposed to write a code for decimal rounding this is some of what i came out with but keep getting errors PLEASE HELP






What am i doing wrong ???
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel - Welcome to the Ranch!

Did you get an error when you were compiling? Line 5 (answer long;) - isn't quite right.

Please use Code Tags when posting code - I've added them for you this time.
 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel, it would be better if you posted the actual error if Ron's answer doesn't help you. Also post round method's code in that case.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
If you need help, the more details you supply, the easier it will be to help you. Please also tell us where the round() method you are using comes from, and show us its code if you wrote it.
 
Ahmad Auada
Ranch Hand
Posts: 56
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel, see how you called this method?:

input = console.nextDouble();
As you can see, you didn't need to declare or define the method .nextDouble(). Because some nerds have already written the details of this method in a class called console. Your code imported this class.

But maybe you have a different idea for getting the next double, so you define your own method, like this:

now, you want to use your method. But how would you tell the system which method you want to use? The one you made?(Which belongs to the decimalRound class) Or the one you imported?(Which belongs to the console class)

Here is the rule, if the method belongs to an external class, then you would:
-call the method directly off the class, because the method is static.Like the case of

-if the method was not defined as static, you would create an object and call the method off the object.(The method has to be defined as public so that you can use it) ex:

If the method belongs to the class you made, in this case, roundDecimal, you would just call it anywhere in your code. No dot operators or anything:


Now look at this line in your code:


the method round is not defined in your class, so that means that you either define it, or just import an external class that was written by some nerds which would contain the implementation of the round method.
That class exists, and is called Math, so


let me ask you a question, can you fill in these spaces?:


 
Daniel valdez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help im currently taking a class and im lost on some of the codes,heres what i have for code

this is the error i get


 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 6 you got too many parenthesis ")".

What are the purpose of those multiple string literals (an empty string + space character) in print statements on line 14 and 18? I have in mind "" + " " + ....
Also, what is the purpose of line 9?

main method parameters write as (String[] args), and not as (String args[]). Round brackets suppose to be placed after the data type and not a reference variable. At some point you could run into problems if you keep doing as you do currently. And it is not conventional. Also call reference variable name as args, not agrs - because of arguments in short, not agruments.

More about conventions. Don't add after each written code line an empty line, that is too much, gets difficult for the eye to distinct relevant logic parts from each other. Check Java Coding Conventions (<- link). Document is from 1999, but coding techniques are still widely applied.
 
Daniel valdez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks finally found the problem thanks for the help. And since I'm taking a course I'll be asking for more help in the future so in advance thanks
 
Ahmad Auada
Ranch Hand
Posts: 56
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel valdez wrote:Thanks finally found the problem thanks for the help. And since I'm taking a course I'll be asking for more help in the future so in advance thanks

Good luck 😊
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done finding it Please tell us what the solution was.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel valdez wrote:Thanks finally found the problem thanks for the help. And since I'm taking a course I'll be asking for more help in the future so in advance thanks

One style point for you.
  public static void main(String agrs[]) { ...
(even ignoring the spelling mistake) should be:
  public static void main(String[] agrs) { ...
because the type of the parameter is String[].

It is true that Java allows the style you wrote, but I've never understood why - and I suspect there are others out there like me who find it very annoying.

Winston
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
It is true that Java allows the style you wrote, but I've never understood why



I blame C.
To be honest I usually do.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C might be to blame; some C syntax was copied int Java probably because so many people were familiar with it. Also syntax was taken from C and C++ but to mean something slightly different. People still stumble over those differences.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!