Results 1 to 13 of 13
Thread: Bad Code method
- 06-27-2011, 04:51 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Bad Code method
Contained in the provided workspace is CodeTestExerciseBadCode, in this class is a method called readFile().
The instructions for this problem are as follows:
1. Please list your observations about this class, including bugs, poor programming, inefficiencies, etc...
2. Provide an alternate implementation that addresses your observations above, please call the method readFileFixed().
I just can't figure out what is wrong with it! Help please!!Java Code:package codeTest; import java.io.File; import java.io.FileReader; public class CodeTestExerciseBadCode { public CodeTestExerciseBadCode(){ } /** * @param args */ public static void main(String[] args) { CodeTestExerciseBadCode part2 = new CodeTestExerciseBadCode(); System.out.println(part2.readFile()); } public String readFile(){ File f = null; FileReader fr = null; StringBuffer content = null; try{ f = new File("c:/samplefile.txt"); fr = new FileReader(f); int c; while((c = fr.read()) != -1){ if(content == null){ content = new StringBuffer(); } content.append((char)c); } fr.close(); } catch (Exception e) { throw new RuntimeException("An error occured reading your file"); } return content.toString(); } }
Thank you!Last edited by JosAH; 06-27-2011 at 04:56 PM. Reason: Added [code] ... [/code] tags
- 06-27-2011, 05:03 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
1) What happens if the actual reading from the FileReader throws an IOException? Your FileReader stays open.
2) What happens if the file is empty? The StringBuffer content will still be null after the loop.
3) A StringBuilder is a bit faster than a StringBuffer.
4) You don't need a File object to open a FileReader.
5) You're not passing back what actually went wrong while attempting to read the file.
6) Reading entire lines is much faster than reading single characters.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-27-2011, 05:06 PM #3
So what you're saying is that you want us to do your homework for you? How much are you paying?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 06-27-2011, 05:46 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Jos is getting soft in his moderatordom...:)
- 06-27-2011, 06:01 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
I showed the true me: a cute, sweet, little puppy. It must be the weather: it is great out here; I've been working in my garden all day (in front of my laptop, sitting at my picknick table); the fridge in the barn is full of Grolsch and I have enough tobacco. Life is good for once ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-27-2011, 06:13 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
- 06-27-2011, 06:15 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
How else should we supose to learn if not by asking questions?
What a lovely bunch of ppl us r ;)
tks anyway
- 06-27-2011, 06:28 PM #8
Hi Manole
The people here are helpful, but you must make it as easy as possible for them to help you.
You can do that by stating Exactly what your issue is. In my opinion it is no good just pasting some code, and asking what's wrong with it.
Use the Debugger, find out where your code is going wrong. If you have found out some unexpected behaviour in a certain place in your code, and you're not sure why it is happening. Then ask here. I guarantee you, you'll get some help that way.
Advice from one n00b to another
>> What can be asserted without proof can be dismissed without proof. <<
- 06-27-2011, 06:49 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-27-2011, 06:53 PM #10
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 06-27-2011, 08:41 PM #11
Jos would be a wonderful CodeTestExerciseBadCode.
No bug ever had to calculate its fitnessfunction.
- 06-27-2011, 08:47 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 06-27-2011, 09:08 PM #13
Similar Threads
-
How to call an XML-RPC method from Java Code
By sreekanth.srk in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-31-2010, 12:36 PM -
java method injection in existing code
By abanmitra in forum Advanced JavaReplies: 6Last Post: 04-30-2010, 12:02 PM -
Java method to run code from string
By jforce93 in forum Advanced JavaReplies: 2Last Post: 02-02-2010, 10:33 PM -
Sychronizing a part of a code in a method
By SarahB in forum Threads and SynchronizationReplies: 1Last Post: 01-03-2010, 10:22 PM -
What will be output by the following code? using trim method.
By racewithferrari in forum New To JavaReplies: 4Last Post: 11-17-2009, 09:40 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks