Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!

Michael Duff

Greenhorn
+ Follow
since Mar 13, 2010
Ireland
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Michael Duff

Rudradutt Joshi wrote:I guess you will not encounter with the problem Fred mentioned with the above notion, where the dataMap is reiterated once after string parsing completes, so its one time operation in the end.

And you can do all the beautification for displaying data by enhancing toString for the wrapper object, and to further by a method to print map object the way you like.


I'm not sure I quite understand what you said. I tested your code myself, and when I print the HashMap it says, for example, if I write "abba":

a = [Percent = 25.0, occurrence = 1]

b = [Percent = 25.0, occurrence = 1]

a = [Percent = 25.0, occurrence = 1]

b = [Percent = 50.0, occurrence = 2]

a = [Percent = 25.0, occurrence = 1]

b = [Percent = 50.0, occurrence = 2]

a = [Percent = 50.0, occurrence = 2]

Wouldn't that mean I store a lot of unnecessary information in the HashMap? It would be a nightmare if I needed that information for something. I would only like two things to be stored, namely a = [Percent = 50.0, occurence 2] and b = [Percent = 50.0, occurrence = 2]. Is it possible to do that with the code you provided?

EDIT: Okay I made a silly mistake, I printed the HashMap inside a for loop that's why I got the information I mentioned above! I assume after each iteration it wrote information about what's in the HashMap at that specific time.
8 years ago
Thank you for that code, I had something similar planned at one stage. But when I try to implement that code I stumbled upon the problem that Fred Rosenberger mentioned earlier, ie. the letters get updated all the time and the Map becomes a mess when you try to print it out.
8 years ago
I found out how to get the percentages of each letter:


Now, if I would want to use those percentages for comparison would it be better to store them somewhere? (NOTE: I only need to analyze a text once).
8 years ago

Campbell Ritchie wrote:
I am not sure I understand the bit about a List. How would a List help with counting?



The thing about a List is, in the end, I would like to have a structure where HashMap has information about a certain character, how many times it has appeared in the input text and how much % of the input text that letter represents. I'm just having a hard time figuring out how to do it .

Thanks for all the help so far.
8 years ago
Okay I have figured out a more clearer version of my lettercounter


Now I would like to make the <Integer> in the HashMap into an ArrayList<Integer>, is that possible? If so, how would the incrementing of an existing character work?
8 years ago
Hello

I am in progress to write a program that analyzes a text-input. It should print out the letters, occurence of a letter and its percentual value of all letters in the text ("abba" would mean a and b have 50% each). Now I have decided to use HashMap where the letter would be the key, and the value would be a Vector holding the count of the letter and its percentage (is that even possible in a single Vector?). But I'm kinda stuck in the implementation phase, I can't see what I'm doing wrong.

When I print out my HashMap it returns the letters (in scrambled order), and the vector which is same for all letters (what I want is just the letter, its count and a percentage). Here is what I have so far:


[edit - i moved the comment on line 22 up one line to prevent one REALLY long line]
8 years ago
I figured out a solution, by introducing a counter variable I can create an object of my Ship class every second (every 10 ms the counter grows by 1, and when it reaches 100 the counter resets, meaning every second). I don't know if this is elegant but it works like I wanted it to!
In my Java applet I want infinite number of enemys ships to pass from my top left corner and the ships will work like space invaders (ie come closer when hitting the side of applet). But I'm stuck on implementing the feat where infinite amount of ships would arrive in a set interval. The ship objects are added to an ArrayList, but I don't know how to achieve my goal in a correct way The following code causes certain amount of ships to appear but they slowly melt into eachother after each row jump, and they aren't arriving continously. Anyone could give me some hints?



Edit: following code in another method would provide the same result. What I want is continuous flow of enemyships from a side, not having them all appear at start (which these codes do).

Any ideas/suggestions would be appreciated
Thanks for the tip, but I'm looking for something more simple where I could use Future, something like this:




But I'm clueless how to properly implement that functionality into my class in the first post (and my two other classes). Do I need to send the objects to the threadpool in the constructors of my classes?
Hello

I have 3 classes, each one which implements Runnable. What would be the ideal way of using Executor/ExecutorService so I don't need to use Thread.start() method in each one of them?

Here I have one of the classes
I just made it like that as an example (not very good, I admit).

What I'm interested in is if I can change , for example , a JLable text from both of my classes (Is that even possible?).

Cheers,

Michael
9 years ago
I wondered if this question belongs to the Swing/Gui part of the forum, but the question itself probably belongs here. If not I'm sorry for posting it here .

I have two classes, and I want to set a specific value in my JTextField and my JLabel in my GUI. Is there any way I could use the setText functions in my other class too, or do I have to rely on returning Strings to the GUI class (which I'm not very fond of, because my other class will be quite complex)?

GUI class:


And in my other class I have

9 years ago
Good one, now I know it does in fact save and load the HashMap. The problem must be related to JComboBox, which doesn't seem to save/load the values I have added to it.

Edit. I found the solution! In my ClickListener method where I load the HashMap I had to do a for each loop over the keys so it would actually repopulate the JComboBox.
9 years ago
I got a HashMap where I put my keys and values in, but whenever I save and try to load it next time I start the program the "information" in the HashMap seems to be gone. I do get the print confirmation I had in the code (which says I have saved or loaded the file), no errors, which makes me think I am doing something wrong with my HashMap.

I am using Swing where I have buttons assigned for the save and load methods, and another button with which I am adding keys and values into HashMap. I have the keys from the HashMap in a JComboBox, which I would like to be able to save and load if I wanted to.

Cheers,

Michael
9 years ago