Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 48
  1. #21
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    if you want to benchmark, at least make sure you are comparing apples to apples. Otherwise, its all useless. For example, you could compile the pattern first before using the loop. It will only be less than 2-3s difference for a million times loop.
    Done; it'll still be dead slow compared to a simple String traversal (which is inferior an laughable as you say)

    Java Code:
    import java.util.Arrays;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class T {
    
    	private static final int RUNS= 1000000;
    	
    	private static boolean method1(String s) {
    		
    		int letter= 0, digit= 0;
    		
    		for (int i= 0; i < s.length(); i++) {
    			char c= s.charAt(i);
    			if (Character.isLetter(c)) letter++;
    			else if (Character.isDigit(c)) digit++;
    		}
    		
    		return letter != 0 && digit != 0;
    	}
    	
    	private static boolean method2(String s) {
    		
    		return s.matches(".*[0-9]+.*") && s.matches(".*[a-z]+.*");
    	}
    
    	private static Pattern pd= Pattern.compile(".*[0-9]+.*");
    	private static Pattern pn= Pattern.compile(".*[a-z]+.*");
    	
    	private static boolean method3(String s) {
    		
    		Matcher md= pd.matcher(s);
    		Matcher mn= pn.matcher(s);
    		
    		return md.matches() && mn.matches();
    	}
    	
    	public static void main (String[] args) {
    
    		String a= "12345";
    		String b= "abcde";
    		String c= "a1b2c";
    		
    		for (int i= 0; i < RUNS; i++) {
    			method1(a); method1(b); method1(c);
    			method2(a); method2(b); method2(c);
    			method3(a); method3(b); method3(c);
    		}
    		
    		long start= System.currentTimeMillis();
    		for (int i= 0; i < RUNS; i++) {
    			method1(a); method1(b); method1(c);
    		}
    		System.out.println(System.currentTimeMillis()-start);
    
    		start= System.currentTimeMillis();
    		for (int i= 0; i < RUNS; i++) {
    			method2(a); method2(b); method2(c);
    		}
    		System.out.println(System.currentTimeMillis()-start);
    
    		start= System.currentTimeMillis();
    		for (int i= 0; i < RUNS; i++) {
    			method3(a); method3(b); method3(c);
    		}
    		System.out.println(System.currentTimeMillis()-start);
    
    	}
    }
    Jos
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by Singing Boyo View Post
    @JavaHater: you too. Your original code was good, but honestly, I wonder at the validity of using patterns and matchers for this, especially for a new programmer. Personally, I'd assume that newlines would break the pattern, so just using String methods should be fine, but without confirmation, I can't say.
    My solution (or rather suggestion) is an example of what people would do in the real world and is posted here for the OP, whether he wants to learn more about it or not is entire up to him. And then comes the funny thing. First of all, this is a public forum. There will be people from all walks of life who come here and there may be people with real world experience who can provide their insights to the forum. why is there someone there to restrict how many solutions there is that can solve the problem.

    You might say that now, the student doesn't need it, true in a certain extent, but what is there also to forbid me posting a real world method to solve an often seen real world problem and why should anyone deprive the student of learning a new technique? ( except egotistical people who think that only their solution is correct? )


    Using regex for is so common in the real world , there's no reason no to learn about it now. It's certainly better than writing tons of code to do what the regex already can do in a jiffy. Plus, regex is versatile enough if one wants to add additional requirements. Also, if the logic behind the regex needs to be used in another language, its easy to "port" over, unless of course one uses exotic syntax only existing in a certain language.

  3. #23
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    Done; it'll still be dead slow compared to a simple String traversal (which is inferior an laughable as you say)
    did you know that .matches() must match the whole line, that's why initially i need to use .* . But now since we are doing a mini benchmarking here, the pattern compiled could be just "[0-9]" and "[a-zA-Z]", because after all , we are only just checking for one, and then using find() instead of matches(). Plus, there is no reason to declare Matcher variable every time, like what you did here
    Java Code:
    private static boolean method3(String s) {
    		Matcher md= pd.matcher(s);
    		Matcher mn= pn.matcher(s);
    		return md.matches() && mn.matches();
    }
    it could simply be md = pd.matchers(s) ....

    Plus.. there is a reset() matcher method one can use for new input strings, so method3 could look like
    Java Code:
    .... boolean method3(String s){
       md.reset(s);mn.reset(s) ;
       return ....
    }
    I did my test on a *nix machine , separating the code out to do each method and using the unix time command to run them each one by one. The best try is a 2.5s difference.

    Since we are on this topic, i thought of optimizing your method further, since OP needs only to check for mixture of digits or letters, there's no need to increment till the end
    Java Code:
    	int letter= 0, digit= 0; 
    		for (int i= 0; i < s.length(); i++) {
    			char c= s.charAt(i);
    			if (Character.isLetter(c)) letter=1;
    			else if (Character.isDigit(c)) digit=1;
                            if ( letter && digit ){
                                return ....
                           } 
    		}

  4. #24
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    Using regex for is so common in the real world , there's no reason no to learn about it now. It's certainly better than writing tons of code to do what the regex already can do in a jiffy. Plus, regex is versatile enough if one wants to add additional requirements. Also, if the logic behind the regex needs to be used in another language, its easy to "port" over, unless of course one uses exotic syntax only existing in a certain language.
    For the same reason people won't/can't use collections (List, Maps etc.) and use simple arrays because they 'haven't studied collections' yet or their assignment tells them to use arrays; people haven't 'studied regular expressions' yet so your 'real world examples' don't apply. But your stubborn egotistical ego forbids you to understand that so you should go away. b.t.w. regular expressions are heavily overrated.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #25
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    Since we are on this topic, i thought of optimizing your method further, since OP needs only to check for mixture of digits or letters, there's no need to increment till the end
    I didn't even apply those micro optimizations and still my 'laughable' inadequate method is way faster than those regular expressions (they load far less code too).

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #26
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    In this thread he was trying to convince me that his regular expression solution was superior to a simple String traversing loop; see my speed test for a counter proof.
    Jos
    considering the costs (development time) of doing a hand crafted version versus using regex, i take regex anytime. Speed has nothing to with it considering the application domain. Regex is relatively fast enough for a simple password validation. Who's going to run a loop 1 million times for a password validation routine?

  7. #27
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    considering the costs (development time) of doing a hand crafted version versus using regex, i take regex anytime. Speed has nothing to with it considering the application domain. Regex is relatively fast enough for a simple password validation. Who's going to run a loop 1 million times for a password validation routine?
    A big user based system that has to check all passwords, who knows. But it wasn't me who uttered 'inadequate' and 'laughable' hence I implemented the small benchmark class. You are trying to weasle yourself out of the inevitable conclusion that regular expressions are way slower than ordinary code (where applicable of course). I still want you out of here because your are just self righteous and insult me on a regular basis when I say or do or write otherwise (by not using regular expressions e.g.). One of the mods is back now and we'll see what he can do about you.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #28
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    For the same reason people won't/can't use collections (List, Maps etc.) and use simple arrays because they 'haven't studied collections' yet or their assignment tells them to use arrays; people haven't 'studied regular expressions' yet so your 'real world examples' don't apply. But your stubborn egotistical ego forbids you to understand that so you should go away. b.t.w. regular expressions are heavily overrated.

    Jos
    you are acting like a spoilt child considering your age. And read the OP's question carefully. (did you fail your reading comprehension in school? if so, you should go retake it). Did he mention anything that says he can't use this and that ? And so what if he is only allowed to use this and that? What i am posting is another way to solve the problem, which he might use in future. What's wrong ? You should be the one to be banned for being egotistical and acting like only your solution is correct, and others are crap.

  9. #29
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    ). I still want you out of here because your are just self righteous and insult me on a regular basis
    Jos
    I wouldn't have done that if you didn't do it first. Let's see what the mod says about you as well, when i show them proof of what you did.

  10. #30
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    I wouldn't have done that if you didn't do it first. Let's see what the mod says about you as well, when i show them proof of what you did.
    You are not worth discussing with; I'm slow recognizing those types, I know. Let the mods decide what to do; if you're not going to be banned I'm out of here because I'm sick and tired of your kind of low lifes.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #31
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    banned I'm out of here because I'm sick and tired of your kind of low lifes.
    Jos
    this statement is derogatory. I can sue you for this in my country. It will be reported and will be submitted as one of the many evidence of you deliberately starting an argument with me for nothing.

  12. #32
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    this statement is derogatory. I can sue you for this in my country. It will be reported and will be submitted as one of the many evidence of you deliberately starting an argument with me for nothing.
    I my country you'll have a restraining order for stalking me; you keep on following my replies in threads and ruining them. Yes, accept it: you are a low life and deserve to be banned from here because you're ruining the atmosphere and all threads you participate in. Go away.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #33
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    I my country you'll have a restraining order for stalking me; you keep on following my replies in threads and ruining them. Yes, accept it: you are a low life and deserve to be banned from here because you're ruining the atmosphere and all threads you participate in. Go away.

    Jos
    anyone with eyes can see, its always you who started it ALL. All i ever did was post to help the OP. ALL you ever did was to criticise other people, sometimes failing reading comprehension, calling people low life, idiots etc... and acting like the only solution worth it all is definitely yours.

    The solution to ALL THESE CRAP that we are doing right now is actually VERY SIMPLE. You can IGNORE me and my posts, even if you don't like what i do. It will solve all future problems between us.

  14. #34
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    anyone with eyes can see, its always you who started it ALL. All i ever did was post to help the OP. ALL you ever did was to criticise other people, sometimes failing reading comprehension, calling people low life, idiots etc... and acting like the only solution worth it all is definitely yours.

    The solution to ALL THESE CRAP that we are doing right now is actually VERY SIMPLE. You can IGNORE me and my posts, even if you don't like what i do. It will solve all future problems between us.
    A simple ban for you would be much easier; spreading boiler plate code around is not helping anyone at all but you are just too stupid to realize that simple fact. I have the right to say so but you started a 'vendetta' and I am not a person who hides from low lifes such as you. I'll argue against them until they are gone and cause no further harm.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #35
    monic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Hi guys

    Hey thank you for the help !

    I have a question, I downloaded eclipse and I did a small test Program:



    import java.io.BufferedReader;
    import java.io.FileReader;

    public class Master {

    /**
    * @param args
    */
    public static void main(String[] args) throws Exception{
    // TODO Auto-generated method stub
    System.out.println("hallo welt");

    BufferedReader in = new BufferedReader(new FileReader("myFile.txt"));
    String str;
    while ((str = in.readLine()) != null) {
    System.out.println(str);
    }
    in.close();
    }


    }


    My question is, where should I save myFile, so that the Program can find it. I have 3 folders src, bin and settings. I saved the file in all 3 folders, but the program still cant find it. What Im doing wrong?

    Thanks a lot,
    Regards

  16. #36
    monic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    This is the error message that I got:

    hallo welt
    Exception in thread "main" java.io.FileNotFoundException: myFile.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at Master.main(Master.java:14)

  17. #37
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by monic View Post
    Hey thank you for the help !

    I have a question, I downloaded eclipse and I did a small test Program:

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    public class Master {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception{
    		// TODO Auto-generated method stub
    		System.out.println("hallo welt");
    		
    		BufferedReader in = new BufferedReader(new FileReader("myFile.txt"));
    	    String str;
    	    while ((str = in.readLine()) != null) {
    	      System.out.println(str);
    	    }
    	    in.close();
    	  }
    }
    My question is, where should I save myFile, so that the Program can find it. I have 3 folders src, bin and settings. I saved the file in all 3 folders, but the program still cant find it. What Im doing wrong?
    If you don't supply a path name for your file it will be searched for in the 'cwd' (Current Working Directory). You can find that directory value in the System.getProperties() object. The "user.dir" specifies the cwd. Otherwise you have to specify a path to your file, e.g. "c:\\temp\\myfile.txt".

    There is a third option: you can set the cwd in eclipse: when you run your configuration select the 'Arguments' tab and specify where your java program should run (set the cwd to the directory where your file is stored).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #38
    monic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Thank you !

    Hi Jos,

    thank you so much ! I works now. I will divert the output in a StringBuffers as you suggested yesterday and than I will try to implement my algorithms. Thank you a lot !

    Regards

  19. #39
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    A simple ban for you would be much easier;
    there's no smoke without fire. If ever i am banned, you should too

    spreading boiler plate code around is not helping anyone at all
    once again, spreading nonsense. Which post of mine did you see that doesn't help OP at all, even though they are not to your taste?

    but you are just too stupid to realize that simple fact.
    once again, i see abusive words used, calling people stupid. But I will keep my cool. Because the more you do this, the more negative points you get, the higher chance you will get banned.

    I have the right to say so
    And i have the right to post here, like anyone else

    but you started a 'vendetta'
    isn't you the one who has a vendetta. Remember, I am here to help the OP much like anyone else, but you are here to criticise and talk down people.

  20. #40
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by monic View Post
    Hi Jos,

    thank you so much ! I works now. I will divert the output in a StringBuffers as you suggested yesterday and than I will try to implement my algorithms. Thank you a lot !
    You're welcome of course and sorry for all the hacoc in this thread; it'll be over and done with soon (I hope).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Need help with my java homework.
    By nightrise420 in forum New To Java
    Replies: 6
    Last Post: 09-24-2010, 07:23 PM
  2. Help:Design programm in data structure in java
    By bosaod in forum New To Java
    Replies: 1
    Last Post: 05-09-2010, 12:38 PM
  3. Why my java lan game programm slow!
    By arnelpogs in forum Networking
    Replies: 2
    Last Post: 12-07-2009, 04:00 PM
  4. java homework help
    By jenniferrlie in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 08:12 PM
  5. Java homework please
    By Indulgence in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 02:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •