Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regular expression problems

 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
having trouble starting a regular expression or telling the JVM to recognize the code as a regular expression and not a variable.

here are the errors I am getting:



C:\Documents and Settings\ben\Desktop\park>javac BenHultinDateDriver.java
.\Date.java:54: illegal start of expression
if (monthStr == [a-zA-Z]) {
^
.\Date.java:54: not a statement
if (monthStr == [a-zA-Z]) {
^
.\Date.java:54: ';' expected
if (monthStr == [a-zA-Z]) {
^
.\Date.java:73: illegal start of expression
if (dayStr == [a-zA-Z]) {
^
.\Date.java:73: not a statement
if (dayStr == [a-zA-Z]) {
^
.\Date.java:73: ';' expected
if (dayStr == [a-zA-Z]) {
^
.\Date.java:54: cannot find symbol
symbol : variable a
location: class Date
if (monthStr == [a-zA-Z]) {
^
.\Date.java:54: cannot find symbol
symbol : variable zA
location: class Date
if (monthStr == [a-zA-Z]) {
^
.\Date.java:54: cannot find symbol
symbol : variable Z
location: class Date
if (monthStr == [a-zA-Z]) {
^
.\Date.java:73: cannot find symbol
symbol : variable a
location: class Date
if (dayStr == [a-zA-Z]) {
^
.\Date.java:73: cannot find symbol
symbol : variable zA
location: class Date
if (dayStr == [a-zA-Z]) {
^
.\Date.java:73: cannot find symbol
symbol : variable Z
location: class Date
if (dayStr == [a-zA-Z]) {
^
12 errors

C:\Documents and Settings\ben\Desktop\park>



Here is my code:



There is more but I felt this is mainly the issue.

Thanks a lot for your help
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The culprit lies in this line:

String should be compared with the string.
 
Vinod Tiwari
Ranch Hand
Posts: 466
1
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this for regular expression in string.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also seem to believe that regular expressions get special syntax in Java (like in Ruby etc.), but you are incorrect; they do not.

Any search for java +regular expressions would have answered this basic syntactic question in about 30 seconds, no?
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help...

"String should be compared with the string."

What do you mean by this?

saying I should compare monthStr with monthStr?

or

saying I should compare monthStr with the value being held by monthStr?

first one, not checking for a match checking if any letters exist in the string variable

second one, I dont know the value of monthStr its a user input.


I have been doing internet searches before and after the post as was mentioned to solve the problem. My problem is I am not sure which package to import for my specific problem and I am not totally sure about how matcher and matches works. My best idea of what I read is:



Mind filling me in on what I am missing?

Thanks a lot
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A typical usage:

As previously noted first argument should be of type String.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response,

mind explaining how that could help me in checking monthStr for any letters contained in it? I just dont understand what its doing. I have read the articles online as well and I just need some clarification on the topic so I can get it better.

Thanks a lot for the help
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just change that one line into the method you and Vijitha mentioned:
However, that check isn't what you want. You don't want to check if monthStr contains a letter; if it doesn't it can still contain whitespace, punctuation marks, basically anything but letters. What you want is to really check on not being digits:
This will execute the if-statement body if monthStr is anything but exactly one character. Check out how to change that regular expression to allow for one or more matches. Then you get what you want: if monthStr is not one or more digits (a.k.a. not a number) then execute the body.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic