This question already has answers here:
Simple division operation returning zero?
(5 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Hey people of Stackoverflow! I just have a question about an error that I came across while doing this lesson on Java online. So this is the code:
import java.util.Scanner;
public class GradesAndPoints {
public static void main(String[] args) {
System.out.print("Type in your score between (0-27): ");
Scanner ask = new Scanner(System.in);
int num = ask.nextInt();
int result = (num/27);
System.out.println(result);
The error is: whenever I run the code with the variable "num" being any int value, it prints out to be 0. Can someone explain to me why this error occurs and a solution I can implement to solve this?
The way you're doing this, you're diving integers. This, by definition, will get you an integer that is truncated.
5 / 10 = 0
If you turn one of them into a float (by adding a . at the end), you will get floating point division, which is what you're looking for.
5. / 10 = 0.5
5 / 10. = 0.5
5.0 / 10.0 = 0.5
Related
This question already has answers here:
Get a specific digit of a number from an int in Java [duplicate]
(4 answers)
Closed 3 years ago.
How to extract the hundred of an int variable?
For example, I have a random number:
int i = 5654217;
I want code to extract the number "2".
I tried to do
i/100
Which gave me 56542.
But I can't find a way to extract only the last number.
Too, I'm really unsure this is the best way to extract the hundred of the variable.
I am not 100% sure what you are asking so I will put the two guesses that I have of what your question is. If it doesn't answer your question please feel free to let me know, I will help you.
1) You are dividing an integer (int) by 100 and the last 2 digits disappear.
double x = (double)i/100.0;
//ints cannot store a decimal
2) You have a decimal (double) and are trying to output hundreds digit.
public int hundredthsDigit(double x){
if(x>0.0) return (x/100)%10;
//This moves the 100s digit to the 1s digit and removes the other digits by taking mod 10
return 10-Math.abs(x/100)%10;
// does practically the same thing, but is a work around as mod doesn't work with negatives in java
}
The modulus operator, % effectively gives you the remainder of a division.
You can get the last digit by getting the number, mod 10. Try (i / 100) % 10
You can read up more on modular arithmetic and such here: https://en.m.wikipedia.org/wiki/Modular_arithmetic
Please find code below:
package com.shree.test;
public class FindNumber {
public static int findNumberAt(int location,int inputNumber) {
int number = 0;
//number = (inputNumber % (location*10))/location; // This also works
number = (inputNumber/location)%10; // But as mentioned in other comments and answers, this line is perfect solution
return number;
}
public static void main(String[] args) {
System.out.println(findNumberAt(100, 5654217));
}
}
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
when i want to get the result of 100/100000..
i got only 0.
Example
int one = 100;
int two = 100000;
int result = one/two;
toast(result); //Result is 0
Hey there "int" data type only stores integer values and not the decimals.
So if you divide 3 with 2 you would get 1 as answer instead of 1.5 .
Int just ignores the decimals .
You need to choose float or double data type for this to work.
Your variable named result must be declared and casted to float data type.
Appreciate the effort and mark this as answer if it helps you.....
This question already has answers here:
Division operation is giving me the wrong result [duplicate]
(3 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Here is my code, Eclipse always return integer value of results, even if h is double. Please help me to fix this.
public static void main(String[] args) {
double h=0.0;
for(int i=1;i<=1000;i++) {
h=h+ 1/i;
}
System.out.println("Harmonic sum "+h);
System.out.println("Harmonic sum "+String.format("%.4f", h));
Result:
Harmonic sum 1.0
Harmonic sum 1,0000
You need to cast the result of your division into double
Try this out:
h=h+ (double)1/i;
I hope this is what you were looking for.
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
public class Test {
public static void main(String[] args) {
int test = 1;
System.out.println((double)(Math.pow(test/++test, 2)));
}
0.0 is printed to the screen. Why? Why is the cast not working as expected?
If test is declared this way...
double test = 1;
I get what I expect to print... 0.25.
Why?
I am new to programming and I'm playing around. Reading some of the documentation at this level is next to useless.
You are doing integer division. SO change the line to
System.out.println((Math.pow(test/(double)++test, 2)));
to do double division
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 8 years ago.
I'm writing a method in java which relates to a die throw and I try to use the math.random() but i realized that 0 is included in the random integers involved. Additionally, I don't quite get the *7 part what does it mean?
I went to research from the Java API but it doesn't mention any bit about this or is it I am doing the wrong research? Thanks so much for reading!
public int dieThrow()
{
int num = (int)(Math.random() *7); //returns an integer
return num;
}
This is a pretty simple exercise. You observe that 0 is a possible outcome, so you simply + 1 to the result, like so:
public int throwDie()
{
return (int)(Math.random() * 6) + 1;
}