• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date Program is not honoring month values

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi can you help me please?
I've been doing a Date program which is pretty much done except that when I introduce the month or the day with a zero in front, for example 05 it gives me the error "int number too large". What can I do about it and also if you have a better way to validate the year because I only have if theYear !=0  and that doesnt seem very good to me. Thank you!!



public void Date(  int theYear, int theMonth, int theDay)
                   {
                       // initialise instance variables
                       if (theYear !=0  && theMonth >0 && theMonth<13 && theDay !=0 && theDay>365){
                            throw new IllegalArgumentException("Please enter a valid date.");
                       } else{
                          year =  theYear;
                          day =   theDay;
                          month = theMonth;
                         
                       }
 
Bartender
Posts: 206
14
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zero in front means octal number and that's probably why it says that the number is too large, but don't worry about that for now. Where do you get your input from? Integer.parseInt() will take care of the leading zero for you and parse it correctly, I guess you just hardcoded the number.
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sandra baez wrote:when I introduce the month or the day with a zero in front, for example 05 it gives me the error "int number too large"

Adrian Grabowski wrote:Zero in front means octal number and that's probably why it says that the number is too large


05 in octal is equal to 5 in decimal, so I don't think that it related to the problem.
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sandra baez wrote:when I introduce the month or the day with a zero in front, for example 05 it gives me the error "int number too large"


Can you post a runnable example of code which gives this error?
 
sandra baez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. Thanks for your response. So do I put Integer.parseInt(theDay)? if I do that it says incompatible types. Sorry I'm new coding
 
sandra baez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

           
           
Staff note (Ron McLeod) :

Fixed your code tags

 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your code using a month of 05 and I didn't see any errors:
However, you do have a problem with your constructor: public void Date(  int theYear, int theMonth, int theDay)

You should not be specifying a return type - should be like this: public Date(int theYear, int theMonth, int theDay)
 
sandra baez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Im sorry I said it gives an error with 05. I checked and its after 8, for example 08, 09.
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sandra baez wrote:Hi Im sorry I said it gives an error with 05. I checked and its after 8, for example 08, 09.


Ok - that's different.  Only digits 0 through 7 are allowed in an octal number (like 0 through 1 for a binary number, or 0 through 9 for a decimal number).

If you specify 08, you should get a compile-time error, something like: The literal 08 of type int is out of range
 
sandra baez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how can I fix that. I tried with Integer.parseInt(theDay) but it doesnt work
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fix is to use values 8 and 9 rather than 08 and 09, since 08 and 09 and not valid numbers and cause a compile-time error.
 
sandra baez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what if the user uses 08 and 09. Then the program will fail
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sandra baez wrote:But what if the user uses 08 and 09. Then the program will fail


Can you show a code example of how the user would provide/enter the date information?
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to distinguish between how you test your program as you're writing it, by putting code in a java class, and how the user will interact with it, which is typically different - typing in a text area somewhere, or writing a text file, or typing in a terminal in response to a question.  The rule about a leading 0 implying octal numbers, and therefore you can't have 08 or 09... that rule is only for you, the programmer, while you're writing a Java class.  That's a rule for Java program formatting.  It doesn't apply to stuff users type as input to your program.

So, don't try to write 08 or 09 for a number in a Java program.  That will never work.  But there are ways to interpret user input, and that user input does't need to obey Java rules.

For more info, Ron's last question is key: how are you getting info from the user?
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sandra baez, your post topic:  "Help please!" does not tell much about the issue at hand, I have gone ahead and modified it to better suit your requirements.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:I tried your code using a month of 05 and I didn't see any errors . . .

What happened when you used 08 for August, or a month later in the year?
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic