Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Using loops to Ensure Correct Variable Type from User Input

First off, sorry if my title is a little misleading.

What I mean to ask, is if I am asking a user to input something, say a dollar amount, and they enter it in type double when I want type int, it'll either mess my program up or result in a run time error.

How would I use loops to ensure that this doesn't happen and instead gives the user an error message and asks them to re-input the value? Or is there another way to do it that I do not know of?

I am a very basic beginner taking classes at college when I ran into this problem. I figure I would ask the forums rather then my teacher because I know she doesn't want me to jump ahead of the curriculum too much.

Thanks for all the help!

p.s Here is the program I built where I was thinking I could implement this idea.
Scanner scan = new Scanner(;

int tens, fives, ones;
double money, quarters, dimes, nickels, pennies;

System.out.print("Enter the amount of money you have and I'll tell you how much change you can get
money = scan.nextDouble();

tens = (int)money/10;
money = money%10;
fives = (int)money/5;
money = money%5;
ones = (int)money/1;
money = money%1;
quarters = money/0.25;
money = money%0.25;
dimes = money/0.10;
money = money%0.10;
nickels = money/0.05;
money = money%0.05;
pennies = money/0.01;

System.out.println(tens + " ten dollar bills");
System.out.println(fives + " five dollar bills");
System.out.println(ones + " one dollar bills");
System.out.println((int)quarters + " quarters");
System.out.println((int)dimes + " dimes");
System.out.println((int)nickels + " nickels");
System.out.println((int)pennies + " pennies");
Sign In or Register to comment.