Malcolm Whitely

Greenhorn
+ Follow
since Feb 06, 2003
Merit badge: grant badges
For More
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 Malcolm Whitely

Thanks Jamie and Ashish for your replies. Ashish, unfortunately, I am guilty of practicing what you mentioned as bad programming practice since I break my loop by checking for a Null reference. At present it seems to be the easiest way to get around determining when to stop looping
I have since solved the issue with the sorting by storing my frequency data in a hashmap. I then create an array of size hashmap and populate it with the hashmap's keys.
Thanks to everyone for their help.
PS. No, I don't know when to toss exceptions and throw errors. Mind explaining it to me
21 years ago
Hi there Maulin,
I did a quick test and resized my array to 19 (number of actual used elemented) and the sort worked fine. However, when there are null elements in the array, neither algorithm works properly. I hope there is a way around this.
Maulin, do you mind if I email you my code since it is quite long and there are a fair number of interfaces in it.
Thanks again
21 years ago
hello everyone,
I'd like to use vectors in my program instead of arrays because of the advantages of them being dynamic. Here is my question:
If I originally had an array of type "typeX" declared as such:
typeX[] array = new typeX[10];
How could I declare a vector of type typeX? I've tried:
typeX Vector myVector = new Vector();
but that doesn't seem to work.
Any help would be appreciated.
Thanks
21 years ago
Thanks for the reply Ashish. Your code works great, but I am unable to see how different it is to the code posted by Maulin.
I've spent a few hours so far comparing my code with yours and am still frustrated. Could it be that my array has a size of 100 whereas only about 20 elements are used?
Thanks for your time.
21 years ago
Thank you for the replies.
Maulin, I tried the code you suggested and it compiles correctly. The only problem is that the sorting does not seem to be working properly.
My unsorted array looks like this (word/frequency):
this 2
is 3
a 3
test 2
and 1
sentence 2
to 2
see 1
if 1
the 1
code 1
works 1
properly 1
line 1
three 1
which 1
also 1
happens 1
be 1
When sorted by frequency, the result comes out as:
and 1
this 2
test 2
sentence 2
is 3
a 3
see 1
if 1
the 1
code 1
works 1
to 2
properly 1
line 1
three 1
which 1
also 1
happens 1
be 1
Sorting by word gives a different order, but like the frequency sort, the first two records appear to be sorted properly (ie "a" and "and" are the first two).
I'd be thankful for your help
Take care.
Malcolm
21 years ago
Hello,
I currently have an array made up of objects, with attributes 'word' and 'frequency'. After populating the array the user is asked if they would like the array to be displayed in alphabetic order (ie by word) or by decreasing frequency order (ie by frequency).
When I applied the Arrays.sort(object[]) method to my array I got the ClassCastException error, so I read through the API and it seemed to me like I needed to use the Comparable interface to be able to sort the array.
I've implemented 'Comparable' in the object class and defined a CompareTo method, but don't know where to go from there:
Here is my object class code:


and this is a method from my main class (note that words[] is already populated wuth unsorted data:

Any help would be greatly appreciated.
[ February 23, 2003: Message edited by: Malcolm Whitely ]
21 years ago
That worked great Chad! As you suggested, the flush() method, which I have since read about, was the reason the data was not written to file.
I have noticed that the Java API is not as kind to beginners as Microsoft's MSDN which elaborately explains everything. Is there a book, more of a reference one with breakdowns of packages and classes, that you would recommend?

Thanks and best regards.
Malcolm
[ February 21, 2003: Message edited by: Malcolm Whitely ]
21 years ago
Thanks again for the reply. It cleared out a few things for me.
At the moment, I've got a main() method and an output() method, which writes the contents of an array to a file. I've tried passing in my PrintWriter object from the main() method but the writing never seems to occur from within the output() method.
Here is my code:

System.out.println() works fine in output() but pwrtr.println() doesn't. Another thing worth mentioning is that "Hello" which is executed in main() is written to file.
Thanks for the help. I really appreciate it.
[ February 21, 2003: Message edited by: Malcolm Whitely ]
21 years ago
Thanks for your reply. Am I right in thinking that PrintWriter can be used alongside the println method to print characters on screen? If so, then how would the code differ?
One other thing, I tried the code you suggested but get a NullPointerException when I try to write a a string to the output file.
Thanks in advance and looking forward to hear from you.
[ February 20, 2003: Message edited by: Malcolm Whitely ]
21 years ago
Hi everyone,
I am trying to declare a new PrintWriter object as so:
PrintWriter pw= = new PrintWriter(OutputStream out);
which appears correct, but on compiling I get the 4 error message including:
") expected" and "cannot resolve symbol"
I've also tried "PrintWriter pw = new PrintWriter(Writer out, boolean autoFlush)" but to no avail.
Hope someone can help.
21 years ago
Hi there,
I currently have a class that extends Thread and loads some sort of animation. Can anyone recommend a container with size/color attributes which I would be able to add the instance of the class to? I have tried a canvas and a panel but with no luck. I get a "Incompatible Type for method" error every time.
Thanks.
21 years ago
Hello everyone,
I've got a class that extends Thread and performs a simple animation by redrawing an oval every fraction of a second. What it actually does is that it sets the circle's color at the previous location to white and then recreates it with its normal color at the new position.
I have tried to add this to a canvas, or any other container for that matter, but to no avail. Note that I am not using the repaint() method.

I've defined a canvas in my main class (which extends Applet) as such:

I'm simply trying to add an object of type Graphics 'g' (from my other class) to the canvas. I've tried myCanvas.add(g), add(myCanvas); but doesn't seem to be working.

Any help would be greatly appreciated.
21 years ago
I've just set up a scrollbar in my Java applet. I have the following two questions that I hope someone could kindly help me with:
1. How can I determine the width or height of a scrollbar? It doesn't seem to be among the arguments in a Scrollbar's initialization line.
2. I'm comparing the process of drawing a scrollbar in Java to VB and would like to ask if there is any way of graphically drawing a scrollbar or any other object for that matter in an applet?
Thanks in advance.
21 years ago
I see All is clear now. I'm currently trying to make the circle bounce back in the other direction. If I need any help I'll be sure to post back.
You've been a great help.
21 years ago
Thanks a lot Michael for your reply. It worked great! I do have a quick question though about the logic involved. Shouldn't x++ and ++x both increment the value of x?
Hope you can help me out with this?
Thanks again for your prompt reply.
21 years ago