Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

mony
5,283 PointsHow to accept input if user enters int or double/float?
I want the input to take any number, whether it be an integer, double/float. I can only get it to accept one or the other. Code is very bad/corny I know, but I would appreciate pointers on any part of it too : ) . Can someone help? Thanks.
Scanner input = new Scanner (System.in);
float n1;
float n2;
float n3;
float n4;
float sum = 0;
float product = 0;
float difference = 0;
float quotient = 0;
float answer = 0;
String name;
String kind;
System.out.println("Hi, what is your name?: ");
name = input.nextLine();
System.out.printf("Hi there %s! What kind of math do you want to try today?\n (+,-,*,/): ", name);
kind = input.nextLine();
System.out.printf("OK %s, give me your first number: \n", name);
n1 = input.nextFloat(); /*<--- can I put input.nextInt(); and
input.nextFloat(); in an if statement?
Iv'e tried things like this --->
if (input.nextInt() || input.nextFloat()) {
} */
System.out.printf("Now what is the second number: \n");
n2 = input.nextFloat();
System.out.println("Nice, one moment please...\n");
sum = (float) n1 + n2;
difference = (float) n1 - n2;
product = (float) n1 * n2;
quotient = (float)n1 / n2;
if (kind.equals("+")){
answer = sum;
System.out.printf("After some digging, I found that\nthe answer to your problem:"
+ " %.2f %s %.2f ... is %.2f", n1, kind, n2, answer );
}
if (kind.equals("-")){
answer = difference;
System.out.printf("After some digging, I found that\nthe answer to your problem:"
+ " %.2f %s %.2f ... is %.2f", n1, kind, n2, answer );
}
if (kind.equals("*")){
answer = product;
System.out.printf("After some digging, I found that\nthe answer to your problem:"
+ " %.2f %s %.2f ... is %.2f", n1, kind, n2, answer );
}
if (kind.equals("/")){
answer = quotient;
System.out.printf("After some digging, I found that\nthe answer to your problem:"
+ " %.2f %s %.2f ... is %.2f", n1, kind, n2, answer );
}
}
}
3 Answers

Kristian Terziev
28,449 PointsYou can take a float as an input from the user and then if needed be you can cast it to an integer. That happens by using Math.round() which will round the float to the nearest integer.

mony
5,283 PointsThanks Kristian,
The problem is that I don't want to round the input then do int math, or vice versa. I just want for any number to be allowed as input. So if 25 is entered as the first number, and 5 is entered as the second it can solve that. However, if instead the two numbers are 25.39 and 5.6583 it can solve that as well. What would I do in that case? Thanks.

mony
5,283 PointsNever mind. Somehow what I have there does work.