|
yatagan posted:Why do you think this? It's perfectly acceptable to check the types in a switch, and your interface pretty much demands it if you can't change that interface. I'm not sure. It just goes against my intuition that it should be necessary for me to program a decision like that for every type of message receiver. Its likely just because I'm a bit lazy, and I haven't written anything substantial in Java in a long time.
|
# ? Feb 26, 2010 17:15 |
|
|
# ? Jun 13, 2024 05:48 |
|
Edit: stupid mistake
PianoDragn fucked around with this message at 19:14 on Feb 26, 2010 |
# ? Feb 26, 2010 19:10 |
|
SintaxError posted:I'm not sure. It just goes against my intuition that it should be necessary for me to program a decision like that for every type of message receiver. Its likely just because I'm a bit lazy, and I haven't written anything substantial in Java in a long time. The only thing I can think of to get around that without using metaprogramming is to have your abstract Message class define an abstract "onReceive()" method or something like that, and receiveMessage() just calls that. You'd still have to code each one but at least it would be close to the message implementation.
|
# ? Feb 26, 2010 21:13 |
|
SintaxError posted:I'm not sure. It just goes against my intuition that it should be necessary for me to program a decision like that for every type of message receiver. Its likely just because I'm a bit lazy, and I haven't written anything substantial in Java in a long time. The issue is you don't have a good way of doing dynamic dispatch in Java. If your prof is the patterns-happy type you can use the visitor pattern to do that sort of behavior. Otherwise what you're doing works.
|
# ? Feb 26, 2010 23:09 |
|
In my Java class I am required to use FindBugs in all projects. I'm having trouble in my latest lab because I am getting a FindBug message "may fail to close stream" on the line I've commented below. Anybody point me in the direction to fix this problem?code:
|
# ? Feb 27, 2010 18:34 |
|
Any good information on how to create an ideal ETag for, say, a wiki article.
|
# ? Feb 27, 2010 19:49 |
|
Glimm posted:In my Java class I am required to use FindBugs in all projects. I'm having trouble in my latest lab because I am getting a FindBug message "may fail to close stream" on the line I've commented below. Anybody point me in the direction to fix this problem? I don't personally have any experience with FindBugs, but "finally" blocks don't get executed if you call System.exit, so maybe that's why it's complaining? In a real world situation it probably doesn't matter too much, but as far as FindBugs is concerned, there are still paths in your code where "in.close()" never gets called. Does the flag go away if you close your reader inside the catch clauses before you call System.exit?
|
# ? Feb 27, 2010 19:56 |
|
Flobbster posted:I don't personally have any experience with FindBugs, but "finally" blocks don't get executed if you call System.exit, so maybe that's why it's complaining? In a real world situation it probably doesn't matter too much, but as far as FindBugs is concerned, there are still paths in your code where "in.close()" never gets called. It doesn't go away - and CheckStyle tells me not to bother because in can only be null within those locations. FindBugs is pretty neat - I find it helpful generally. Just wish I wouldn't lose points on my assignment for FindBug problems like the one I'm having right now!
|
# ? Feb 27, 2010 20:10 |
|
Glimm posted:In my Java class I am required to use FindBugs in all projects. I'm having trouble in my latest lab because I am getting a FindBug message "may fail to close stream" on the line I've commented below. Anybody point me in the direction to fix this problem? Have you tried this: code:
|
# ? Feb 27, 2010 20:58 |
|
As FindBugs guys say themselves, FindBugs isn't a silver spoon but a great helper. In this particular case it's semantically correct as stated above, however if you're closing the VM then it just doesn't really matter if you close the stream or not. I'd say that in cases like these it's important that you know what's going on and can explain to others (your teacher in this case) why you didn't get rid of that FindBugs error. Out of curiosity, does the error go away if you do code:
|
# ? Feb 27, 2010 20:59 |
|
Couldn't get it to go away with either of the above suggestions - I'll just take it to the prof and try to make sure I don't lose any points Thanks.
|
# ? Feb 27, 2010 21:15 |
|
Slight modification:code:
E: I should have refreshed. Parantumaton is right though. If you can explain it I imagine your prof would be OK with it. RitualConfuser fucked around with this message at 21:22 on Feb 27, 2010 |
# ? Feb 27, 2010 21:18 |
|
RitualConfuser posted:Also, which version of FindBugs are you using? I tried 1.3.9 and it doesn't find any errors with the original code. Weird, I'm using: 1.3.9.20090821 which appears to be the latest version?
|
# ? Feb 27, 2010 21:29 |
|
Glimm posted:Weird, I'm using: 1.3.9.20090821 which appears to be the latest version? Odd. You could try commenting out the "do something with the file" code and re-run FindBugs to see if that's where the problem is.
|
# ? Feb 27, 2010 21:46 |
|
RitualConfuser posted:You could try commenting out the "do something with the file" code and re-run FindBugs to see if that's where the problem is. Doh.. I have an error condition in there that calls System.exit without doing in.close, that fixed it.
|
# ? Feb 27, 2010 22:10 |
|
ufarn posted:Any good information on how to create an ideal ETag for, say, a wiki article. I would think the revision # alone would be a good strong etag.
|
# ? Feb 27, 2010 23:07 |
|
I'm tying to cast an object to the same object but i'm getting a ClassCastException.code:
|
# ? Mar 1, 2010 21:08 |
|
Try just Transport t ¿ in.readObject();
|
# ? Mar 1, 2010 21:14 |
|
MEAT TREAT posted:Try just Transport t ¿ in.readObject(); Doesn't seem to work? Forgot to say, if it means anything, that Transport implements serializable
|
# ? Mar 1, 2010 21:48 |
|
karuna posted:I'm tying to cast an object to the same object but i'm getting a ClassCastException.
|
# ? Mar 1, 2010 22:14 |
|
Try printing out in.readObject().getClass().getName() and see what kind of object you actually are getting back
|
# ? Mar 1, 2010 22:17 |
|
Transport - Used to wrap whatever I want to send accross. I am calling remote methodscode:
code:
code:
karuna fucked around with this message at 22:45 on Mar 1, 2010 |
# ? Mar 1, 2010 22:36 |
|
karuna posted:Returns "Client.Transport" so all seems ok here? I think you'd get a different error were the serial version ids mismatched, but you might want to check that as well, but it should be okay if you're copying your class files. Perhaps you're trying to load them from different ClassLoaders, and that is causing a problem.
|
# ? Mar 1, 2010 22:46 |
|
I see what I've done wrong, I assumed I could just copy Transport in Client to Transport in Server. System.out.println(Transport.class.getName()) returns Server.Transport But I can just import the Transport class instead. Import Client.Transport; This seems to solve the problem code:
|
# ? Mar 1, 2010 23:14 |
|
Could someone explain to me why the following line:code:
|
# ? Mar 3, 2010 12:31 |
|
chippy posted:Could someone explain to me why the following line: Works for me, are you sure you have tabs in your string?
|
# ? Mar 3, 2010 12:40 |
|
Never mind, I'm a tool and had it in the wrong place :facepalm: Should have tested properly before I came running. Thankyou.
|
# ? Mar 3, 2010 12:48 |
|
OK, so for this program for class, I need to make my own linked list and an iterator for that list. I am having trouble creating this iterator class . . . In Java, what IS an iterator? My linked list consists of these DoubleListnode<E> things that are doubly-linked nodes that you'd find in a linked list. This iterator class implements Iterator<E> but trying to just make an iterator doesn't really work. Would the iterator be some sort of <E> object? Or something else entirely? What would the constructor look like? I assume that it has to be set to point at the beginning of the list when it's constructed as well.
|
# ? Mar 4, 2010 00:28 |
|
To "iterate" through a collection means to visit every element in a collection. You can iterate through the ints in an ArrayList<Integer>:code:
code:
code:
An ArrayList is backed by an array. You've got a linked list, so it's a different data structure underneath, but should be the same to work with. Start by pointing at the first element in the linked list, and i.hasNext() should return true and i.next() should return the first element and try to move to the second, and so on. epswing fucked around with this message at 03:01 on Mar 4, 2010 |
# ? Mar 4, 2010 02:51 |
|
Specifically, Iterators must implement Iterator. To use a foreach loop on an object like epswing mentioned, that class must implement Iterable. It's also worth noting that you can use the Java5 foreach construct to iterate over arrays as well as Iterable classes.
|
# ? Mar 4, 2010 02:59 |
|
I should add a note that there is nothing in the Iterator specification that says that you must return the same item order for the same collection every time. It could be completely different each time even though nothing's happened to the data since the last time. It just so happens that people count from 0 to the end of the list for arrays and lists, for example, and we're all used to arrays returning the same thing every time as a silent, implied convention. This idea breaks down when you get into hash tables and data structures that have several intuitive ways to travel through all the elements.
|
# ? Mar 4, 2010 19:52 |
|
Okay, so here I'm suppose to make a simple program that will read from a file, and then make two more files (firstFile and secondFile). firstFile is suppose to be the input file, except in all uppercase. secondFile is suppose to be a compilation of all content of all the input files so far, also in uppercase. Now, the first output works, I can consistently make a file with firstFile that gets overwritten each time I run the program, which is exactly what I want. However, it does not write into the second file. secondFile is created, but it remains empty, no matter how many times I run the program. What's wrong? I suspect I'm doing something wrong with FileWriter. code:
Thanks for any help
|
# ? Mar 4, 2010 20:58 |
|
Without giving a full blown solution: In the first loop, you can actually do two things at once - write each line to first file in uppercase (which you are already doing, and will complete the first subtask), and write each line to the second file "as-is". This will be the first part of the second file task (Thats if i'm understanding your task correctly). Doing it in one loop is faster in this instance. To do this you will need to save the nextLine() as a String inside the loop and pass that in to println so you can use it more than once. In the second loop, read from the first output file and write straight in to the second output file. This should give you a second duplicate block of text in that file, but in uppercase. A few observations in your current code: You are executing the same loop twice (in effect). If the first loop has already ended because fOne has no further lines, why would it loop at all the second time? You also seem to be getting lost in writers wrapping writers. Use a separate writer for each file, and give them a better name (e.g "firstFileOutput" instead of "outputfile"). It will make your code easier to understand, not just for you but anyone else trying to use it. You can get clever with variables/instances when you have the basics down. EDIT: I don't have JDK installed on this machine to check it, but this is how I would do it if I were to follow the style of your current code (i'm also assuming a little with regards to the classes you are using, having never had the need to use them before). code:
General Lee Awesome fucked around with this message at 23:20 on Mar 4, 2010 |
# ? Mar 4, 2010 22:50 |
|
I need to sort a collection: LinkedList<ArrayList<String>> I need to sort it based on an element in the ArrayList, the last element. I am looking to write my owm comparator and use Collections.sort but I'm having trouble getting some examples for a comparator that I can adopt. Anyone care to have a look at how I would go about writing the comparator? Would be much appriciated! Note: the last element in ArrayList<String> can be cast to a Double Thanks
|
# ? Mar 5, 2010 17:54 |
|
code:
hlfrk414 fucked around with this message at 18:28 on Mar 5, 2010 |
# ? Mar 5, 2010 18:13 |
|
hlfrk414 posted:
EDIT: nevermind, works now. I was changing the list before printing it so thought it wasn't sorting correctly. Thanks man much appriciated. karuna fucked around with this message at 18:40 on Mar 5, 2010 |
# ? Mar 5, 2010 18:38 |
|
*resolved
Kaltag fucked around with this message at 14:25 on Mar 8, 2010 |
# ? Mar 5, 2010 20:50 |
|
Speaking of that pesky iterator . . . I used your guys' advice and tried to write it. I suspect that it's not working correctly though. Could you guys take a look at it and see if it makes sense? I've looked it over several times and I just can't see it.code:
|
# ? Mar 7, 2010 07:49 |
|
Your hasNext() has two ways to check if there are more nodes after the current. You should choose one way and stick with that. How is hasNext() going to work on an empty list?
|
# ? Mar 7, 2010 12:24 |
|
|
# ? Jun 13, 2024 05:48 |
|
I am in posted:Your hasNext() has two ways to check if there are more nodes after the current. You should choose one way and stick with that. How is hasNext() going to work on an empty list? Is this good? code:
|
# ? Mar 8, 2010 02:30 |