Posts

Showing posts from April, 2023

When You Go Rookie and Forget that Arrays Have Indices

You get really lousy code - Rube Goldberg code, that's what 😊 You have to find a figure-of-merit that is the best from a list and then get the object with that FOM. You can, but I forgot that, in an ArrayList, the index is all you need 😊 And, having forgotten, I ended up with : for( int i=0; i<howMany; i++){ double leastDist = 30000000; // meters double distance; for( QuakeEntry qe : copy ){ distance = current.distanceTo( qe.getLocation() ); if( distance < leastDist ){ leastDist = distance; } } for( QuakeEntry qe : quakeData ){ distance = current.distanceTo( qe.getLocation() ); if( Math.abs( distance - leastDist) < 1 ){ copy.remove(qe); ret.add(qe); } } } When all I really needed was : for( int i=0; i<howMany; i++){ int minIndex = 0; // start with the first double distance; for( int k=1; k<copy.size(); k++ ){ distance = current.distanceTo( copy.get(k).getLocation() ); if( distance < current.distanceTo(copy.ge...

BlueJ : Stuck in the Dinosaur Era

Having to use the U. of Kent version of BlueJ. Must be a good reason for that, but, why not get your volunteers to put in basic stuff like autocomplete and... OMG : java.lang.NullPointerException with wordMap.clear() (HashMap) why my class DOES have a constructor. But it's : public WordsInFiles(){     HashMap<String,ArrayList<String>> wordMap = new HashMap<String,ArrayList<String>>(); } Do you smell a fault Yes, you're declaring a local variable inside the Constructor instead of initializing THE instance variable.  Why the hell can't the compiler or the IDE detect and warn for this? Thank you :  https://stackoverflow.com/questions/45199486/java-hashmap-class-throws-a-nullpointerexception How about this one? Why can't it tell that the Value is an ArrayList<String> and therefore cannot possibly contain a File object (yes, they don't do a good enough job of cautioning you to use File.getName() to the actual name :) wordMap = new HashMap...