The Big Programming Thread - Page 203
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Tobberoth
Sweden6375 Posts
| ||
sAsThark
France27 Posts
But as I said, you can also create your own class which will implements Iterable. This class must override iterator() a method which return an home made Iterator (implementing iterator) and override the following method : hadNext(), Next(), remove(). Then you should be able to use a for(Stuff s : MyBadAssList). Clearly it's a waste of time to code this for a simple application, you could be more productive using the API. BTW it's a good way to understand the mechanics of java and a good academic implementation of abstract list ... | ||
frogmelter
United States971 Posts
On November 27 2012 05:08 AmericanUmlaut wrote: It's less a question of convention than a question of readability. If you see for(i=0; i<10; i++){...}, you instantly know what's intended without having to give it a lot of thought, whereas it takes a bit more thinking to parse a while loop that does the same thing (you have to go through the body of the loop and see everything that can happen to your i variable). While loops remain useful, though, because there are a lot of times when you don't have a number of loops you want to do, but rather you just want to loop until you encounter some stop condition, for example looping through a string and doing something with each character: while(null !== curChar) { curChar = myString.getNextChar(); doSomething(curChar); } You could technically do the same thing with a for loop, but it would be really unreadable. In fact, in terms of making a program work for and while loops could both be replaced by GOTO, which is what they devolve to at the machine code level in any case. We have these control structures not because they work better logically, but because they make code a lot more intuitive to read and write for humans. One of the most important concepts in programming as far as I'm concerned is understanding that writing code that a computer can understand (what 99% of people think we do for a living) is only the first part of the job, and is often relatively easy*. When you're good, you give as much consideration to making sure that you're writing code that a person can understand. *Except for pointers and the Javascript SelectionRange object. Edit to add: Another point is that for() gives you a really simple syntax for something that you do over and over and over again as a programmer. A while loop requires several lines of code to do the same thing, which multiplies your opportunites for typos, forgetting to initialize or increment your counter, and so on. One of the reasons that "elegance" is held holy by computer programmers is that if you can figure out a way to solve a problem with 3 lines of code rather than 20, then even if your 3 lines are no faster and no more logically correct than my 20, your code has only 3 things that can be wrong, and mine has 20. At the same time, if a bug in your code does pop up, the guy who gets the job of fixing it only has to understand 3 lines of code, so he'll probably be able to instantly or very quickly grasp what's being done, which might take significantly more time in a function that's much longer. What about for (int i = 0; i < myString.length(); ++i) { //do something } or for a c string char c; for (int i = 0; c != '\0'; ++i) { c = myString[i]; //do something } for looping through a string? I would argue that the first one for a C++ string is just as readable as the while loop that you wrote, if not more. | ||
Kambing
United States1176 Posts
For-loops are used for definite loops, those that have known or "obvious" boundary conditions, e.g., array length, length of an iterator. While-loops are used for indefinite loops, those whose boundary conditions are not known, e.g., terminating when a user enters 'yes'. Also, in many programming languages, this implies that using a for-loop is usually a bad idea. This is because there are usually better higher-level constructs for expressing iteration with known bounds than a for-loop, e.g., Java foreach or Python list comprehension. | ||
omarsito
22 Posts
Heres the code if anybody has the time to assist me ! http://justpaste.it/1kbl | ||
RoyGBiv_13
United States1275 Posts
Initial Symptom: A customer of ours recently upgraded the kernel of our proprietary OS, and were finding that occasionally the debugger was unable to flash the kernel onto the mirochip, and the whole thing crashed. We schedule a web meeting to see this happen in action, and sure enough, the kernel loads up fine with no issues. We retry it, hoping to reproduce the issue, and again, the board works just fine. They start closing every process they can, but still, no luck. Hmm, we check the board initialization script (to boot up the board before you flash the software onto it), and it is the same as the one we sent them and have tested extensively. Maybe it's flakey hardware? We have the customer try with a different board. Same result, it is working fine. We get off the phone, and close out the web meeting. This one is weird. Two minutes later, a phone call from them informs us that it's flaking out again, and to quickly join the meeting. We join up, and the next try works fine "That's strange, the only thing to change has been opening up the GoTo meeting...", so while on the phone with them, we disconnect the screen viewer, and sure enough, the board fails to boot up. In disbelief, we retry the experiment a few times. Literally, in this case, the problem is solved by being on the phone with someone from tech support. We jokingly quote them a rate to always have a tech support guy on the line, but certainly, these two completely unconnected pieces of software have no impact on each other couldn't cause the behavior of the system to change. Right? We now have at least one piece of evidence on this heisenbug, and the ability to branch from there. Do other programs cause this? Nope. Does this same heisenbug exist on other hosts (computers launching the debugger)? Can't test. Hmm, we take a step back. Why is the kernel failing to flash in the first place? There seems to be some registers on the board uninitialized. Checking back in the boot up script, we notice these registers are at the end of the script. There doesn't seem to be any reason for them not the be initialized. It feels like we're getting closer here. We use breakpoint debugging within the startup script to isolate the lines causing the issue. + Show Spoiler + If this script has been running for 10 seconds then shutdown the board, and stop the script Ahh, a watchdog in the script to fail if the script is going on for too long. We check with the developers who wrote the debugger, and sure enough, the wait(n milliseconds) function uses the system timer to wait in a spin loop. + Show Spoiler + The screen share was causing the system timer to slow down just enough so that the microchips initialization would finish before the host computer counted to 10. Without the GoTo meeting, the system counts just a wee bit quicker, and the startup script would exit before completing. We increased the time to 25 seconds and everything worked fine tl;dr - Heisenbug only reproduces when off the phone with tech support. | ||
Shield
Bulgaria4824 Posts
in a class implementing Runnable (method run()):
However, I'm trying to send output from a method in another class. E.g.
I hope it's clear. Sorry for not releasing full code, but university is very strict with plagiarism, so I don't want them to find my code here. I guess scope isn't big enough, but I have no idea how to expand it either. An idea is to pass a reference or something like that, but all my attemps are unsuccessful. | ||
JeanLuc
Canada377 Posts
On November 27 2012 08:33 RoyGBiv_13 wrote: --Debugging story time-- Initial Symptom: A customer of ours recently upgraded the kernel of our proprietary OS, and were finding that occasionally the debugger was unable to flash the kernel onto the mirochip, and the whole thing crashed. We schedule a web meeting to see this happen in action, and sure enough, the kernel loads up fine with no issues. We retry it, hoping to reproduce the issue, and again, the board works just fine. They start closing every process they can, but still, no luck. Hmm, we check the board initialization script (to boot up the board before you flash the software onto it), and it is the same as the one we sent them and have tested extensively. Maybe it's flakey hardware? We have the customer try with a different board. Same result, it is working fine. We get off the phone, and close out the web meeting. This one is weird. Two minutes later, a phone call from them informs us that it's flaking out again, and to quickly join the meeting. We join up, and the next try works fine "That's strange, the only thing to change has been opening up the GoTo meeting...", so while on the phone with them, we disconnect the screen viewer, and sure enough, the board fails to boot up. In disbelief, we retry the experiment a few times. Literally, in this case, the problem is solved by being on the phone with someone from tech support. We jokingly quote them a rate to always have a tech support guy on the line, but certainly, these two completely unconnected pieces of software have no impact on each other couldn't cause the behavior of the system to change. Right? We now have at least one piece of evidence on this heisenbug, and the ability to branch from there. Do other programs cause this? Nope. Does this same heisenbug exist on other hosts (computers launching the debugger)? Can't test. Hmm, we take a step back. Why is the kernel failing to flash in the first place? There seems to be some registers on the board uninitialized. Checking back in the boot up script, we notice these registers are at the end of the script. There doesn't seem to be any reason for them not the be initialized. It feels like we're getting closer here. We use breakpoint debugging within the startup script to isolate the lines causing the issue. + Show Spoiler + If this script has been running for 10 seconds then shutdown the board, and stop the script Ahh, a watchdog in the script to fail if the script is going on for too long. We check with the developers who wrote the debugger, and sure enough, the wait(n milliseconds) function uses the system timer to wait in a spin loop. + Show Spoiler + The screen share was causing the system timer to slow down just enough so that the microchips initialization would finish before the host computer counted to 10. Without the GoTo meeting, the system counts just a wee bit quicker, and the startup script would exit before completing. We increased the time to 25 seconds and everything worked fine tl;dr - Heisenbug only reproduces when off the phone with tech support. I will henceforth refer to all elusive bugs as heisenbug | ||
DeltaX
United States287 Posts
On November 27 2012 07:52 omarsito wrote: I finished a program that recently reads bytes from a file and outputs them in another file (aka copy) But when i run the program im getting a error of the kind java.lang.NullPointerException which points at a code line where i close my outputstream. I was wondering if anybody could quickly check my code because i cant figure the out problem myself, i derived the code from a tutorial but i havent been able to fix it. Heres the code if anybody has the time to assist me ! http://justpaste.it/1kbl Are you sure that your program is actually finding the first file you want to read from? I ran it and got the same null pointer exception with "Storlek på Fil: 0" which would indicate that it can't find the file which my computer clearly does not contain. Also you can get rid of the second "Meny" class if you want and put that all in your read and write class | ||
omarsito
22 Posts
On November 27 2012 09:19 DeltaX wrote: Are you sure that your program is actually finding the first file you want to read from? I ran it and got the same null pointer exception with "Storlek på Fil: 0" which would indicate that it can't find the file which my computer clearly does not contain. Also you can get rid of the second "Meny" class if you want and put that all in your read and write class Yeah im sure the first method is correct because im getting the correct amount of bytes when it prints out, EDIT: I got it working by changing the directory of the output file, it seems my computer doesnt like modifying/putting files in only C: | ||
RoyGBiv_13
United States1275 Posts
On November 27 2012 09:17 JeanLuc wrote: + Show Spoiler + On November 27 2012 08:33 RoyGBiv_13 wrote: --Debugging story time-- Initial Symptom: A customer of ours recently upgraded the kernel of our proprietary OS, and were finding that occasionally the debugger was unable to flash the kernel onto the mirochip, and the whole thing crashed. We schedule a web meeting to see this happen in action, and sure enough, the kernel loads up fine with no issues. We retry it, hoping to reproduce the issue, and again, the board works just fine. They start closing every process they can, but still, no luck. Hmm, we check the board initialization script (to boot up the board before you flash the software onto it), and it is the same as the one we sent them and have tested extensively. Maybe it's flakey hardware? We have the customer try with a different board. Same result, it is working fine. We get off the phone, and close out the web meeting. This one is weird. Two minutes later, a phone call from them informs us that it's flaking out again, and to quickly join the meeting. We join up, and the next try works fine "That's strange, the only thing to change has been opening up the GoTo meeting...", so while on the phone with them, we disconnect the screen viewer, and sure enough, the board fails to boot up. In disbelief, we retry the experiment a few times. Literally, in this case, the problem is solved by being on the phone with someone from tech support. We jokingly quote them a rate to always have a tech support guy on the line, but certainly, these two completely unconnected pieces of software have no impact on each other couldn't cause the behavior of the system to change. Right? We now have at least one piece of evidence on this heisenbug, and the ability to branch from there. Do other programs cause this? Nope. Does this same heisenbug exist on other hosts (computers launching the debugger)? Can't test. Hmm, we take a step back. Why is the kernel failing to flash in the first place? There seems to be some registers on the board uninitialized. Checking back in the boot up script, we notice these registers are at the end of the script. There doesn't seem to be any reason for them not the be initialized. It feels like we're getting closer here. We use breakpoint debugging within the startup script to isolate the lines causing the issue. + Show Spoiler + If this script has been running for 10 seconds then shutdown the board, and stop the script Ahh, a watchdog in the script to fail if the script is going on for too long. We check with the developers who wrote the debugger, and sure enough, the wait(n milliseconds) function uses the system timer to wait in a spin loop. + Show Spoiler + The screen share was causing the system timer to slow down just enough so that the microchips initialization would finish before the host computer counted to 10. Without the GoTo meeting, the system counts just a wee bit quicker, and the startup script would exit before completing. We increased the time to 25 seconds and everything worked fine tl;dr - Heisenbug only reproduces when off the phone with tech support. I will henceforth refer to all elusive bugs as heisenbug Heh, it's supposed to refer to the fact that attempting to debug the behavior affects the behavior in the first place. http://en.wikipedia.org/wiki/Heisenbug | ||
RoyGBiv_13
United States1275 Posts
On November 27 2012 09:11 darkness wrote: I'm trying to print network output from another class, but 'out' doesn't get recognised. To be clear, here is some code: in a class implementing Runnable (method run()):
However, I'm trying to send output from a method in another class. E.g.
I hope it's clear. Sorry for not releasing full code, but university is very strict with plagiarism, so I don't want them to find my code here. I guess scope isn't big enough, but I have no idea how to expand it either. An idea is to pass a reference or something like that, but all my attemps are unsuccessful. Try moving "PrintWriter out = null;" into the Class's scope, with a public static modifier, or access it via an instantiated object without the static modifier. In either case, the scope runs out with the last bracket in run(){...}, so you will have to move it into the class's scope. | ||
Shield
Bulgaria4824 Posts
On November 27 2012 10:22 RoyGBiv_13 wrote: Try moving "PrintWriter out = null;" into the Class's scope, with a public static modifier, or access it via an instantiated object without the static modifier. In either case, the scope runs out with the last bracket in run(){...}, so you will have to move it into the class's scope. Are you sure static is a good idea? I think the program doesn't run well. Object instance is what I've tried, but I need to find a way to do it while satisfying my constructor (variable client). | ||
phar
United States1080 Posts
That lets you use 'client' and 'out' anywhere in the scope of the class. If you need to use 'out' from another class, make a method to access it: public class Foo { Instantiate foo, call foo.writeString. Really hard to give more specific advice without knowing the situation more. But in general, you should reserve static references from outside a class into another (e.g. Preconditions.checkNotNull) for things that do not rely on any state that can change. It's ok for helper methods that rely only on constants and arguments passed in directly to the method, but it's really not kosher to have it reference something from a constructor. What happens when someone calls that static method without having previously called the constructor? Probably lots of NPEs (if you're lucky), or random wonky shit. There are other reasons to use or avoid static variables or static methods, but they're probably out of scope for what you need to know now. | ||
AmericanUmlaut
Germany2577 Posts
On November 27 2012 06:21 frogmelter wrote: What about for (int i = 0; i < myString.length(); ++i) { //do something } or for a c string char c; for (int i = 0; c != '\0'; ++i) { c = myString[i]; //do something } for looping through a string? I would argue that the first one for a C++ string is just as readable as the while loop that you wrote, if not more. You are right, strings aren't a very good example of my point. I was trying to come up with something that fit on very few lines ![]() | ||
KaiserJohan
Sweden1808 Posts
On November 27 2012 08:33 RoyGBiv_13 wrote: --Debugging story time-- Initial Symptom: A customer of ours recently upgraded the kernel of our proprietary OS, and were finding that occasionally the debugger was unable to flash the kernel onto the mirochip, and the whole thing crashed. We schedule a web meeting to see this happen in action, and sure enough, the kernel loads up fine with no issues. We retry it, hoping to reproduce the issue, and again, the board works just fine. They start closing every process they can, but still, no luck. Hmm, we check the board initialization script (to boot up the board before you flash the software onto it), and it is the same as the one we sent them and have tested extensively. Maybe it's flakey hardware? We have the customer try with a different board. Same result, it is working fine. We get off the phone, and close out the web meeting. This one is weird. Two minutes later, a phone call from them informs us that it's flaking out again, and to quickly join the meeting. We join up, and the next try works fine "That's strange, the only thing to change has been opening up the GoTo meeting...", so while on the phone with them, we disconnect the screen viewer, and sure enough, the board fails to boot up. In disbelief, we retry the experiment a few times. Literally, in this case, the problem is solved by being on the phone with someone from tech support. We jokingly quote them a rate to always have a tech support guy on the line, but certainly, these two completely unconnected pieces of software have no impact on each other couldn't cause the behavior of the system to change. Right? We now have at least one piece of evidence on this heisenbug, and the ability to branch from there. Do other programs cause this? Nope. Does this same heisenbug exist on other hosts (computers launching the debugger)? Can't test. Hmm, we take a step back. Why is the kernel failing to flash in the first place? There seems to be some registers on the board uninitialized. Checking back in the boot up script, we notice these registers are at the end of the script. There doesn't seem to be any reason for them not the be initialized. It feels like we're getting closer here. We use breakpoint debugging within the startup script to isolate the lines causing the issue. + Show Spoiler + If this script has been running for 10 seconds then shutdown the board, and stop the script Ahh, a watchdog in the script to fail if the script is going on for too long. We check with the developers who wrote the debugger, and sure enough, the wait(n milliseconds) function uses the system timer to wait in a spin loop. + Show Spoiler + The screen share was causing the system timer to slow down just enough so that the microchips initialization would finish before the host computer counted to 10. Without the GoTo meeting, the system counts just a wee bit quicker, and the startup script would exit before completing. We increased the time to 25 seconds and everything worked fine tl;dr - Heisenbug only reproduces when off the phone with tech support. Awesome story, thanks for sharing! | ||
FreezingAssassin
United States455 Posts
He said there was a way to link them from another source and it would make it that much easier. So my question is what is the other way to link them? | ||
fabiano
Brazil4644 Posts
On November 28 2012 01:57 FreezingAssassin wrote: We are currently working in HTML where we use a <a href> to link two pages together as you all know. But What my question is my instructor said there was a possible way for us to link pages without copying and pasting all the a href tags to every single page just to make it work. He said there was a way to link them from another source and it would make it that much easier. So my question is what is the other way to link them? With pure HTML I don`t really know any, but if you are using PHP and SQL (or any other server side language) you could leave your links stored in a DB and dynamically fetch for them. You can do a lot of things with Javascript too, I suppose... | ||
nakam
Sweden245 Posts
On November 28 2012 01:57 FreezingAssassin wrote: We are currently working in HTML where we use a <a href> to link two pages together as you all know. But What my question is my instructor said there was a possible way for us to link pages without copying and pasting all the a href tags to every single page just to make it work. He said there was a way to link them from another source and it would make it that much easier. So my question is what is the other way to link them? Frames? | ||
FreezingAssassin
United States455 Posts
On November 28 2012 02:05 fabiano wrote: With pure HTML I don`t really know any, but if you are using PHP and SQL (or any other server side language) you could leave your links stored in a DB and dynamically fetch for them. You can do a lot of things with Javascript too, I suppose... I don't think we were supposed to do it all in HTML. But it can be done in Javascript? | ||
| ||