JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The range of keys that will be included in the subMap view are from "b" to "g". That includes all possible keys that sort after "b" but before "g". You can add any keys within that range, but not ouside it. "b" being the second letter in the alphabet has no significance whatsoever.

ps: Technically speaking the range is from "b" inclusive to "g" exclusive

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

TreeMaps are always sorted by their keys.
subMap returns a view of a contiguous range of keys of the TreeMap, including only those keys within the specified range.
Because it's a view, updates to it just update the origional (backing) TreeMap.
Similarly updates to the origional TreeMap arre immediate visible in the view (provided they are inside the range specified when the subMap as created).
Because the view is only a specified range of keys, any attempt to add a key to it must be within that range. Anything outside that is an error.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, I agree with all that. He certainly should not be removing/adding listeners dynamically to fix this.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thanks for the link - hopefully OP will identify the exact source of the NPE to determine whether this is a Swing problem or something related to the underlying code.
I have had similar problems in the past - ie replace the contents of a list box -> fires the list box listener multiple times, first time with no contents. Seemed to me at the time that testing for, and ignoring, the irrelevant calls was the easiest solution.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your analysis of the problem seems reasonable. Why not just check the selection in JList ListSelectionListener and ignore the event if the selection is null?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Input: you need a List. You give it an ArrayList which is a subclass All ArrayLists are Lists, so it's OK
Output: You need an ArrayList. YOu give it a List, which is a superclass. Not all Lists are ArrayLists (eg some are LinkedLists), so it's not OK

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ArrayList implements List, so every ArrayList is also a List.

Can you post the exact syntax you used for the error version?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

After a very quick look I see a model with a boolean column (checkbox) but all String data.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Looks like you have put some String data into a column whose class is Boolean.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You should know by now that when there's an exception message we want to know WHICH LINE IS IT ON!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, that's getting a lot closer. Main problem is that the getColumnClass method needs to be in the table model class, and takes an int as its parameter.
Here's an example that just illustrates the stuff you need (for the requirements that you have posted so far).
First, you need a table model that's just like the DefaultTableModel, except that it returns sensible values for getColumnClass, eg

    class BetterDefaultTableModel extends DefaultTableModel {

        private Class[] columnTypes;

        public BetterDefaultTableModel(Object[][] data, Object[] columnNames,
                Class[] columnTypes) {
            super(data, columnNames);
            this.columnTypes = columnTypes;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return columnTypes[columnIndex];
        }
    }

This just adds an array of column classes to the default, and implements a constructor just like the one you are currently using, except that you also pass in an array of column classes. That array is then used to give the correct return value for getColumnClass.

And here's an example of using it:

        JTable table = new JTable();
        Object[][] data = {{"hello", true}, {"goodbye", false}};
        String[] columnNames = {"a String", "a Boolean"};
        Class[] columnTypes = {String.class, Boolean.class};
        table.setModel(new BetterDefaultTableModel(
                data, columnNames, columnTypes));

Please spend a few minutes reading and understanding that code, then try using it in your environment. Assuming it does what you need, you can then use what it taught you to write your own program.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I really don't know how to make this any clearer than the Java and training experts who wrote the tutorial. Your code doesn't even start to resemble the tutorial code I linked to. Did you read it? I honestly don't know to make it any clearer than that. Sorry.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK. That code is going off in all kinds of wrong directions. I need you to go back and re-readthe link I gave earlier
This part of the tutorial
You need to create a simple table model, just like the one in that tutrorial, and implement a getColumnClass metjod in it, like the example a bit further down that tutorial (code in bold), that returns the appropriate class (eg String, Integer, Boolean) for each column. Returning Boolean will make that column use check boxes automatically.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Without seeing your code it's impossible to say what's wrong. Did you implement getColumnClass in yout table model?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your processing is preventing Swing from getting any CPU cycles, so any GUI updates are blocked until your processing finishes. You need to read up on the Swing Event thread and the SwingWorker class. Start here http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

peek if you want to see the first item without removing it, yes.
I don't think gthere's any way to see the later items in the correct order without removing (poll) the first element.
NB highest priority == lowest Integer value, counter-intuitively!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

PriorityQueue inherits its toString() from java.util.AbstractCollection, which doesn't know about sort orders. (println uses toString() on all its arguments).
If you poll() the elements as intended, you get them in the correct order, eg

Integer i;
while ((i = pq.poll())!= null) System.out.print(i + " ");
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry, my misunderstanding. Let's start again from the very beginning.
A JTable will display a column of boolean values as check boxes by default. Provided it knows tha the column contains Booleans. If you just pass a Object[][] or Vector to the JTable constructor it doesn't know the column types. So you need to provide a custom table model that defines your "e" column as Boolean. This part of the tutorial explains...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's what check boxes do. They toggle btween true and false when you click them.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, "exactly the same" was a bit of an exaggeration, but the overall approach is the same.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This Oracle tutorial shows how to put a combo box ina JTable - it's exactly the same for a check box.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Either you set the constraints for the layout (like line 76 above) then those apply to whatever you add afterwards by default, OR you specify a set of constraints in the add(component, constraints) method. I prefer to stick with the standard API and expicit constraints, if only for clarity, and it's not really any extra code, eg...

constraints = new GridBagConstraints();
...
constraints.xxx = xxx // set all the common options
...
constraints.yyy = yyy // set any  unique options for component 1
add(component1, constraints);
...
constraints.zzz = zzz // set any  unique options for component 2
add(component2, constraints);
// etc

... or I frequently don't bother with a constraints variable at all, and just copy/paste/update as required, eg

import static java.awt.GridBagConstraints.*;

Insets zeroInsets = new Insets(0, 0, 0, 0);

contentPane.add(jButton1, new GridBagConstraints(0, 6, 1, 1, 0.5, 0.0,
               CENTER, HORIZONTAL, zeroInsets, 0, 0));
contentPane.add(jButton2, new GridBagConstraints(1, 6, 1, 1, 0.5, 0.0,
               CENTER, HORIZONTAL, zeroInsets, 0, 0));
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In his horrible method - line 76 - he calls layout.setConstraints( component, constraints) before calling add

The example in the API doc is similar, except that it passes the constraints explicitly into the method, so everyone knows what's going on.

ps: Personally I always use the simple add(component, constraints) method - no extra methods and it's perfectly clear what's going on.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That profiler output is pretty typical.You cklick the start button so that triggers a whole raft of methods calling other methods until it gets down to your code, after which all the methods terminate. So they all take at least as long as your method. The "self-times" are just an artifact of the profiling. Just ignore the awt/swing stuff.
It looks like your entry submission method is taking 22 secs, of which 21.7 are spent in the matching method that it calls. That's where the problem lies.

How do I efficiently implement this functionality that I want?

My fastest version used a hashmap as per my previous posts.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK!!!
1) Just for simplicity. You can have a GridBagLayout on a JPanel, but there's often less need for sub-panels to achieve your layout than there would be for some other managers. On average its still very common to see multiple JPanels - relating to different logical parts of the layout.
2? This makes me shudder. The program doesn't directly call the normal add method, it uses the author's own addComponent method (lines 69-78) that calls the normal add after setting the layout's constraints using the constraints variable. As an example of sharing a global variable that works via side effects it should go down in the all-time book of terrible programming.
It's like the example shown in the GridBagLayout API doc, but corrupted.
3? Every swing component is a subclass of JComponent, which in turn is a subclass of java.awt.Component. So if the method takes a Component it will accept any AWT or Swing compoent.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's a real problem with Scanner's design - so many people fall into this trap.
You have some input with an int followed by some text, eg
101
John Doe
... and you try to read it with
int num = scanner.nextInt();
String name = scanner.nextLine();
... and name is an empty String ("")!

Here's why:
Your input looks like this with the new line characters shown explicitly
101\nJohn Doe\n
nextInt takes the int value from the scanner, and stops when it finds a chaacter that's not part of an int, so it takes the "101", leaving this in the scanner
"\nJohn Doe\n"
then nextLine takes everything up to the first \n character - a zero-length String ("").

Possible fixes:
1. Add add extra nextLine() between the nextInt and the real nextLine to clear the unwanted \n. This may be difficult if the nextInt and the nextLine are in different areas of code which are not always executed together.
2. Give up on nextInt (etc) and just read whole lines and parse then into ints (etc) with Integer.parseInt (etc) - in which case you can junk the whole scanner and use a BufferedReader instead.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That message implies you are still on Java 1.6. If so an immediate upgrade to 1.7 is a top priority, noit just for the language/API enhancements buta also for security and perfomance fixes. Oracle officially stopped releaseing public updates for 1.6 in March 2013 (see http://www.oracle.com/technetwork/java/eol-135779.html )

Having done that, a clean install of the latest NetBeans 7.4 should fix the profiler problem.

I still find this bizzare. The only Java things I have ever seen taking more than a few seconds were either IO-bound or massively recursive.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just a random thought - it's not possible that any of those get methods are causing the word list file to be re-loaded each time (that could explain the time)?

It's bizarre that your profiler is going NPE - the profiler is absolutely the most (only?) valuable tool to sort out performance problems, so it would be worth your time to try to sort that out. Can you skip the calibration and go on to profile regardless?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

at myapp1.init(myapp1.java:39)

tells you its on line 39, ie add(c4); the only thing that can be null in that is c4. You declare c4 on line 9, but you never seem to give it a value, so it's null. On line 21 you give c1 a value. On line 25 you give it a different value. Should that have been c4?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

They are all variables.
Some veriables are "primitives", eg int, float, boolean, char (See Java Language Spec section 4.2 for the complete list, but they alll have names that begin with a lower case letter. All the classes in standard Java have names the begin with an upper-case letter). They just contain a value; and == workslike you would expect.
Others are "reference variables" and they contain a reference ("pointer") to an Object. Thet are not themselves objects, nor do they contain objects, they just have a reference to an object. Their == tests that their references are equal, ie they are both references to the same Object. To see if two different Objects are considered "equal", you have to call their equals method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Have you tried a "clean and build" on the project?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I still can't see anything in there that could get you within orders of magnitude of 600,000,000 microSecs with such tiny data volumes.
Did you try the NetBeans profiler to where that CPU is being burned?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

All this encouraged me to finish the map version, and it is insanely fast.

I built a map where the key is a String of all the letters in a word, sorted in to alpha order, and the value is the word or words that match those letters.

Then given a word I sort its letters into alpha order and use the resulting word to get the matches from the map.

Because you always know the word's length, I split the map into an array of 20 maps, one for each length.
The master word list was the 267,750 word master UK/US scrabble word list.

Loading the list from file and building the maps took 700mSec (one-time only, at startup).
Finding all the words for a given anagram takes a consistent 2 microSeconds if the Map is a HashMap, 4 microSeconds if it is a TreeMap. (The very first anagram takes about 4 times that).

I'm happy to share the relevant code if anyone is interested.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

JamesCherrill says his hashed maps solution takes a few seconds if that.

So now I had to log the actual elapsed times - they are actually about 60 microSeconds for 3 letter anagrams, up to 1.5 milliSecs for 9 letters. That's for the simple search of an ArrayList of Scrabble words matching letters in a small nested loop.
I didn't keep the TreeMap version, that was just an experiment, and I didn't finish all the tidying up around multiple words with the same anagrams. My gut feel is that it would be faster, but at a worst case thats one or two millisecs I decided that the simple approach was fast enough.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's no problem holding it all in RAM. I found a large Scrabble dictionary with about 180k words in it, but that's only a few MegaBytes stored in an ArrayList<String> and you can scan its entire contents in milliSeconds.

I did experiment at the time with holding a sorted version of every word as key in a TreeMap (the original words as values). That gives a guaranteed O(log n) time to find any anagram. That's surely got to be the the fastest algorithm, even after adding in the overhead of dealing with multiple words that have the same sorted letters. In the end, however, it was just as subjectively instantaneous, but easier code, to check anagrams on the fly using a method that matches letters one at a time

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Nothing there jumps out at me, but since you are usig Netbeans, why not run the profiler and see where your CPU time being consumed? It's the clock face with a green run arrow near the right-hand end of the standard toolbar....

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's bizarre - can't find the file when it just gave you that file from its directory listing but I may know why ;)
Is this still the code?

for (File file : listOfFiles) {
   ... 
   files = file.getName();

   if (files.endsWith(".txt") etc
      br = new BufferedReader(new FileReader(files)); 

if so - you get the file name to check the extension, then use the file name to open the reader. The filename doesn't include the path, so this only works for your home dir. All you need to do is to open the stream using the actual File object as parameter, not just the file name string.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

print the File's absolute path to see how Java has completed the path, eg

String path = scan.nextLine();//Files present in this path will be analysed
File directory = new File(path);
System.out.println("Searching in " + directory.getAbsolutePath());
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@stultuske: yes, he is reading all the .txt files in a directory and anyalysing their contents. This is a follow-up to this thread
http://www.daniweb.com/software-development/java/threads/470704/java-code-to-make-an-word-frequency-counter
Dinesh got the application working beautifully in his development environment, but is now having problems deploying it in a different directory structure.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The Date class was there in the very first Javas, but it was a poor piece of design - it doesn't handle time zones etc, wich makes it essentially useless for anything real. It was replaced by the Calendar interface, which has an implementation in the GregorianCalendar class. This is what you should today use for anything date-related.
If you have legacy code that will break if you change the Date instances, then your best solution may be use Calendar to do all the day/month/year stuff, then convert to Date at the last possible moment using

new Date(myCalendarInstance.getTimeInMillis();

You may want to know that is all about to change again. Java 8 comes with a complete new date/time API that greatly enhances and fixes support for dates, times, intervals etc. Depending on your support requirements for different Java versions it may be possible/better to postpone any Date-related changes until you can use the Java 8 classes?

http://docs.oracle.com/javase/tutorial/datetime/

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There are 5040 combinations, and perhaps 20 words in the row of the dictionary. Should it really be takin 10 minutes? Or you think there is maybe another problem in the design/implementation?

Running 5K iterations against 20 data items should so so fast you can't measure it. I have a Java program to find anagrams and it searches an 18K word database for anagrams in real time faster than I can type the letters.

There's obviously somethiung very wrong with your code - post it here and let's see what it is.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Excellent! You've done a lot of work there, and showed that you can learn fast. You're going to ace this course.

One tiny hint: if you're using Java 7 (like you should) then you can omit many of those type specs, eg

List<Map.Entry<String,Integer>>topList=new LinkedList<>();

Java 7 will infer the missing types based on the variable's declaration.

Anyway, if we're all done with this please mark this "solved" - it's gone on long enough, and you can always start another thread for your next assignment.

It's been fun
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's quite a neat NPE generator if you start with an empty map.
Check out this from Java 8:

    wordMap.put(word, wordMap.getOrDefault(word, 0)++)

and pre-incrementing may be closer to what you areally want

    wordMap.put(word, ++ wordMap.getOrDefault(word, 0))

;)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, a for loop is a good solution, or you could use List's subList method...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

First: you did a good job on that - excellent progress. Just one problem with the sort:
The Map.Entry class does not implement Comparator, so it has no default or "natural" sort order. That's why you need to create your own Comparator for the sort. The code you found on the web is a good place to start - it defines a Comparator that compares Map.Entry objects by ignoring their keys and comparing their values.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That code creates a new LinkedHashMap. LinkedHashMap is interesting because it remembers the order that its elements were added in. The method starts by getting a List of all the entries (key/value pairs) from your Map, sorts them by value, according to the new Comparator, then adds them to the LinkedHashMap, so the LinkedHashMap is now in the same order that the List.

At this point you should have noticed that you have no need of the LinkedHashMap at all for this application. Everything you need is in the sorted List.

Printing the first "n" entries from the list is trivial, but since you want the highest counts, not the lowest, you need to change the comparator.

I recognised that code immediately, so there's a serious chance that your teacher will as well, which may not help your final grade!

If I were you, I would now write my own highy simplified version of that, which just does what's needed for this exercise. Get rid of all the generics and hard-code the types from your own HashMap. Fix the comparator to sort descending rather than ascending. Get rid of the LinkedHashMap and just use the List. That will also prove that you understood what you were doing and din't just copy something blindly.
Good Luck!
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is a really interesting problem, but because it's your homework I can't just give you the answer. But here are some things to remember:
HashMaps don't have any ordering, so it makes no sense to sort them. A TreeMap is held in the sort order of its keys, but you want it sorted by its values.
You can't just use the count as key and word as value because there will be many duplicate counts.
You really need a way to group together a word and its count so you can sort them together... ... ...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, but how does that work in general? What genberal rukle do you apply?

Do you ignore "er", "est", etc at the end of words? What about "deer" or "west"?

Do you ignore a word if it's the same as another word but with extra letters at the end? What about "man", "many", "manifest"?

Do you combine both those rules?

Do you have a complete English dictionary with the "root words" identified?

... you get the idea ....

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thta's becuase you are checking if stopList contains the whole line from the txt file. You should be checking individual words.