JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

short is an integer type, as is 34, which I guess makes the difference

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, open an output file and it overwrites the previous.
Try opening the fileoutput stream in append mode, eg
new FileOutputStream(fileName, true)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, it's a cast/box too many in one step.

Float a = 3f; 
Float a  = (float) 3;
Float a = new Float(3);

all work

ps Khalid Mogul may be a great guy, but I would always prefer to find an authoritative source in the official Sum/Oracle documentation

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This code (assuming the class end brace was there somewhere later, and ignoring the non-issue of putting braces round a single-statement if) runs perfectly for me.
Maybe the problem's somewhere else in the rest of the code or the system config, not in this code.
Do other programs run ok?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, just in the Java VM's memory. But they can be written & read in a single method, so its no big deal.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

i interlocked my whole pieces[][] array into conditions and methods, now i cant get them out and manipulate them

Yes!
Seeing that is the first step to fixing it. Time to step back and do a little design before doing any more coding. Get that array out, clean up its design, and start with it as a single central shared thing that the methods can use.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you get all your results into some object (eg an ArrayList<Result>) then it's very easy to write/read the whole object to/from a file in a single staement using ObjectOutputStream/ObjectInputStream. That's probably the easiest way if you don't need to do record-based I/O

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Interesting question! My take is:
new B[]; populates the array with two <references to type B>, so the second statement fails because a new A is not a B.
ie: A is a reference to an array that can hold references to type A, but a[0] and A[1] (perfectly validly) hold references to type B.
But I'm not certain.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Casting to a subclass of the objects actual type doesn't work. The subclass may contain variables and constructor code that the superclass does not have, so simply pretending its a subclass instance isn't going to work.
You can create a kind of copy constructor like this:

public DateTime(Date d) {
        super(d.getTime());
     }

And create an instance directly like this:

DateTime d = new DateTime(Calendar.getInstance().getTime());
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just for my info, what OS and version did you test it on?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think I will go back a rewrite this from scratch... Starting with just clicking in a rectangle, then randomly in a rectangle, then randomly in a rectangle somewhere on the screen, etc.

It's very refreshing to see someone taking a logical step-by-step approach to solving a problem rather than the usual "I've written 500 lines of code and when i try to run them they DON'T WORK - panic panic.

However, you can't be far off - checkout line 16 where you have an x that should be a y.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Since Java 1.6.(something) there has been an implementation of transparent windows, which lets you display window content (eg text or shapes such as elipses) without the window itself being visible. It's going to be a full feature in Java 1.7, but right now you have to know where to look for it.
I wrote the following to check it all out under Win7 - it doesn't exactly do what you want, but it should give you the additional pieces you need...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.lang.reflect.Method;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;


public class DemoWindows implements ActionListener {

   public static void main(String[] args) {
      // create a new demo, and update it every 50 mSec
      new Timer(50, new DemoWindows()).start();
   }

   int phase = 0; // demo runs a number of consecutive phases
   int count = 0; // each of which takes a number of timesteps

   JFrame window1 = new JFrame("Java windows demo");
   JLabel text1 = new JLabel("<HTML><H1>This is a demo of some of the effects"
         + "<BR>that can be achieved with the new Java"
         + "<BR>transparent window methods</H1>"
         + "<BR>(requires latest version of Java)");
   JFrame window2 = new JFrame("Java windows demo");
   JLabel text2 = new JLabel("<HTML><center>Java<BR>rocks");

   int w, h, r, x, y; // parameters of iris circle

   DemoWindows() {

      // build and diplay the windows
      window1.add(text1);
      window1.pack();
      centerOnScreen(window1);
      window1.setVisible(true);

      window2.setUndecorated(true);
      setTransparent(window2);
      setAlpha(window2, 0.0f);

      text2.setFont(new Font("Arial", 1, 60));
      text2.setForeground(Color.red);

      window2.add(text2);
      window2.pack();
      centerOnScreen(window2);
      window2.setVisible(true);

      // …
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I've followed this with some bemusement. You say "This means I need to impose some artificial constraints on the range of values we allow.". So what exactly is wrong with this:

do {
  sdX = (int)Math.round(r.nextGaussian() * xCenter + xCenter);
} while (sdX < 0 || sdX >=w);
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Look closely at the difference between line 48 (wrong) and line 41 (better). The problem's near the beginning of the line.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK man. No worries.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@Katana.
Please observe the rules for DaniWeb. Do not give the O/P a coded solution to his problem - that will teach him nothing, except how to cheat at homework.
If you want to help, give him some guidance so he can learn to use the reference material, and will remember what he has learned.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes you do, you have at least 2 in your code already.
Around line 29 you try to compare namesEntered with the whole validNames array, but you should just compare with the i'th element of that array.
And note my previous about comparing Strings.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You need to loop through the array of valid names testing each one against the entered name. If you find a match you can stop searching. If you reach the end of the loop and you haven't found a match then the name is invalid.
Compare Strings by using the String equals method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

it is impossible to define a local (§14.3) enum, or to define an enum in an inner class (§8.1.3).

Java Language Reference version 1.6 section 8.9

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A member can't be both static and instance.
I think there may be confusion between references and objects here. In masijade's example
one is a reference variable. It is static.
new Integer(1) is an object. It's an instance of the Integer class.
Instance variables are not the same as instances of a class; I agree, using the same word for both is confusing.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hello, I've looked all over for the solution to this problem, but they all present a similar solution to this one and they all don't work.

Well, actually, they do, which is why you see it described so often. It's not the solution that doesn't work, it's your implementation of it.
So a little less arrogance and a bit more code (and a proper explanation of your problem) would help us fix your program

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This should do it. includes a sample prog that does exactly that.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"Enhanced for loop" - new in Java 1.5, and very good.
Read it as "for each double d in numbers"
It copies each element of numbers in turn to a new double, and executes the loop once for each.
You can use this for arrays, lists, all kinds of collections, eg:

ArrayList<String> someStrings = // create and populate the arraylist of Strings
for (String s : someStrings) {
   // this is executed once for each String in someStrings.
   // the value of each String is in variable s
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just have a look at any of the recent posts here - you'll find a constructor in most of the pieces of code. You can identify one because its name is the same as the name of the class, eg

class X{
   public x() { // this is a constructor
     // do some stuff
  }
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Its a special method that is called when a new instance of a class is created. It's used to initialise anything that needs to be initialised for a new instance. Every class needs at least one, and if you don't supply one the compiler gives you a "default" constructor that does nothing. You can define constructors with different parameters, depending on how you want to initialise your new instance.

Megha SR commented: very useful info +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

is that how it works,
1) create a reference to the interface (reciever).
2) use a constructor to allocate a reference (r) and make it equal to the object reference,
3)then call the interface by a method (sendText), which would put an argument/parameter in it
4)implement teh interface on a class(TickerTape).

Not really. Read norm's first post again.
This is a better version of your post:
1. create a reference variable that holds references to objects that implement the interface
2. Use a constructor to create an instance of a class that implements the interface
3. Call the sendText method on that object. The compiler knows that's ok because the class implements the interface and the interface defines that method.

Think of it as if the interface only exists at compile time. It tells the compiler what methods your object has, but at runtime the calls are always to actual methods in an actual object, not to the interface.

It's useful becuase you may have many different classes that share something in common and the interface is a way of defining that. Eg. The Java API defines an interface List which defines methods for adding, removing, retrieving (etc) items from a list of items. There are many classes in ther API that can hold a list of items (ArrayList, Vector, LinkedList, Stack etc), but they all implement, in their own different ways, the methods of the List interface. That means …

NewOrder commented: THANKS . i saved your comment on a word file +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is the class not serialisable because the writer didn't declare it as such, or is there some real reason why it cannot be serialised? Most classes that just contain ordinary data can be serialised - it's normally only a problem if they have handles into non-Java stuff in the operating system, or complex thread/state information.
If its the former you could try creating your own trivial subclass of the class and declaring that serialisable and use it instead of the original class.
Otherwize you will have to get() all the instance variables for the object and write them one at a time across the stream

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Perform you function in a Swing Worker Thread, not on the Event Dispatch Thread. I have no time to explain this now, but Google will help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

To expand on what norm said:
A char is a numeric primitive - its a 16 bit number that's used to hold a Unicode value representing an individual character in some chosen characterset. Primitives like char do not have methods.
A String is an Object that holds some text as a (private) array of chars and has many methods for working with them.
"Parse a String into a char" just doesn't make sense. A String of length n contains n chars.
If you want to get a char from some text input stream or String array you have to ensure that the String is of length 1, then get that one char from the String using its charAt method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

:icon_biggrin:

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is that a Swing JTextfield? If so, you can do this quite easily.
Read up on the javax.swing.Timer class
http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html
When you ask the Q you can then start a swing timer to run for 60,000 millisecs. When the timer completes, its run method will do whatever you wanted to do when the user hasn't responded. If the user answers while the timer is still running, just stop() the timer.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, lets have a look at that. In the meantime, you will still use the method I showed above to calculate the elapsed time & thus your points.
What form of user input are you using?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The Date class should help. It holds not just the date but also the time in mSecs. Its default constructor initialises it to the exact time it was created. Its getTime() method returns mSecs, so you can do this:

long start = new java.util.Date().getTime();
 // do whatever
 long end = new java.util.Date().getTime();
 System.out.println("Watever took " + (end - start)  + "mSec");

If you want to interrupt the user after 1 minute and stop waiting for him to reply, then that's another problem, involving multiple Threads. Do you need/want to go there?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

NormR1 wrote:

Your are expecting the constructor of your class to return a value. If you look at the syntax for a constructor you see that it does not return any value.

Sorry Norm, that's wrong. You don't get to specify the return value because a constructor ALWAYS returns the new instance.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I knew I'd seen this before...

Autoboxing uses Integer.valueOf, which internally caches Integer objects for small integers (by default -128 to 127, but the max value can be configured with the "java.lang.Integer.IntegerCache.high" property - see the source code of Integer.valueOf), so it is different from calling new Integer directly.

BestJewSinceJC commented: good post +5
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Because Integers are final, and can't change, maybe the compiler optimises by creating one Integer(5) and pointing both refs at it (like Strings). ? ???

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

its an instance of ArrayList<Person> ?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

readObject returns an Object, and given that it's reading it from a Stream at runtime, there's nothing else it can do. You have to cast it to the right class before you can use it fully. There's no way to avoid an unchecked cast in this case. You may want to use instanceof to check what kind of Object has actually been read before casting, but all that does is allows you to handle any errors gracefully.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Not sure what point you are trying to make here tong1, but the bald statement "Therefore neither casting nor the instanceof operator is needed" could easily mislead a learner.
toString is a special case because it's one of the very few methods defined for Object, and therefore inherited by every object in Java. Because, and only because, its defined in Object, you don't need to cast.
In the case shown by the O/P the instanceof and cast are necessary, as they are for all but the very few methods defined in Object.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Good question - the difference is because ints and doubles are primitive types, they are not accessed via a reference, and the cast creates, in effect, a new primitive and performs the conversion to initialise it. Cats and Objects are objects and are accessed by reference variables, and it's these references that are cast in that case.
So, to answer the question, the primitibve and the Object cases are quite different.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

chaospie:
What you say is basically right, but it hides an important point that can confuse learners. You cabnnot "cast an object". An Object's class is unalterable. What you are casting is the reference. Ie you are saying "this ref to an Object is really a ref to Cat". The object iteself was, and always will be a Cat.

NewOrder commented: Thanks for the (Cat)animal[i] explanation +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1. animals[i is an Object (as far as the compiler knows, because that's how you declared it), and Objects don't have a makeNoise() method. The code checks that this particular Object is in fact a Cat, the uses the cast (Cat) to tell the compiler that this particular reference to animals is actually a reference to a Cat. The compiler says "ok, if you say so..) and now knows that the makeNoise() method is valid. If you get this wrong then there will be an error at runtime when it finds out that animals isn't really a Cat.
2. Class D overrides foo(), so if the code on line 30 just read foo(t) that would call the same method - infinite loop. With super. it calls the version of foo defined in the superclass (B) - which prints "B", then prints "D". Not very useful, but safe.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your mask should print as ".*\.txt" -- you should only see the double backslash when entering a Java String literal. You have doubled the backslashes once too many in line 6, which should be replace(".", "\\.");

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi jon. The dir param isn't relevant to the problem as stated - he just wants to filter by file name regardless of directory.
the aragex(..) method certainly looks odd; is it intended to return a result or set a local variable, or both (in which case why?). And we have no evidence that this particular FilenameFilter is even being used at all...
hence my "try debugging it" suggestion.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

How about some basic debugging? after line 12 print the name and the strRegex to see if they are both what you expected.
ps does this mean that yesterdays dangling metachar thread is now "solved"?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your problem is the infinite loop I described earlier.
I don't know why you are using paint(...), but it is easy to read your image into an ImageIcon, add that to a JLabel, and use borderlayout to center the JLabel, all without the need to do special paint code.
ps: I'll be offline now till tomorrow (C.E.T.)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Instead of your getX/getY methods you can use JFrame's getWidth and getHeight, which will always be current. coil's suggestion is another good way to approach this, depending on what else you intend to do later.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Perhaps pass the PictureFrame instance to myImage's constructor as a parameter, rather than creating a new one? You will certainly need to do it this way if the user can resize the PictureFrame.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It looks like PictureFrame's constructor creates a new myImage (line 11), and each new myImage creates a new PictureFrame (line 7). Just saw your post - yes = stack overflow!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Divide an integer by another integer and the result is truncated to int. So 7/2 is 3 exactly.