Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
samcarsten
Sep 13, 2022

by vyelkin
ok, so can i cast wordToFrquencyMap to an array, then reverse the order of the array?

Adbot
ADBOT LOVES YOU

samcarsten
Sep 13, 2022

by vyelkin
None of the stuff google turns up is making me less confused about collectons.

samcarsten
Sep 13, 2022

by vyelkin
ok, looks like a collection cannot contain the key/value pairs I'm working with, so I'm back to square one.

Tesseraction
Apr 5, 2009

Yes, looking back at it, is the idea that the output for that part is that if the words "the" "bill" and "rights" appear 100, 200 and 150 times respectively, you get this output:

200: bill
150: rights
100: the

?

samcarsten
Sep 13, 2022

by vyelkin

Tesseraction posted:

Yes, looking back at it, is the idea that the output for that part is that if the words "the" "bill" and "rights" appear 100, 200 and 150 times respectively, you get this output:

200: bill
150: rights
100: the

?

yes, exactly.

Tesseraction
Apr 5, 2009

And what does it do if two words appear at the same frequency?

samcarsten
Sep 13, 2022

by vyelkin
prints "1 : " then each of the words.

Tesseraction
Apr 5, 2009

Right, so your teacher gave you a better hint:

code:
         frequencyToWordMap.forEach((k, v) -> {
            System.out.println(k + ": " + v);
        });
Which shows that Maps have a forEach function that effectively iterates over the map. Remember too that frequencyToWordMap was explicitly constructed to store things in reverse order:

code:
private static final Map<Integer, String> frequencyToWordMap = new TreeMap<>(Collections.reverseOrder());
This should be enough to get your assignment done.

Oh, but a fair warning. value++ and ++value have different effects - something I picked up on while copying your code. ;)

CPColin
Sep 9, 2003

Big ol' smile.
Neither makes sense in this context, as the code doesn't use value again after replacing it in the map.

Tesseraction
Apr 5, 2009

Yeah I'm not sure why they both exist except the teacher testing what the student does.

samcarsten
Sep 13, 2022

by vyelkin
ok, almost there. It's getting information put in to the proper map, but it's not listing more than one value for the key. Is there another command I should be using besides put?

code:
 private static void process(File file) throws FileNotFoundException {
    	Scanner input = new Scanner(new File("BillofRights.txt"));
    	while (input.hasNext()) {
        	String currentWord = (input.next()).toLowerCase();
    		if (dictionaryWords.contains(currentWord)) {
    			if (wordToFrequencyMap.containsKey(currentWord)) {
    				Integer value = wordToFrequencyMap.get(currentWord);
    				wordToFrequencyMap.replace(currentWord, ++value);
    			}
    			else {
    				wordToFrequencyMap.put(currentWord, 1);
    			}
    		}
    		else {
    			misspelledWords.add(currentWord);
    		}
    			
    	}
    	wordToFrequencyMap.forEach((k, v) -> {
            frequencyToWordMap.put(v, k);
        });
    }

Tesseraction
Apr 5, 2009

Think about how you incremented the count, and how you can do something similar with a string.

samcarsten
Sep 13, 2022

by vyelkin

Tesseraction posted:

Think about how you incremented the count, and how you can do something similar with a string.

you can increment a string?

Tesseraction
Apr 5, 2009

No, but think about the mechanism of addition.

You write value++ but that's just a shorthand for value + 1

samcarsten
Sep 13, 2022

by vyelkin
ok, i know what I'm trying to do, but I can't get it to work, it's just repeating the same word.

code:
    	wordToFrequencyMap.forEach((k, v) -> {
            String word = k;
            if (frequencyToWordMap.containsKey(v)) {
            	frequencyToWordMap.put(v, k + " " + word);            	
            }
            else {
            	frequencyToWordMap.put(v, k);
            }
        });

Tesseraction
Apr 5, 2009

You're using:
String word = k;

But you need another way of doing this.

You're no longer using a Scanner, so you need to think about where you're getting the value of what is potentially being added vs. what has already been added.

Remember what a Map lets you do.

samcarsten
Sep 13, 2022

by vyelkin
I'm stumped. I've got it so it's no longer replacing each new word, but it's not adding words either.

code:
wordToFrequencyMap.forEach((k, v) -> {
    		String word = k;
            if (frequencyToWordMap.containsKey(v)) {
            	frequencyToWordMap.replace(v, k, k + " " + word);            	
            }
            else {
            	frequencyToWordMap.put(v, k);
            }
        });

Tesseraction
Apr 5, 2009

How do you get a current value from a Map?

samcarsten
Sep 13, 2022

by vyelkin

Tesseraction posted:

How do you get a current value from a Map?

you give a key.

Tesseraction
Apr 5, 2009

Right, and look at where you are (or aren't) giving it a key.

Tesseraction
Apr 5, 2009

It might help if you comment each line of the code you just posted and tell me what it's doing, as far as you're concerned.

samcarsten
Sep 13, 2022

by vyelkin

Tesseraction posted:

It might help if you comment each line of the code you just posted and tell me what it's doing, as far as you're concerned.

code:
    	wordToFrequencyMap.forEach((k, v) -> {//for each key/value entry
    		String word = k;//current word
            if (frequencyToWordMap.containsKey(v)) {//if new map has a key/value already
            	frequencyToWordMap.replace(v, k, k + " " + word);//replace value with value + new word     	
            }
            else {
            	frequencyToWordMap.put(v, k);//add key/value pair
            }
        });
I'm absolutely stumped.

Tesseraction
Apr 5, 2009

Right, so you're thinking along the right lines, and you're handling v correctly.

But if you look on

code:
            	frequencyToWordMap.replace(v, k, k + " " + word);//replace value with value + new word     	
What did you previously define k as?

samcarsten
Sep 13, 2022

by vyelkin

Tesseraction posted:

Right, so you're thinking along the right lines, and you're handling v correctly.

But if you look on

code:
            	frequencyToWordMap.replace(v, k, k + " " + word);//replace value with value + new word     	
What did you previously define k as?

word? I think?

Tesseraction
Apr 5, 2009

Right, so you're changing the new value of the value for key(v) to "word" + "word"

samcarsten
Sep 13, 2022

by vyelkin

Tesseraction posted:

Right, so you're changing the new value of the value for key(v) to "word" + "word"

so how do i hold the value of the current word and add it to the previous value of the key/value pair?

Tesseraction
Apr 5, 2009

When you needed to get the previous word count, how did you get value before you applied ++?

samcarsten
Sep 13, 2022

by vyelkin

Tesseraction posted:

When you needed to get the previous word count, how did you get value before you applied ++?

Welp, that didn't work either.

code:
    	wordToFrequencyMap.forEach((k, v) -> {
            if (frequencyToWordMap.containsKey(v)) {
            	String word = frequencyToWordMap.get(v);
            	frequencyToWordMap.replace(v, k, k + " " + word);     	
            }
            else {
            	frequencyToWordMap.put(v, k);
            }
        });

Tesseraction
Apr 5, 2009

Check your invocation of replace() - you're using the replace-if-matches replace(K,Vold,Vnew), while you only need replace(K,Vnew)

You want this https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Map.html#replace(K,V)

not this https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Map.html#replace(K,V,V)

I was actually curious why it wasn't throwing an error with three arguments, hence looking up the Javadoc.

samcarsten
Sep 13, 2022

by vyelkin
ok, that makes it work. well, this weeks homework is done. Thank you very much for your help.

Tesseraction
Apr 5, 2009

No worries. Just remember me when you're a billionaire. ;)

Volguus
Mar 3, 2009

Tesseraction
Apr 5, 2009

Honestly, better than being forgotten.

samcarsten
Sep 13, 2022

by vyelkin
ok, so this weeks homework is manually creating a list object without using the inbuilt code object. So that means manually programming basic commands. Let's start with the first one.

code:
@Override /** Return true if this list contains the element e */
	  public boolean contains(Object e) {
		  
	    return true;
	  }
Now, I'm more familiar with how to do this in python, but shouldn't this just be a for loop and compare e to the object at list index i? The problem with that is, when I try to do that, I get a weird error.

code:
	  @Override /** Return true if this list contains the element e */
	  public boolean contains(Object e) {
		  for (int i = 0; i < MyLinkedList.size(); i++) {
	           
	        }
	    return true;
	  }
With this code, I get the error: Cannot make a static reference to the non-static method size() from the type MyLinkedList. I don't know what that means.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
When you use the name of a class like that (as in, MyClass.foo()), you're accessing things that are related to the class itself, rather than any particular object. These things are called static. The error message is telling you that you've written code that looks like it's trying to access a static thing, but the thing you're trying to access doesn't make sense to access statically - it needs a specific object to talk about.

Since you're implementing a method, you can use the special this keyword to mean "the object that my method was called on" - so your loop would look something like this:

code:
for (int i = 0; i < this.size(); i++) {

}
Or you can take it one step further - since it's such a common thing to do, if there's no ambiguity, you don't need to actually write out the this. As long as there's nothing else called size that you could be talking about, writing:

code:
for (int i = 0; i < size(); i++) {

}
will do exactly the same thing, because the compiler knows that when you say size() you really mean this.size().

samcarsten
Sep 13, 2022

by vyelkin
code:
	  @Override /** Return true if this list contains the element e */
	  public boolean contains(Object e) {
		  for (int i = 0; i < this.size(); i++) {
	           if (e = this.get(i)) {
	        	   return true;
	           }
	        }
	  }
Getting a new error: Type mismatch: cannot convert from Object to boolean

Kuule hain nussivan
Nov 27, 2008

samcarsten posted:

code:
	  @Override /** Return true if this list contains the element e */
	  public boolean contains(Object e) {
		  for (int i = 0; i < this.size(); i++) {
	           if (e = this.get(i)) {
	        	   return true;
	           }
	        }
	  }
Getting a new error: Type mismatch: cannot convert from Object to boolean

There's a small mistake in your if-clause. e = this.get(i) tries to assign what is returned from the List to e. You want to compare the two, so the operator is ==.

samcarsten
Sep 13, 2022

by vyelkin
now it's saying it must return a boolean. I made it return something already, what is it talking about?

Kuule hain nussivan
Nov 27, 2008

samcarsten posted:

now it's saying it must return a boolean. I made it return something already, what is it talking about?

What does it return if the list does not contain the Object you're looking for?

Adbot
ADBOT LOVES YOU

samcarsten
Sep 13, 2022

by vyelkin

Kuule hain nussivan posted:

What does it return if the list does not contain the Object you're looking for?

but if i change it to an if;else, the it doesn't iterate. Where should I put the return false?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply