This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Date Program is not honoring month values  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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;
                         
                       }
 
Ranch Hand
Posts: 62
2
  • Mark post as helpful
  • send pies
  • 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.
 
Saloon Keeper
Posts: 2566
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 2566
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator

           
           
Staff note (Ron McLeod):

Fixed your code tags

 
Ron McLeod
Saloon Keeper
Posts: 2566
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 2566
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
So how can I fix that. I tried with Integer.parseInt(theDay) but it doesnt work
 
Ron McLeod
Saloon Keeper
Posts: 2566
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
But what if the user uses 08 and 09. Then the program will fail
 
Ron McLeod
Saloon Keeper
Posts: 2566
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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?
 
Ranch Foreman
Posts: 3243
19
  • Mark post as helpful
  • send pies
  • 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: 2277
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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: 64471
225
  • Mark post as helpful
  • send pies
  • 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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!