Results 1 to 20 of 48
- 01-08-2011, 11:41 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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 file (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 !
- 01-08-2011, 11:45 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2011, 11:56 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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. :)
- 01-08-2011, 11:58 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Thany you very much.
best regards,
Monic
- 01-08-2011, 12:07 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2011, 12:16 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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
- 01-08-2011, 12:22 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2011, 12:46 PM #8
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
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
for more information on regex. Read the documentation. Or documentation on the Pattern /Matcher class.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() ); }
- 01-08-2011, 02:57 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 01-08-2011, 03:28 PM #10
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
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" ?skip those crappy examples; they're of no use to a starting programmer.
Jos
I presume you will be reach 60 soon? why are you still acting so childish?
- 01-08-2011, 03:55 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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.
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2011, 04:13 PM #12
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
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.?
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.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
the feeling is mutual. you are too egotistic, one who thinks he is always right, when in fact, he is not.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.
- 01-08-2011, 04:20 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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.
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2011, 04:25 PM #14
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
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.
with no benchmarks, nothing is absolute.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;
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.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
- 01-08-2011, 04:44 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Ok, here you go: see for yourself (no debate about it anymore):
JosJava 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); } }When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2011, 06:40 PM #16
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 01-09-2011, 06:32 AM #17
Abuse reported on user JavaHater.
db
- 01-09-2011, 06:35 AM #18
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 01-09-2011, 07:27 AM #19
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
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.
- 01-09-2011, 07:33 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Need help with my java homework.
By nightrise420 in forum New To JavaReplies: 6Last Post: 09-24-2010, 07:23 PM -
Help:Design programm in data structure in java
By bosaod in forum New To JavaReplies: 1Last Post: 05-09-2010, 12:38 PM -
Why my java lan game programm slow!
By arnelpogs in forum NetworkingReplies: 2Last Post: 12-07-2009, 04:00 PM -
java homework help
By jenniferrlie in forum New To JavaReplies: 5Last Post: 09-22-2009, 08:12 PM -
Java homework please
By Indulgence in forum New To JavaReplies: 1Last Post: 11-03-2008, 02:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks