Page 1 of 3 123 LastLast
Results 1 to 20 of 48
  1. #1
    monic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Need a little help for a homework programm in Java

    Hi to all,

    I need your help to solve my homework. I have to write a programm, which implements a given search algotithm, but the seach should be faster then 2 secounds.

    In the programm description is said: the programm should "reads the complete test fi le (there are efficient and non-efficient ways for it!)".

    Well my question is, what is the efficient way to read the whole file? And where should I save it? May be in a string array!?

    Than I have to search in it for a given patern according to a given algorithm. I stuck with the first part, how to read a file in an efficient (fast) way and where to storage the information.

    Does anybody have an idea how to solve this problem?

    Thank you in advance !

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

    Default

    Reading the (text) file line by line (read the API for the BufferedReader class or the Scanner class) and storing each line in an ArrayList<String> is good enough for now. There is no need to jump trough hoops trying to do it a faster (but less clear) way.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Hey JosAH,

    thank you for your fast responce, well in case that the pattern, which I`m searching for stands on two different lines, I will be not able to find it. For example if the pattern we search is "abc" and the file we search in, looks like this:

    xxxxxa
    bcxxxx

    we would not be able to find it, and the programm would say that the pattern "abc" does not apperar. I hope you can catch, what Im trying to say. :)

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

    Default

    Thany you very much.

    best regards,
    Monic

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

    Default

    Quote Originally Posted by monic View Post
    Hey JosAH,

    thank you for your fast responce, well in case that the pattern, which I`m searching for stands on two different lines, I will be not able to find it. For example if the pattern we search is "abc" and the file we search in, looks like this:

    xxxxxa
    bcxxxx

    we would not be able to find it, and the programm would say that the pattern "abc" does not apperar. I hope you can catch, what Im trying to say. :)
    In your example, strictly speaking the pattern "abc" doesn't exist; the pattern "a<new line character(s)>bc" does exist. If you want to ignore the new line characters you can glue all lines together (in one StringBuffer e.g.) and play with that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Thank you! Than I will read the file line by line and save the characters in a string array, or should I use StringBuffer?

    best regards,
    Monic

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

    Default

    Quote Originally Posted by monic View Post
    Thank you! Than I will read the file line by line and save the characters in a string array, or should I use StringBuffer?

    best regards,
    Monic
    String arrays have a fixed length, StringBuffers (or StringBuilders) can grow on demand. All you have to do is append( ... ) each line to it. (Read the API documentation for that class). But before you glue all those lines together you have to be sure that you don't need those end-of-line characters.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Quote Originally Posted by monic View Post
    xxxxxa
    bcxxxx

    we would not be able to find it, and the programm would say that the pattern "abc" does not apperar. I hope you can catch, what Im trying to say. :)
    here's an example, you can use the Scanner class to read in the whole file ( if its small) and a regular expression to find your string
    Java Code:
             Scanner sc = new Scanner ( new File("file") );
             sc.useDelimiter("\\Z");
             String data = sc.next(); //get data
             sc.close() //close scanner
             Pattern ex = Pattern.compile("a\n*b\n*c"); // search for abc, with 0 or more newlines in between.
             Matcher match = ex.matcher( data);
             while ( match.find() ){
                   System.out.println( match.group() );
             }
    for more information on regex. Read the documentation. Or documentation on the Pattern /Matcher class.

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

    Default

    Quote Originally Posted by JavaHater View Post
    Pattern ex = Pattern.compile("a\n*b\n*c"); // [/code]
    I want to see that work on a MS Windows machine; skip those crappy examples; they're of no use to a starting programmer.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    I want to see that work on a MS Windows machine;
    firstly, there was no indication of what OS the OP is working on.
    secondly, I was on *nix so when i tested it, its "\n".
    thirdly, If i want to make it cross platform, i can always add \r* to it.

    skip those crappy examples; they're of no use to a starting programmer.
    Jos
    wrong. Its a valid example (at least for *nix) and it solves the issue at hand. I don't see you providing your example. So what right have you to say they are "crappy" ?
    I presume you will be reach 60 soon? why are you still acting so childish?

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

    Default

    Quote Originally Posted by JavaHater View Post
    firstly, there was no indication of what OS the OP is working on.
    secondly, I was on *nix so when i tested it, its "\n".
    thirdly, If i want to make it cross platform, i can always add \r* to it.


    wrong. Its a valid example (at least for *nix) and it solves the issue at hand. I don't see you providing your example. So what right have you to say they are "crappy" ?
    I presume you will be reach 60 soon? why are you still acting so childish?
    How cute: Java attempts to work in a platform indepent way but you introduce platform dependencies again and offer it as a solution for the OP, as an afterthought you're trying to correct your mistake again but there is no write once, run anywhere concept anymore. Childish? No, I am warning OPs for your stupid boilerplate code; it won't make it in the industry, it is just adolescent hacking with the wrong ego put in it. I do hope the admins, or moderators, ban you this Monday; I, for one, have had enough of your obnoxious behaviour. b.t.w. I'm 54 years old if you really want to know.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    How cute: Java attempts to work in a platform indepent way but you introduce platform dependencies again and offer it as a solution for the OP, as an afterthought you're trying to correct your mistake again but there is no write once, run anywhere concept anymore.
    Old man, I do know that languages like Java , Python etc were meant to work in a platform way. That's not my intention anyway when i presented my solution. I am merely showing OP basic regex patterns and how they can be crafted. Note, some language doesn't know about platform dependencies, so sometimes, there is a need to craft such patterns that works in any language that support basic regex. Got that.?

    Childish? No, I am warning OPs for your stupid boilerplate code; it won't make it in the industry, it is just adolescent hacking with the wrong ego put in it.
    Jos
    if you want to talk about crappy code, I know of one that you made earlier, remember the one on character by character string counting? crap, I would laugh my ass off if those who learn it from you here ever were to use it in the real world using Java. No wonder someone said programmers are now shoddy in their programming. I guess they all learn from you here.

    I do hope the admins, or moderators, ban you this Monday; I, for one, have had enough of your obnoxious behaviour. b.t.w. I'm 54 years old if you really want to know.
    the feeling is mutual. you are too egotistic, one who thinks he is always right, when in fact, he is not.

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

    Default

    Quote Originally Posted by JavaHater View Post
    if you want to talk about crappy code, I know of one that you made earlier, remember the one on character by character string counting? crap, I would laugh my ass off if those who learn it from you here ever were to use it in the real world using Java. No wonder someone said programmers are now shoddy in their programming. I guess they all learn from you here.
    The main purpose of this forum is that people learn from it, not to show off your ego. It can still be debated whether or not traversing a String once is good or bad code but I most certainly don't want to do that with you; I want you banned because you've ruined enough threads, shown enough of your stubbornness and bad character. You don't belong here and I have asked (more than once) for your ban and I continue doing so.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    The main purpose of this forum is that people learn from it,
    you are correct. I am merely showing people what some other ways they can do things with Java, not some ways that for example, follow C programming or being too archaic/old/prehistoric/draconian you name it. Ego or not, has nothing to do with it. Get that into your thick head.

    It can still be debated whether or not traversing a String once is good or bad code but I most certainly don't want to do that with you;
    with no benchmarks, nothing is absolute.

    I want you banned because you've ruined enough threads, shown enough of your stubbornness and bad character. You don't belong here and I have asked (more than once) for your ban and I continue doing so.
    Jos
    Oh yes, I have not reported you for the bad words you said about me. I guess I will do so as well, since you started the ball rolling. I hope you get banned as well.

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

    Default

    Quote Originally Posted by JavaHater View Post
    with no benchmarks, nothing is absolute.
    Ok, here you go: see for yourself (no debate about it anymore):

    Java Code:
    import java.util.Arrays;
    
    
    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]+.*");
    	}
    
    	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);
    		}
    		
    		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);
    	}
    }
    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Oh dear god, sometimes I wonder...

    @JosAH: calm down, please. You're no longer helping the OP.

    @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.

    OP, could you post the exact requirements of what your pattern would match? I assume it would come with examples, and if we could get a couple, that would be great. Until we know what the actual requirements are, there are too many possibilities, because of things like this:

    Find: abc

    abc //definitely a match

    a
    bc //is this?

    a bc //probably not, but could be

    a
    b
    c //hmm...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Abuse reported on user JavaHater.

    db

  18. #18
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Abuse reported on user JavaHater.

    db
    I didn't expect someone ELSE to get in on it. Can't we just leave well enough alone? He may be annoying sometimes, but his advice IS good if used appropriately, as with any of us.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

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

    Default

    Quote Originally Posted by JosAH View Post
    no debate about it anymore
    says who?


    What's there to benchmark if you are not doing it properly? first of all, method2 is not optimized to be run like that in a loop. Its is only written to solve a simple real world problem ( password validation), and not for benchmarking. Secondly, 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.

    Now, we ask ourselves. is this important? Certainly not. In the real world, nobody is going to run a loop a million times in a password validation routine ! The cost savings of using regex rather than hand crafting my own password validation method from scratch far outweigh that 2-3s difference in performance.

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

    Default

    Quote Originally Posted by Singing Boyo View Post
    I didn't expect someone ELSE to get in on it. Can't we just leave well enough alone? He may be annoying sometimes, but his advice IS good if used appropriately, as with any of us.
    Boilerplate code is considered 'not done' in this forum. There is a philosophy here that spoonfeeding doesn't help; I agree with that. JavaHater is just a 'look how clever I am' type of youngster with a no good character: he's rude, impolite and no good for this forum. I know I should've kept my mouth shut but JavaHater irritates me to the bone and should be banned asap.

    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. Regular expressions are no good for beginners: they don't understand them (yet) and these REs are dead slow.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 3 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, 08: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, 01:38 PM
  3. Why my java lan game programm slow!
    By arnelpogs in forum Networking
    Replies: 2
    Last Post: 12-07-2009, 05:00 PM
  4. java homework help
    By jenniferrlie in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 09:12 PM
  5. Java homework please
    By Indulgence in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 03: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
  •