programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Keep Fractional Part

Greenhorn
Posts: 27
1
Hi, everyone. I'm new to Java and I need help with a code I wrote.

I realized that if one or more of the questions are wrong, then a is 0. I think that is because (cnt1 / 10), loses its fractional part thus becoming 0, sine they're 10 questions to do. I don't know if I am correct. Also, is there a way to make the expression cnt / 10 keep its fractional part?
Thanks in advance for all the help.

lowercase baba
Bartender
Posts: 12565
49
java (and many other languages) do something called "integer division". When you divide an int by an int, you get an int - so yes, any fractional part is lost.

One way to solve it is to use a floating point variable. So your 'a' variable would have to be a float, and you'd have to make one of your terms a float as well...

so you could do something like this:

long a = 0.0;

and then:

a = (cnt1 / 10.0) * 100;

Note: it is early, and I haven't had coffee, so this may not be 100% right, what with floats and longs and so forth, but it should give you the idea.

Marshal
Posts: 56610
172
As Fred has already said, you cannot keep the fractional part in integer arithmetic. You can however get the remainder.
int remainder = i % j;
You can find more about it in the Java Language Specification (JLS). Note the % operator is Euclidean; as it says in that JLS link, you can get the original value back from a combination of / % and *.
You are risking imprecision with floating‑point arithmetic, but you can simply get a double result like this:-
double d = 1.0 * i / j;
Try it with some small values.

Daisy Smith
Greenhorn
Posts: 27
1
Thanks for the replies guys.
Fred, I change my 'a' variable to a double and 10 to 10.0 but it didn't run like I thought it would. This is part of the results.
Congratulations. You are ready to go to the next level Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 'd'
at java.util.Formatter.format(Formatter.java:2448)
at java.io.PrintStream.format(PrintStream.java:937)
at java.io.PrintStream.printf(PrintStream.java:838)
at CAI.main(CAI.java:47)

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
• 1
So you are now getting an exception - not the end of the world. It is very important you start learning how to read those, as they give you a LOT of info on what the problem is. What has printed out is the "stack trace" of all the methods that are currently called...so method A has called method B has called method C...etc.

You start reading them from the top down until you get to something that YOU wrote.

You didn't write java.util.Formatter.format
or java.io.PrintStream.format
or java.io.PrintStream.printf

but you DID write CAI.main - so let's start there. The exception tells you to look at line 47, which is this:

The Stack trace also tells you there is a problem with " Format specifier 'd' " - specifically, you are "MissingFormatArgumentException" - you are missing a format argument.

What do you think the "%d" means in your print string? Also, you can look up on the Oracle docs page what this exception means. In your post, do you see how that is underlined? Click it, and it will take you to the docs page, and you can read what the exception means. Take a look, and see if you can figure out what you need to do.

Campbell Ritchie
Marshal
Posts: 56610
172
Another thing: Have a look through the Formatter specification; there are pages and pages about the % tags, and it tells you what the %d tag means. It also tells you what sort of arguments you can apply to %d.

Greenhorn
Posts: 15
You can also use type casting like this:

Output:

i/j is 0.000000 before cast
i/j is 0.428571 after cast

 Don't get me started about those stupid light bulbs.