Computer
Microsoft Blames Apple for Xbox Mobile Store Delay
Read more of this story at Slashdot.
Google Is Baking Gemini AI Into Chrome
Read more of this story at Slashdot.
Starfish Space Announces Plans For First Commercial Satellite Docking
Read more of this story at Slashdot.
Jupiter Was Formerly Twice Its Current Size and Had a Much Stronger Magnetic Field
Read more of this story at Slashdot.
CodeSOD: Buff Reading
Frank inherited some code that reads URLs from a file, and puts them into a collection. This is a delightfully simple task. What could go wrong?
static String[] readFile(String filename) { String record = null; Vector vURLs = new Vector(); int recCnt = 0; try { FileReader fr = new FileReader(filename); BufferedReader br = new BufferedReader(fr); record = new String(); while ((record = br.readLine()) != null) { vURLs.add(new String(record)); //System.out.println(recCnt + ": " + vURLs.get(recCnt)); recCnt++; } } catch (IOException e) { // catch possible io errors from readLine() System.out.println("IOException error reading " + filename + " in readURLs()!\n"); e.printStackTrace(); } System.out.println("Reading URLs ...\n"); int arrCnt = 0; String[] sURLs = new String[vURLs.size()]; Enumeration eURLs = vURLs.elements(); for (Enumeration e = vURLs.elements() ; e.hasMoreElements() ;) { sURLs[arrCnt] = (String)e.nextElement(); System.out.println(arrCnt + ": " + sURLs[arrCnt]); arrCnt++; } if (recCnt != arrCnt++) { System.out.println("WARNING: The number of URLs in the input file does not match the number of URLs in the array!\n\n"); } return sURLs; } // end of readFile()So, we start by using a FileReader and a BufferedReader, which is the basic pattern any Java tutorial on file handling will tell you to do.
What I see here is that the developer responsible didn't fully understand how strings work in Java. They initialize record to a new String() only to immediately discard that reference in their while loop. They also copy the record by doing a new String which is utterly unnecessary.
As they load the Vector of strings, they also increment a recCount variable, which is superfluous since the collection can tell you how many elements are in it.
Once the Vector is populated, they need to copy all this data into a String[]. Instead of using the toArray function, which is built in and does that, they iterate across the Vector and put each element into the array.
As they build the array, they increment an arrCnt variable. Then, they do a check: if (recCnt != arrCnt++). Look at that line. Look at the post-increment on arrCnt, despite never using arrCnt again. Why is that there? Just for fun, apparently. Why is this check even there?
The only way it's possible for the counts to not match is if somehow an exception was thrown after vURLs.add(new String(record)); but before recCount++, which doesn't seem likely. Certainly, if it happens, there's something worse going on.
Now, I'm going to be generous and assume that this code predates Java 8- it just looks old. But it's worth noting that in Java 8, the BufferedReader class got a lines() function which returns a Stream<String> that can be converted directly toArray, making all of this code superfluous, but also, so much of this code is just superfluous anyway.
Anyway, for a fun game, start making the last use of every variable be a post-increment before it goes out of scope. See how many code reviews you can sneak it through!
[Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!Fortnite Returns To Apple US App Store After 5-Year Ban
Read more of this story at Slashdot.
Japan's Honda To Scale Back On EVs, Focus On Hybrids
Read more of this story at Slashdot.
19-Year-Old Accused of Largest Child Data Breach in US Agrees To Plead Guilty To Federal Charges
Read more of this story at Slashdot.
KDE Is Getting a Native Virtual Machine Manager Called 'Karton'
Read more of this story at Slashdot.
KrebsOnSecurity Hit With Near-Record 6.3 Tbps DDoS
Read more of this story at Slashdot.
Spain Blocks More Than 65,000 Airbnb Holiday Rental Listings
Read more of this story at Slashdot.
Coinbase Data Breach Will 'Lead To People Dying,' TechCrunch Founder Says
Read more of this story at Slashdot.
Google Launches Veo 3, an AI Video Generator That Incorporates Audio
Read more of this story at Slashdot.
Google Is Rolling Out AI Mode To Everyone In the US
Read more of this story at Slashdot.
Chicago Sun-Times Prints Summer Reading List Full of Fake Books
Read more of this story at Slashdot.
Delta Can Sue CrowdStrike Over Global Outage That Caused 7,000 Canceled Flights
Read more of this story at Slashdot.
Google's Gemini 2.5 Models Gain "Deep Think" Reasoning
Read more of this story at Slashdot.
Google Brings AI-Powered Live Translation To Meet
Read more of this story at Slashdot.
Adobe Forces Creative Cloud Users Into Pricier AI-Focused Plan
Read more of this story at Slashdot.
Microsoft is Putting AI Actions Into the Windows File Explorer
Read more of this story at Slashdot.