Results 1 to 19 of 19
 03292012, 02:56 AM #1Member
 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!");
}
}
}
 03292012, 03:07 AM #2Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,638
 Rep Power
 13
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.
 03292012, 03:35 AM #3Member
 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.

Re: 4 digit decimal integer help appreciated very much
 03292012, 08:43 PM #5Member
 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 emailed 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.

Re: 4 digit decimal integer help appreciated very much
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 emailed 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.
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(); // *** add this line 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!"); } } }
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?
 03292012, 11:48 PM #7Member
 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.

Re: 4 digit decimal integer help appreciated very much
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.
 03302012, 12:04 AM #9Member
 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) ?

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.
 03302012, 09:54 PM #11Member
 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!");
}
}
}

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"?
 03312012, 12:46 AM #13Member
 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.

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.
 03312012, 12:58 AM #15Member
 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.

Re: 4 digit decimal integer help appreciated very much
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 set keepAsking to false catch a NumberFormatException warn user that input is bad and that they will need to try again while keepAsking is true
 03312012, 01:22 AM #17Member
 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?

Re: 4 digit decimal integer help appreciated very much
 03312012, 01:41 AM #19Member
 Join Date
 Mar 2012
 Posts
 55
 Rep Power
 0
Similar Threads

Any Help Much Appreciated!
By danielinthebed in forum Advanced JavaReplies: 4Last Post: 01042012, 10:27 AM 
Integer Comparison, Outputting Largest Integer Not Working
By killingthemonkey in forum New To JavaReplies: 4Last Post: 10162011, 09:59 PM 
User Enters 4Digit Integer, Console Returns 1 Digit Per Line
By STANGMMX in forum New To JavaReplies: 0Last Post: 01232011, 01:37 AM 
Decimal to binary, octal to decimal
By matejm1994 in forum New To JavaReplies: 3Last Post: 12262010, 10:59 AM 
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05172010, 01:50 AM
Bookmarks