JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In one post you describe starting with Main(string) which calls Main() sometime later. In another post you call Main() directly at startup. What exactly is the correct sequence starting from when the program is first run?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, in that case I see no reason why the second method is a constructor - why not just have it as an ordinary method called addMenus() or some such (I assume the call in the final line 55 is just a debugging thing, not how it will normally be used)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can't call a constructor like that - it's implicit in new Main(...).
I'm confused about your two constructors (not to mention the initialisation code in the main(...) method). Do you want to execute all the code in both of these when instantiating a new Main? If not, when do you want to execute each of them? Probably these should either be a single constructor, or at least one of these should just be an ordinary method

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Probably a typo - should be: for (String arg : args){

It's a "for each" loop, introduced in Java 1.5. You read it as
"for each String arg in args"
It sets arg to the first element of args and executes the following println code, it then sets it to the second element, executes the code again (etc etc). It's roughly equivalent to, but much neater than

String arg;
for (int i = 0; i < args.length; i++ ) {
  arg = args[i];
  System.out.println(arg);
}

It also works with any kind of Collection eg ArrayList in exactly the same way, and avoids the complexity and pitfalls of Iterators.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I can't see anything else in the code you posted. Maybe the problem lies elsewhere? eg Is there any kind of change listener on the U10 listbox...
Did you tracing the execution for the U10 case to see if it executes (only) the code you expected?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Dunno if this is relevant, but why do you call checkAge() 3 times in btnClassifyActionPerformed - is it possible that it returns different values after the whatever else happens between the first and second calls (eg clearFields())?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Since your method's code doesn't actually use the ActionEvent parameter, you could just call gameEndTurnBtnActionPerformed(null); from anywhere in your class (or anywhere else if you make it public).

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You declare and initialise cetvorougao inside your mouseClicked, so every time the mouse is clicked you create a new cetvorougao, which is garbage collected at the end of the method. You need to declare and initialise it inside the class but outside any method so there is just one instance of it that is shared between all methods and executions of any method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Would it be a big deal to replace those pass-by-reference params with a one-element array? Just thinking...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi Vernon
I'm curious. In a single-threaded environment, apart from the "swap" method, why is it so important to have a mutable Integer class? I've gone fat dumb and happy in SmallTalk and Java for 20 years without ever feeling the need for one.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 7 -you create a new vector every time you iterate thru the loop, which goes out of scope when the loop exits. Try creating the Vector before entering the loop.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, I never suggested moving the if tests out of the loop, just the messages

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Move the messages out of the loop:
Initialse failurePoint to false, set it to true if/when you find a failure point (do not set it back to false inside the loop!), the after exiting the loop test the boolean to see which Message to display.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Mark this solved please.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What you have is OK, just one thing missing...
you define a "users" variable, but you don't actually create an array, so the variable just contains a null pointer. You need something like
user Users = new user[99]; // creates an array with 99 slots for users

nb 1 Java convention is class names begin with a capital letter, variables begin with a lower-case letter - if you reverse this its confusing for other people

nb 2 Most people would use an ArrayList here rather than an array so they don'thave to worry about how big it is.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

... then in that listener just setVisible(true) for the JTextField - or am I missing the point of this question???

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you really want fine control of your layout, while retaining the platform-independence and re-sizing ability, the GridBagLayout layout manager has just about anything you can imagine. There's a bit of a learning curve, but it's well worth the effort. Tutorials and reference materials on the web as usual.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

google "java virtual machine"

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Add an ItemListener?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you remove the "int[][]" from that line then the remaining code
array = new int[rows][cols];
will refer to the variable "array" that you declared on line 9.
You can also refer to that variable from your other methods - ie because it is declared outside any methods it belongs to the whole class, and any method can use it. You don't need to pass it explicitly.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 66 instead of assigning the new array to the existing "array" class variable, you define a new local "array" variable which goes out of scope at the end of the method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Excellent!
Suggest you mark this thread "solved" - if you have any more problems you should start a new thread.
J

ps - better version of line 64

for (int i=0; i < db.length; i++) System.out.println(db);

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is well documented on the web,with lots of examples, but basically you need to split this into two methods:
1. In the first method you display the first panel and start a Timer with a 5 second delay
2. When the Timer fires it calls a second method in which you remove the first panel and display the second one.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Google the Swing Event Dispatch Thread - this is a standard problem that's well documented - but basically your ActionPerformed method blocks all screen updates etc until it has finished.
The right way to handle delays in a Swing GUI is to use a javax.swing.Timer - once again this is well documented on the web.

StevoLord commented: Quick and helpful. Thanks again +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

db is an array of Strings, so you can't store BankAccounts in it. Please review my previous post.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can't create a new array instance like that.
First you create a new array of BankAccounts, giving it a size. This will have all its elements initially null.
Then you can store a BankAccount into any of the array elements:

BankAccount[] accounts = new BankAccount[10]; 
// create array of 10 BankAccount references, all null

accounts[4] = new BankAccount(...  
// store ref to a new BankAccount in array element 4
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

line 6 instead of using 3 as the upper limit of your loop you should use the actual length of the words array. (It's like, but not the same syntax as, line 7, where you use the actual length of the String as the upper limit for the loop)

Latvija13 commented: Helped solve the problem! +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

To create a string of "n" stars:
1. Have a loop that executes n times and adds a star each time.
2. Start with a string that contains a lot of stars and use substring(...) to get the first n characters from that string.
3. I'm sure there are other ways as well.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK. It was the path to your jdk C:\jdk1.3\bin that rang alarm bells!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Would I need a for loop to run through each of the words?

Yes. So that's two loops - one loops through the (up to 3) words, then a loop inside that loops thru the letters of the word.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Are you really using JDK 1.3? That was released in 2000 replaced in 2002. The language had major upgrades with 1.5, and we are currently on 1.6
Time to update.
ps If you really want to give sample code to someone trying to learn the language, please don't use deprecated APIs.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can use the split method (String class) to split your input into an array of words, then check how many words there are, then check each word using isLetter

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ps - much shorter version of Color lookup by name...

// get a Color from a String containing a standard color name
   public static Color getColorByName(String colorName) {
      try {
         Field f = Color.class.getField(colorName.toUpperCase());
         return (Color) f.get(null);
      } catch (Exception e) {
         e.printStackTrace(); 
         return null; 
      }
   }
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Having looked v. quickly at your code I don't see why you are working with Strings for colors in the first place! Instead of

String color:
...
if (colgen > 0.8){
color = "red";
// now we have the problem of converting to an actual Color

why not

Color color
... 
if (colgen > 0.8){
color = Color.RED;
// now we have the actual Color ready to use
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If this isn't homework then it's OK for me to collaborate!
This uses Reflection - it's not optimised or polished, but it shows the idea

public static Color getColorByName(String colorName) {
      // get all the fields declared in the Color class...
      Field[] fields = Color.class.getDeclaredFields();
      for (Field f : fields) {
         if (f.getName().equals(colorName.toUpperCase())) {
            // found a field with name matching
            try {
               // return the (Color) value of the field
               return  (Color) f.get(null);
            } catch (Exception e) {
               e.printStackTrace(); // should not happen
            }
         }
      }
      return null; // no field has a matching name
   }
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm sorry I forgot to add this, but I am working in applet. Does this work in applet?

I can't see any reason why not.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is this a homework question?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think you will have to set up a HashMap to link the Strings to actual Colors, eg

HashMap<String, Color> map = new  HashMap<String, Color>();
map.put("Red", Color.RED);
map.put("Green", Color.GREEN);
// etc

...

String colorName = "Green";
g.setColor(map.get(colorName));

Or you could use Refection to access the Color constants by name.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't think there is any way to delete lines in the middle of a file. What you can do is to copy the file to a new file, only writing the lines you want to keep.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I agree with bibiki, at least as far as learning the basics of the language and debugging are concerned. The danger is that a beginner will find some program architecture that he can get to work for one particular exercise, but which actually represents a bad practice/habit that will be hard to get out of.
For example

so I make one "monster class" but multiples of it right (with different class names eg. class monster 1, class monster2 etc.?) then an array of "int's" for the x and y coords of each monster?

Now that will work, but it's a really bad direction to head in.
We have some responsibility to make suggestions like Ezzaral's about using classes properly - that way the OP will get the maximum value.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1. call the class Monster. Java class names normally start with a capital and describe the kind of thing the class represents. Each instance of this class is a Monster. Please don't call it mon1 - that will confuse everybody.
2. yes, x,y,width,height are the position and size of each Monster. In your case, right now, the widths/heights may all be the same, but they don't need to be.
3. Image is the Image you use to draw this particular instance of a Monster. For the first instance this will probably be mon1.gif (etc)
4. To understand the "null" look at the API documentation for that method.
5. Yes, the move method is for the animation. Call every Monster's move() method from a Timer so every Monster will move (eg) once every 50 mSec. I would make the dx,dy values instance variables of the Monster class, just like its x and y. That way each Monster has its own velocity. In that case there's no need to pass the right dx,dy valkues into each move() method. It's much easier that way.

To do (5) you need a list of all the Monsters. That's where the array comes in. All you need is a simple 1D array of Monsters, so you can loop thru the array calling the move() for each element (Monster) in the array. If you've covered ArrayList or Vector in your course these would be a better solution, if not just make an …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, but the pictures are rectangles aren't they?
If the monsters occupy just part of the picture, and you want to test for collisions between the actual monsters, rather than the whole pictures, then that's a pretty difficult, and slow, thing to do.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Are your monsters etc rectangles? If not, this is quite hard.
Are they Java Shapes (instances of the Shape class) - if so there's a method in Shape that does that.
For rectangles you can easily see if they touch/overlap by comparing their top/bottom coordinates to see if they overlap, then their left/right coordinates ditto. If they overlap both vertically and horizontally then the rectangles overlap.
Draw a few cases out on a sheet of paper and write in the top/bottom/left/right coords for both rectangles - you should be able to see how to compare them to get the result you need.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Glad to help! Mark this as "solved"?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi Akill - I think he wants to use keys to set things moving, then have them run on on their own - so the move code needs to be in the timer. Set the flags, speeds or whatever in response to the keys but update positions etc each time the timer fires. The paint method should really just re-paint the screen based on the latest positions etc. Moving things in the paint (like it does now) is bad because you don't know when or how often paint will be called.
The start/stop timer was my first suggestion, trying to keep thing s simple for a beginning. As it develops I would leave the timer running and accept that it may do little or nothing if no objects are actually moving.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I posted some suggestions about how to re-structure your code earlier, and I'm going to stick by that advice

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Personally I wouldn't try to add major new features until the existing parts are working. I posted some suggestions about how to re-structure your code earlier, and I'm going to stick by that advice. But it's your code, so good luck.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1 mSec timer is crazy - that's 1000 screen updates/sec. Start with something slow enough to see exactly what's happening (eg 1000 mSec), then speed it up until the movement is smooooth (eg 60 mSec).
I'm off now until tomorrow. good luck.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

To clarify: the program structure goes like this:

You have some moving objects to draw in your window.
Each object has a current position, speed & direction.
You have a paint method that paints all the objects at their current positions.
When the user presses certain keys you change the speed/direction
Each time the timer runs you update each object's position according to its speed & direction.
Each time you change an objects position you call repaint so that the window gets updated (via the paint method).

This overall design will allow you to have almost unlimited scope for many objects, complex movements, varying speeds, etc.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I was referring to the coordinates at which you will draw the bullets or whatever when you repaint() causes your paint method to be called - so each time the timer fires you update the variables that control the position of the bullet, then your paint repaints it (just once each call) at that position.