Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Way to find a correct date.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been trying to figure out what is wrong with this code for quite some time. The point is to input a Date string lets say "5/10/2020". The code lets everything through the method that is 1/1, 1/12, 10/1, 10/11. But for instance, typing in a 0 before a day or month like 09, 012, 02 should throw a run time exception error. I got most of it to work except for one date which is "1/10" or having a one digit month and the day as 10, 20, or 30.

My code is here,



should I take,


and replace that with

I have tried that and it will not seem to work.
 
Sheriff
Posts: 16657
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're over complicating this with matches(). I would just use String.startsWith() -- simple and straightforward.
 
Steven Villarreal
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I visited the Java API and understood the startsWith method which is exactly what I needed. I just made two ifs, one with that method

and the other with the startsWith method and with

which looked like

Thanks!
 
Junilu Lacar
Sheriff
Posts: 16657
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could have also used String.split() to make your code more readable.


Sure, it's a few lines longer that what you wrote but nobody has to spend a lot of time deciphering an expression like geoDate.substring(3, 4) (yuck) .
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Villarreal wrote:The point is to input a Date string lets say "5/10/2020". The code lets everything through the method that is 1/1, 1/12, 10/1, 10/11. But for instance, typing in a 0 before a day or month like 09, 012, 02 should throw a run time exception error.


Why? Seems perfectly reasonable to me (well, maybe not "012"), and the way many dates are actually written.

However, my advice would be to forget "rolling your own", and use the parser provided by SimpleDateFormat - ie, parse(String).

You should read up on it carefully first because there's quite a lot to know; but it's a far more generic solution and you don't have to write a single line of code.

The class even allows you to set its "strictness" (see setLenient(boolean)).

Winston
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic