• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dealing with wildcard user entry which is coming in as a String.  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to code a simple program in which the user can pass dates for four different quarters:

3/31, 6/30, 9/30 and 12/31.

This data will be stored in a String variable. However, the user can enter any year.

In other words, the user can request data for any year. For example: 3/31/16 or 3/31/15 or 3/31/88.

In my program, there are only two flows. If the user requests data for 3/31 and 6/30, it will be one flow.
If the user requests data for 9/30 or 12/31, then it will be another flow.

The problem I'm facing is dealing with the fact that the year field can always change, so I can't hardcode this, or use a "switch" statement. How to deal with this wildcard situation?
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have come up with something like this, but I'm not sure if this is correct:



requestedDate is the date value which will arrive as a String and I want to have two different flows depending on whether if its 6/30 and 12/31
or the other scenario in which it's a 3/31 and 9/30 date.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37474
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would work. There's two ways to make it more efficient.

1) Use || instead of |
| operates both conditions even if the first one is true. This is wasted work for the JVM. || is more common for this reason

2) Use starts with
Regular expressions are slower than other operations. This isn't a problem if you want the power of a regular expression. Here, you have something simple though so it isn't necessary.

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:That would work. There's two ways to make it more efficient.

1) Use || instead of |
| operates both conditions even if the first one is true. This is wasted work for the JVM. || is more common for this reason

2) Use starts with
Regular expressions are slower than other operations. This isn't a problem if you want the power of a regular expression. Here, you have something simple though so it isn't necessary.



Thank you, Jeanne. That was great advice. However, I am now running into this unforseen situation:



I am using JXLS, so my intention with the Hashmap is to point "dataset" to the ${dataset} parameter in my XLS template. However, it turns out that the only dataset which is being recognized by my Hashmap is the one outside of the if-statement, in the "else" portion. (naturally) - How do I work around this?

I have pasted a screen shot of my IDE for better readability:  http://imgur.com/wsTADfZ

Thank you!
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String#startsWith() takes a String as its argument, not a regex, so you can just write
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!