Floating Numbers - 2 basic questions
Hi,
New to programming, and im trying to build a small program which will give back a bit of text for each number a user inputs in depending on the number within each range. I've managed to create that how i want, but i want some kind of validation so that the user must enter a number between 0 and 1000 and if they dont, they are prompted to do so. Whats the code to do this?
I would also like to allow the user to enter decimal places, such as 100.5. And for the number to get rounded up. At the moment, when i type in a decimal place, it gives me an ms dos error msg.
Many thanks in advance
Donut
> New to programming, and im trying to build a small
> program which will give back a bit of text for each
> number a user inputs in depending on the number
> within each range. I've managed to create that how i
> want, but i want some kind of validation so that the
> user must enter a number between 0 and 1000 and if
> they dont, they are prompted to do so. Whats the code
> to do this?
if (value < 0 || value > 100) System.out.println("try again.");
> I would also like to allow the user to enter decimal
> places, such as 100.5. And for the number to get
> rounded up. At the moment, when i type in a decimal
> place, it gives me an ms dos error msg.
It certainly won't give you a DOS error message. In fact, I bet it's a Java exception. I further bet that you use parseInt() or to parse a number that's not an int.
if i have correctly understood ur question here is the solution .
first u declare the number which is to be read as flaot and if u waana print integer than typecast it here is the code
if(i<0||i>100)
System.out.println("try again")
else
System.out.println(" "+(int)i);
thanks for your very helpful response.
the first solution was great.
for the 2nd one, you were right it was an exception:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at Deliverable2.main(Deliverable2.java:23)
i dont quite understand what you mean about the parseint.
i have "int mark" at the beginning of the file and then relate back to the mark with if statements. not sure how i fix this?
thanks in advance for your help!
Hey, when i try to use ur method i get these 3 errors:
C:\Programming\week5\Deliverable2.java:28: ';' expected
else
^
C:\Programming\week5\Deliverable2.java:60: not a statement
else(mark < 101)// the last part of the if statement.
^
C:\Programming\week5\Deliverable2.java:60: ';' expected
else(mark < 101)// the last part of the if statement.
^
3 errors
Tool completed with exit code 1