public class Practice { public static void main(String... args){ Pattern p = Pattern.compile("\\d*"); Matcher m = p.matcher("a1"); boolean b = false; while(b = m.find()) System.out.println("Hello"); } }
It prints,
Hello Hello Hello
WHY ??
and another question ...
%b = boolean
%c = ?
%d = digit
%f = float ?
%s = String
* please dont tell me that is because "*" or "?" is greedy because i don't understand what "greedy" means.
* don't tell me go read the javadoc (don't be mean )
Thanks a lot ! [ June 26, 2008: Message edited by: podonga poron ]
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35258
7
posted
0
For starters, you might print out something more useful than "Hello", e.g.
Also, you could use a different regex, namely "\\d+". I suspect that's what you're actually looking for. Are you familiar with the exact meaning of "*", "?" and "+" in regexps?
please dont tell me that is because "*" or "?" is greedy because i don't understand what "greedy" means.
Then you had better take steps to learn what it means, hadn't you? It's not the answer here, though.
don't tell me go read the javadoc (don't be mean)
With Java, the answer very often is in the javadocs, so getting into the habit of consulting them is a very useful trait. In particular, the page about the Pattern class is very extensive, telling you just about everything you need to know about how to use regexps in Java. If it contains concepts you're unfamiliar with, ask about them. Not reading the javadocs is not a feasible approach to Java programming.
podonga poron, if you're studying for the SCJP, I assume that means you're preparing for a career in Java programming. A really important part of being a good programmer is learning. There are always new APIs, libraries, tools, and languages to learn. You have to enjoy learning those things, or you're going to be very unhappy with your career choice. My advice to you is to listen to these smart guys; they're not just trying to help you with the answer, they're trying to help you learn how to find the answers on your own. That's an incredibly valuable skill and one you'll absolutely need as your career begins. Take the time to do the background reading, explore all the resources available to you, and learn. You will be glad you did.
Originally posted by Ernest Friedman-Hill: That's an incredibly valuable skill and one you'll absolutely need as your career begins.
Absolutely, and by being able to see the value returned in each case you will (hopefully) be given the tools to answer the question yourself. You could even post the results here and explain it to us, since I certainly have no idea what the answer is
That's an incredibly valuable skill and one you'll absolutely need as your career begins.
Another point to make here is that these solutions are incredibly easy to what you will be facing as a professional programmer. Yes, it is true, it is easier to have an answer spoon fed to you than to look up the answer in the JavaDoc -- but keep in mind, you *can* look up the answer, and in one source.
When you are a professional programmer, your problem will be so specific that it is highly unlikely that you find a source for your answer directly. And the answer won't be in a JavaDoc -- it will be buried deep in tomes of books that you have to get up to speed on. If you feel that it is mean to send you to an easy organized source -- how are you going to do your job as a professional programmer?
IOWs, we are not being mean. The JavaDoc is easy. "researching" is a skill that you need to master. If we spoon fed you, so you never learned to correctly look up stuff, now, that would be mean.
Henry [ June 26, 2008: Message edited by: Henry Wong ]
Just curious, is anything similar to this question likely to be on the SCJP 1.5 edition? I've been studying for a while and haven't seen anything like this... How familiar with regexp should I be?