I am in an intro to Java class and we have to make a numerology program. i have everything except the data validation done. We have to validate that the date put in was put correctly, including the forward slash. I tried using
if(slash1 !< /)
continue;
as it is in a while statement to make the whole thing repeat if something is incorrect. It always tells me that using a forward slash is invalid. Could someone point me in the direction of how to solve this?
It’s a little difficult to help until you post a bit more code and example data – what you have posted isn’t valid Java…
You have
!<, which is not a valid operator in Java, only!=.You also have a plain
/in the code, which is not legal Java – you’d have to quote it"/"or'/'to make it a legalStringorcharliteral.If you want to compare something against a String such as “/”, you need to use
.equals()or.contains()or similar methods. Do not try to compare Strings using==or!=or you will get confusing results.A powerful way to validate string patterns is to use Regular Expressions – see the Java tutorial on this topic.
Another way (for dates) is to define a SimpleDateFormat for your desired pattern.
Hope that helps…