It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Array problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array problem" Watch "Array problem" New topic
Author

Array problem

Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
So Im working on a project with simple sorting and for some reason I can't get this array working. The problem is occuring in my countUniqueWordOccurrences method at the bottom. The pseudo-code is written below the method. I'm assuming there is something wrong with one of my for loops, but I'm not sure what it is (I need to review my loops). The output says there is 1 unique word.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11151
    
  16

Your for-loops in the countUniqueWordOccurrences method look odd...

Generally, you would do


I'm not sure what you are trying to do, but I would think what you have wouldn't loop at all.

I am a big proponent of putting in "System.out.println()" statements to see what the code is really doing.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
fred rosenberger wrote:Your for-loops in the countUniqueWordOccurrences method look odd...

Generally, you would do


I'm not sure what you are trying to do, but I would think what you have wouldn't loop at all.

I am a big proponent of putting in "System.out.println()" statements to see what the code is really doing.


Well, after continuing to rearrage code, you would be correct, it no longer loops at all and I get an output of 0.

EDIT: Would I need to somehow re-add the scanner to this method?
Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
Alright, so I have changed that method to this:



It works for the most parts, except it's just relisting all the words with a "1" next to them and adding a "null" before each word. How can I get this to add up all instances of said word?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

What do you think line 11 of your last code snippet does?

The answer to what it actually does explains why it puts 'null' in front of each word. Then, to explain why each word has a count of 1, is because you assign 1 to it in line 12:

Since you assign 1 to every index in the array, that is what you get out.


Steve
Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
Steve Luke wrote:What do you think line 11 of your last code snippet does?

The answer to what it actually does explains why it puts 'null' in front of each word. Then, to explain why each word has a count of 1, is because you assign 1 to it in line 12:

Since you assign 1 to every index in the array, that is what you get out.


Right after I posted that I figured that out, but I'm still having trouble how to get that count to increment correctly. I'm either getting a unique word count of 296 (all the words) or 106 (no clue where that comes from). It should be 169 total unique words. After changing up my code to follow the pusedo-code given exactly I have:



However I'm getting 1 unique word again. I think I'm having the most trouble following the line "// add words[i] to uniqueWords[numUniqueWords] and set wordCounts[numUniqueWords] to 1." What am I coding wrong there?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

Show the new code.
Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
Steve Luke wrote:Show the new code.


The complete current code for the section is as follows:

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

What role does j play in that code (what is it supposed to do)? How does it change throughout the execution of the method? Why do you compare it to numUniqueWords? When you realize how j behaves through the method, you should understand how come you get exactly 1 unique word count.

It seems like you are taking shots in the dark. You need to stop coding and start writing the algorithm out on paper. Not just in pseudo-code, but a step-by-step execution so you understand what is happening.
Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
Steve Luke wrote:What role does j play in that code (what is it supposed to do)? How does it change throughout the execution of the method? Why do you compare it to numUniqueWords? When you realize how j behaves through the method, you should understand how come you get exactly 1 unique word count.

It seems like you are taking shots in the dark. You need to stop coding and start writing the algorithm out on paper. Not just in pseudo-code, but a step-by-step execution so you understand what is happening.


Nvm. xD Still working through it.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Steve Luke wrote:
It seems like you are taking shots in the dark. You need to stop coding and start writing the algorithm out on paper. Not just in pseudo-code, but a step-by-step execution so you understand what is happening.


I cannot agree strongly enough with this.

Write the steps down in English. Very simple, precise steps. When you go to translate those steps to Java, make sure you know what each piece of the Java you write is for, and how it corresponds to your English description. If you're not completely sure how a given bit of Java will behave, stop right there. Create a new Java program that ONLY demonstrates what that Java does. Only once you understand it should you put it in your real program and move on to the next step. If you have just 2 or 3 lines of Java code where you go, "I think this will do something like this, but I'm not really sure," then when you run your program and something goes wrong, it will get exponentially harder to figure out what the source of the problem is.
Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
Jeff Verdegan wrote:
Steve Luke wrote:
It seems like you are taking shots in the dark. You need to stop coding and start writing the algorithm out on paper. Not just in pseudo-code, but a step-by-step execution so you understand what is happening.


I cannot agree strongly enough with this.

Write the steps down in English. Very simple, precise steps. When you go to translate those steps to Java, make sure you know what each piece of the Java you write is for, and how it corresponds to your English description. If you're not completely sure how a given bit of Java will behave, stop right there. Create a new Java program that ONLY demonstrates what that Java does. Only once you understand it should you put it in your real program and move on to the next step. If you have just 2 or 3 lines of Java code where you go, "I think this will do something like this, but I'm not really sure," then when you run your program and something goes wrong, it will get exponentially harder to figure out what the source of the problem is.


Well, thanks to this, I finally figured it out. All the numbers count and list correctly, just having a little trouble with my sortByWordCount, but that shouldn't be too hard to figure out.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Excellent! The advice to step back, forget Java for a minute, and attack the problem in small pieces is often hard to follow. It can seem like the long way around when you already have a bunch of code and feel like you're "this close" to having it done. It's always rewarding when somebody embraces that advice!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array problem
 
Similar Threads
count instances of each word in a String
Passing Vector elements to an array
need help sorting Integer objects
Writing a faster String split.
Illegal Access Exception