# Thread: 4 digit decimal integer help appreciated very much

1. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## 4 digit decimal integer help appreciated very much

So I;ve completed this program and it almost works perfectly:

Write a program that asks the user to enter a positive decimal integer with up to 4 digits. The program displays Thats it! if the user actually does enter a positive decimal integer with up to 4 digits, and Thats incorrect! if the user enters something which is not a positive decimal integer with up to 4 digits. (Hint: your program reads the users input as a String.)

Unfortunately when i run it, if i type in any decimal i get an error, but when i type in a normal number the program runs as it should. I would greatly appreciate someone looking at my program and telling me what needs to be fixed. Thanks so much!

import java.util.*;
import java.util.Scanner;

public class DigitPrinter {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int decimalNumber;
System.out.print("Enter a positive decimal integer with up to 4 digits. ");
decimalNumber = in.nextInt();

if (0 < decimalNumber && decimalNumber < 1)
System.out.println("That's it!.");
else {
System.out.println("That's incorrect!");

}
}
}  Reply With Quote

2. Moderator  Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
12

## Re: 4 digit decimal integer help appreciated very much

Read the API for the classes you are using, in particular Scanner:
Scanner (Java Platform SE 6)
It describes why an exception is thrown under certain input for each method, and also has other methods that you may find useful.  Reply With Quote

3. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## Re: 4 digit decimal integer help appreciated very much

Thanks for the reply! Yes, I look a look at the link, but it's still difficult for me to understand (i'm new at this!). So what do you suggest I do to my program to fix this error? Thanks again.  Reply With Quote

4. ## Re: 4 digit decimal integer help appreciated very much Originally Posted by JMAsterson Thanks for the reply! Yes, I look a look at the link, but it's still difficult for me to understand (i'm new at this!). So what do you suggest I do to my program to fix this error? Thanks again.
First you need to understand how to use Scanner. Why not tell us what confuses you about the tutorial?  Reply With Quote

5. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## Re: 4 digit decimal integer help appreciated very much

It's more that the reading is so long it is almost overwhelming. I e-mailed my professor and this was his response to my problem:
I'm afraid the program isn't actually close to doing what you want it to do --
unless I misunderstand the problem.

To figure out what the program is doing, I suggest
you add a System.out.println() after line 10 to display the value of DecimalNumber
(i.e. right after DecimalNumber is set to what's input),
then run the program and see what gets displayed,
and see if you can explain the value of DecimalNumber at that point.
(Hint: what does "nextInt()" do?)

I am also now a bit confused on what he says, because I dont understand what he is telling me to write in the System.out.println().
It's just that, I dont know what I should to change my program to make it work. Sorry I'm not very advanced, i find it difficult to under some of these explanations - thanks for helping.  Reply With Quote

6. ## Re: 4 digit decimal integer help appreciated very much Originally Posted by JMAsterson It's more that the reading is so long it is almost overwhelming.
Ah, I see that he linked to the API, not the Scanning tutorial. Please have a look at the tutorial -- Scanning Tutorial -- which I think you'll find much easier to read and understand.

I e-mailed my professor and this was his response to my problem:
I'm afraid the program isn't actually close to doing what you want it to do --
unless I misunderstand the problem.

To figure out what the program is doing, I suggest
you add a System.out.println() after line 10 to display the value of DecimalNumber
(i.e. right after DecimalNumber is set to what's input),
then run the program and see what gets displayed,
and see if you can explain the value of DecimalNumber at that point.
(Hint: what does "nextInt()" do?)

I am also now a bit confused on what he says, because I dont understand what he is telling me to write in the System.out.println().
It's just that, I dont know what I should to change my program to make it work. Sorry I'm not very advanced, i find it difficult to under some of these explanations - thanks for helping.
He's suggesting that you do something like this:

Java Code:
```import java.util.Scanner;

public class DigitPrinter {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int decimalNumber;
System.out.print("Enter a positive decimal integer with up to 4 digits. ");
decimalNumber = in.nextInt();

System.out.println("decimal number is: " + decimalNumber);

if (0 < decimalNumber && decimalNumber < 1)
System.out.println("That's it!.");
else {
System.out.println("That's incorrect!");
}
}
}```
So you will see what is the value held by your variable.

As for your program itself, your logic doesn't quite make sense as your if block checks to see if a decimal number is greater than 0 but less than 1 -- how many decimal numbers fulfill that criteria? What criteria should you be checking?  Reply With Quote

7. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## Re: 4 digit decimal integer help appreciated very much

Oh I see what you mean there... You seem to have a good understanding of what I am asking. As far as the decimal being greater than 0 and less than 1... I'm not very good at the math part. Now it makes sense - not all decimals are in that limit! So as far as the math goes.... what values do you suggest i put in the if() in order to make sure that a decimal gets "That's it!". I dont exactly know how to word that, I hope you know what I mean.  Reply With Quote

8. ## Re: 4 digit decimal integer help appreciated very much Originally Posted by JMAsterson Oh I see what you mean there... You seem to have a good understanding of what I am asking. As far as the decimal being greater than 0 and less than 1... I'm not very good at the math part. Now it makes sense - not all decimals are in that limit!
Actually, no decimal number satisfies that if statement.

So as far as the math goes.... what values do you suggest i put in the if() in order to make sure that a decimal gets "That's it!". I dont exactly know how to word that, I hope you know what I mean.
Please give it at least one more go (if not more), as I know that you can do this without our help.  Reply With Quote

9. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## Re: 4 digit decimal integer help appreciated very much

I know, I do see that no decimal number will work. And please, don't worry - I'm not trying to get other people to do my work for me- apparently i'm not very good at this though. Did I set up the if statement correctly though? (I mean what is in the parentheses.) Perhaps I shouldn't be writing something like (x < decimalNumber < x) ?  Reply With Quote

10. ## Re: 4 digit decimal integer help appreciated very much

Your current code's if statement structure could work well, and in fact your lower bound test is good -- you will remove all negative numbers, but your upper bound test could use some work. Again, I think that the code structure is OK, but the value used, 1, isn't a good one. Keep trying.  Reply With Quote

11. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## Re: 4 digit decimal integer help appreciated very much

Ok, I emailed y teacher and here is what he said, it certainly changed the way i am thinking about my program:
I'm afraid we have a totally different interpretation of the term "decimal integer"!

For me, :a "decimal integer" is an integer, therefore a whole number -- no fractional part, no part less than 1. For me, the "decimal" in "decimal integer" means that the number is base 10 -- e.g. not a binary (base 2) number or hexadecimal (base 16) number. So, 362 and 1983 are decimal integers of 4 or fewer digits, while 2.5 is not a decimal integer.

I suspect that you took "decimal integer" to mean "a number with a fractional part, expressed as a decimal number" -- I think this is oftentimes termed a "decimal number" because a decimal point occurs in the number. Such numbers are definitely not decimal integers.

So, according to my teachers definition of a decimal integer, here is my edited program. it seems to do almost exactly what he wants it to do: .... except now my small problem is a bit reversed, because im trying to tell the program thats its wrong to enter a decimal. I get an error if i enter one. here is my program, please take a look at it!:

import java.util.Scanner;

public class DigitPrinter {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int decimalNumber;
System.out.print("Enter a positive decimal integer with up to 4 digits. ");
decimalNumber = in.nextInt();

System.out.println("decimal number is: " + decimalNumber);

if (0 < decimalNumber && decimalNumber < 9999)
System.out.println("That's it!");
else {
System.out.println("That's incorrect!");
}
}
}  Reply With Quote

12. ## Re: 4 digit decimal integer help appreciated very much

I'm sorry, which of the statements above are your teacher's definition of "decimal integer"?  Reply With Quote

13. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## Re: 4 digit decimal integer help appreciated very much

Sorry, this one is his definition: a "decimal integer" is an integer, therefore a whole number -- no fractional part, no part less than 1. For me, the "decimal" in "decimal integer" means that the number is base 10 -- e.g. not a binary (base 2) number or hexadecimal (base 16) number. So, 362 and 1983 are decimal integers of 4 or fewer digits, while 2.5 is not a decimal integer.  Reply With Quote

14. ## Re: 4 digit decimal integer help appreciated very much

So your program works correctly -- it tells the user if the number is not negative and 4 digits or less, correct?

What behaviors are you trying to add? Please be as specific as possible.  Reply With Quote

15. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## Re: 4 digit decimal integer help appreciated very much

yes, it does that. But you'll notice if you enter a decimal the console window gives an error. I'm just wondering if theres a way to specify if someone enters a decimal it should come up as "thats incorrect" also, and not an error. But im not sure how to write that.  Reply With Quote

16. ## Re: 4 digit decimal integer help appreciated very much Originally Posted by JMAsterson yes, it does that. But you'll notice if you enter a decimal the console window gives an error. I'm just wondering if theres a way to specify if someone enters a decimal it should come up as "thats incorrect" also, and not an error. But im not sure how to write that.
OK, that helps a lot. Yes there is a way to do that, which is to use a try/catch block and catch a NumberFormatException. You would create a try block and in it accept the input in as a String (use Scanner's nextLine() method), and then use Integer.parseInt(...) to try to parse the String into an int. If it fails, the catch block would warn the user.

something like (in pseudocode):

Java Code:
```boolean keepAsking is set to true
do
try:
prompt user for number
Get a String from the user
parse the String into an int  // if this fails, the catch block takes over
// if this line is reached, the parse has succeeded
catch a NumberFormatException
warn user that input is bad and that they will need to try again  Reply With Quote

17. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## Re: 4 digit decimal integer help appreciated very much

Ok, where in my program would i fit this portion (your example) in?  Reply With Quote

18. ## Re: 4 digit decimal integer help appreciated very much Originally Posted by JMAsterson Ok, where in my program would i fit this portion (your example) in?
That's for you to test, experiment and discover.  Reply With Quote

19. Member Join Date
Mar 2012
Posts
55
Rep Power
0

## Re: 4 digit decimal integer help appreciated very much

Ok thanks for everything.  Reply With Quote

decimal printer 