Results 1 to 19 of 19
  1. #1
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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!");

    }
    }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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.

  3. #3
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: 4 digit decimal integer help appreciated very much

    Quote Originally Posted by JMAsterson View Post
    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?

  5. #5
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: 4 digit decimal integer help appreciated very much

    Quote Originally Posted by JMAsterson View Post
    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();
    
          // *** 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!");
          }
       }
    }
    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?

  7. #7
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: 4 digit decimal integer help appreciated very much

    Quote Originally Posted by JMAsterson View Post
    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.

  9. #9
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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) ?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  11. #11
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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!");
    }
    }
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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"?

  13. #13
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  15. #15
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: 4 digit decimal integer help appreciated very much

    Quote Originally Posted by JMAsterson View Post
    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
          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

  17. #17
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: 4 digit decimal integer help appreciated very much

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

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: 4 digit decimal integer help appreciated very much

    Quote Originally Posted by JMAsterson View Post
    Ok, where in my program would i fit this portion (your example) in?
    That's for you to test, experiment and discover.

  19. #19
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: 4 digit decimal integer help appreciated very much

    Ok thanks for everything.

Similar Threads

  1. Any Help Much Appreciated!
    By danielinthebed in forum Advanced Java
    Replies: 4
    Last Post: 01-04-2012, 10:27 AM
  2. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 09:59 PM
  3. Replies: 0
    Last Post: 01-23-2011, 01:37 AM
  4. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 10:59 AM
  5. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 01:50 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •