• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's wrong? i want input decimal, multiplication int, and the result is decimal  RSS feed

 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what's wrong? I want to input any number, and the result is decimal, .##
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other than you didn't declare your variable "a", things work for me. Once you declare a--"int a;" if your result is not what you expected, please leave more of a description.
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gabrielle, some advices on code readability and general conventions.

1. Try not to use imports on demand if you are not going to use all classes from the particular package. Better import particular class you're going to use.
For instance "import java.util.*;" better change to import "java.util.Scanner;" the same with other import statements. Actually there is only one scenario when you would need to use such a imports as you have now if classes from different packages would have the same name, so it would conflict by specifying them explicitly during the import.

2. Second thing, please try to write classes starting in Upper case, actually every other word with capital letter. So "spaceboxing" you'd change to "SpaceBoxing". It is general convention, so get this habit to write in this way.

3.when you specify an array [], please write it next to the type String. Why? I know some books says you could write in this way too and they are right, but there is one exception: if you ever need to specify variable arguments type (varargs) which are specified by ... ellipsis, and if you'd write them next to the variable name (String args...), you would get compile error. So, in this case would work only as (String... args). Again, it is just a habit, but one day it could cause a headache.

4. Another thing, variable name "gab", "bb" I understand you wrote it just for a test purpose, but again, getting practice naming variabels which makes sense, it is something which needs to get used to it and requires some practice. Try to give them always names which are descriptive and tells their purpose.

5. Never place curly braces in the way you placed them now. Always stick to a standard like this:

6. Now your particular case with if statement blocks. Use second and third as "else if", because if "a" is equal to 1, in your case "a" won't be 2 or 3 anymore. So there is no point to check all conditions. The difference is, that "if" first condition is satisfied, second "if" going to be checked also, the same as third and so on. with "if - else if" once one of those are satisfied, the others below not being checked.
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Other than you didn't declare your variable "a", things work for me. Once you declare a--"int a;" if your result is not what you expected, please leave more of a description.


Well, it's the complete program.. i can compile, but When i want to input "9.8", it said Exception in thread ... at java.util.scanner, bla bla bla ...
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Hi Gabrielle, some advices on code readability and general conventions.


Thank you.. Next program, i'll try to do it..
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Hi Gabrielle, some advices on code readability and general conventions.


Thanks for your advices.. I'll try to do it next time..
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabrielle Linkherz wrote:
Well, it's the complete program.. i can compile, but When i want to input "9.8", it said Exception in thread ... at java.util.scanner, bla bla bla ...


Well, that "bla bla bla" is the most important part of the exception...

What int-Value (!) do you expect when you type "9.8"? Try nextDouble() instead of nextInt(). And use the right sign ("," or ".") according to your locale.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Dillinger wrote:. . . And use the right sign ("," or ".") according to your locale.
Not necessarily. If I remember correctly, Scanner#nextDouble will happily take "123.456,789" as slightly over 120000 in most non‑English‑speaking European countries, but Double#parseDouble will complain bitterly if you use anything other than . for the decimal point. Please check carefully because I might be mistaken. See also Double#valueOf() which gives more details.
 
Molayo Decker
Ranch Hand
Posts: 48
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This will work fine. You did not declare the variable at the beginning. Good coding practice is to declare variables at the beginning. Second you were passing a double as an integer bb = gab.nextint(); instead of a double bb = gab.nextDouble();. Thirdly i think using a switch statement makes it simpler than using the if statement. It makes you eliminate typing errors from long coding.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Molayo, Welcome to the Ranch.

We discourage posting complete solutions in this forum. Rather, we try to steer the OP in the right direction, letting them discover the answer for themselves. Posting snippets is okay.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Molayo Decker wrote:. . . You did not declare the variable at the beginning. Good coding practice is to declare variables at the beginning. . . .
Most people would disagree with you there, and say you should not declare local variable until you need them.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Molayo Decker wrote:. . . You did not declare the variable at the beginning. Good coding practice is to declare variables at the beginning. . . .
Most people would disagree with you there, and say you should not declare local variable until you need them.


I dunno... I often prefer to declare local variables at the beginning of the method, except for array indexes in for-statements ( "for (int i=0; and so on"). However many people would say if it matters where your method's local variables are declared then the method is perhaps too long.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A main method with more than one statement is too long on spec.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I would agree that if you can get your methods so short that nobody notices where the local variables are, so much the better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!