JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just noticed - you declare new and b variables in the Draw class, but the listener will refer to the a and b variables in the PlotLuckPanel class. Perhaps you should delete the duplicate declaration in Draw?
(ps using code=java tags and getting the indentation right will help to see what's in what scope)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Try sticking a couple of System.out.println("step n"); type statements in the listener and the paint method so you can see when/whether they are getting called. That should help you to pinpoint where it's failing.
ps: not sure why you have a Draw graph = new Draw(); in the listener - it doesn't seem to be used for anything?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

toUpperCase in the Char class is a static method, so the correct way to use it is:
charArray = Char.toUpperCase(charArray);
You seem to be getting into difficulties with String vs char. It may be bettert to convert your String to a char array, do the selective case changes in the char array, then create a new String from the updated array.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Excellent! Many thanks for that.
James

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'd have to see the code, but it doesn't seem strange to me...

Thanks Vernon, that's reassuring! Still seems a bit of a coincidence that the numbers are quite so "memorable". I'll post the code after this, and maybe, if you have a minute, you could cast your eye over it for stupidities? It's barely more than a dozen statements, nothing too tedious. Thanks
James.

double estimatePi(int decimalPlacesRequired, int maxIterations) {
  // pi = 4 - 4/3 + 4/5 - 4/7 + 4/9...etc.

  double tolerance = .05;    // ±.05 = correct to 1 decimal place
  int decimalPlacesOK = 0; // current level of success

  double p = 4; // current estimate for Pi
  int sign = 1; // alternating sign of terms in series
  for (int i = 1; i < maxIterations; i++) {
    p += (sign = -sign) * 4.0 / (i * 2 + 1);

    if (Math.abs(Math.PI - p) < tolerance) {
        // now accurate within tolerance
        decimalPlacesOK++;
        System.out.println("Iteration " + i + "  " + p
		+ ", correct to ± " + tolerance + " ("
		+ decimalPlacesOK + " decimal places)");
        tolerance /= 10; // reduce tolerance for another decimal place
    }

   if (decimalPlacesOK >= decimalPlacesRequired)  return p;
  }
  return -1; // failed to find good enough estimate
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just as a matter of curiosity, I tried putting together a neat/compact version of this for my own amusement, and was surprised to see a strange pattern in the number of iterations needed for different number of decimal places correct.

Iteration 19  3.09162380666784, correct to ±0.05 (1 decimal place)
Iteration 199  3.136592684838816, correct to ±0.0050 (2 decimal places)
Iteration 1999  3.1410926536210413, correct to ±5.0E-4 (3 decimal places)
Iteration 19999  3.1415426535898248, correct to ±5.0E-5 (4 decimal places)
Iteration 200000  3.141597653564762, correct to ±5.0E-6 (5 decimal places)

Has anyone else tried this and found the same thing, or do I just have some stupid bug in my code?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

(String)(Vector) looks unlikely. Whatever it is that follows is being cast to Vector, then immediately cast to String, which won't work.
Try defining your Vectors with the appropriate types ,as in Vector<String> sv = new Vector<String>(); so you don't have all those horrible casts.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

probably easiest to get the files directly, and loop thru them, as in

File files[] = directory.listFiles();
for (File f : files) {
   // do whatever you want with each  File f
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What is your directory name? Looking at the first code you posted it should be C:/Users/user/Desktop/java. Did you create a sub-directory called test?
You can call directory.exists() after your new File(...) to see if you've got a valid directory reference.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That sounds to me like it's probably the fastest possible algorithm, although it will need some cunning code design!
I was thinking about an approach close to the spirit of digit-by-digit decimal processing, which, although probably a bit slower, would be easy to undersand and code. It goes like this:
We want x^Y. Let's write Y in terms of its digits as:
Y = a + 10*b + 100*c + 1000*d ...
so x^Y is
x^(a + 10*b + 100*c + 1000*d ...)
= x^a * x^(10*b) * x^(100*c) * x^(1000*d) ...
= x^a * x^10^b * x^100^c * x^1000^d ...
= x^a * x^10^b * (x^10)^10^c * (x^100)^10^d ...

which is, in pseudo code, is something like:

res = x^y.digit(0) ; 
power10 = x; // running value of x^10^n
for (n = 1; n < y.numberOfDigits; n++){
  power10 = power10^10;
  res *= power10^(y.digit(n));
}  
return res;

Where all the ^ operations are now <= ^10, so can be done with simple multiplication. (If you optimise ^10 (= ^8 * ^2) you can do it in 4 multiplies.)

Have fun!
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is there a harder/faster way to do the power function without looping multiply potentially thousands of times??

Dunno - not a mathematician! Maybe you can pre-compute the 10th power, use that to pre-compute the 100th power, use that to pre-compute the 1000th power etc then use the digits of num2 to determine how many times to use each of those results? If that works it should scale nearly linearly with the number of digits?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So, I understand that you need to keep each digit separate and have algorithms that loop along the digits (no matter how many there may be) to give the answer. For the multiply - write down two multi-digit numbers on a piece of paper and multiply them together by hand. The process you just used is the algorithm you need!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can create Java File that refers to a directory just like you can for a file, ie new File(directory path). Once you've got the File object for the directory, you can use its listFiles method to get all the files (optionally matching a filter for .txt files only) in it, so you can then loop thru them as you wish.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

for (int i = 1; n * n > Math.pow(2,i); i++) {
...
}

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The parameter for the MyTableModel constructor needs to be the array of Adults that you populated earlier. In your last code fragment that was called MyArray I think. memAd is justa simgle Adult, not the aray of Adults.
As for the JTable, you also need to do all the usual graphical obect stuff - add it to a form, set its position etcetc.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You seem to be getting into problems with using names in the wrong places. To keep it simple, each variable belongs to the class in which it's declared, and won't be accessible from any other class just using its name. The right way to do this is to pass the data from the first class into the next class by passing it as a parameter. So in your example, you are doing the right thing by pqssing your data into the model by making it a parameter on the constructor. Loks like your latest problem is that the class where you create the JTable doesn't have access to the data (memAd) in the first place. You should do the same thing, and pass the data to that class via a parameter in its constructor, or create the model in a class where you DO have access to memAd and pass the model into the class where the JT

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think your last big if test should be all && not || - ie (word not in file 1) AND (word not in file 2) AND ... but I'm not sure. It would help if you used a code=java tag rather than just a code tag so we get proper formatting and line numbers

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

JTable really is the right way to do this, so it's worth the one-time effort to learn about them!
Here's a quick sample of what you need to create a suitable table model (I haven't compiled this, so expect the odd typo, but it should be good enough to get you going right).
It has a constructor to which you pass your Adults array, as in new MyTableModel(MyArray); and the rest of the methods just implement the mandatory interface required for a TableModel

class MyTableModel extends AbstractTableModel {

    private String[] columnNames = {"Name", "Date ...

    private Adult[] data;

    public myDataModel(Adult[] a) { // pass in data array to constructor
	data = a;
    }


    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        Adult a = data[row];
        switch (col) {
           case 0: return a.getName();
           case 1: return a.getDate();
           ...

        }
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

}
jackiejoe commented: Very helpful individual, patient and knowledgeable +1
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

.. I always wanted to visit Mauritius... mine's a pint!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I the new variable is called theServer, but your call refers to cs1 (which is the variable in the main class, not the one in the GUI). so it would be
theServer.sendData
You original version of the constructors was right, so is the commented latest version, but you may find a problem when netbeans regenerates its code unless you stick to the first version.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The new constructor for ServerInterface needs to look like this:

Server theServer; // Add a variable to the GUI class to hold a reference to the server
public ServerInterface(Server s) { // add a new constructor that takes a server reference as parameter
theServer = s; // save a ref to the server
this(); // call the original (default) constructor
}

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you create the GUI, pass the server instance (cs1) as a parameter to the GUI's constructor. (You need to write a very short constructor that just saves the server reference then calls the default constructor) That way the GUI will have a reference to the server instance and will be able to call its methods.

Server cs1 = new Server();
 new ServerInterface(cs1).setVisible(true);
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Good morning neutralfox. Wow, that server code really has been cleaned up!
All you need to do in response to the button being clicked is to get the text from the input field and pass it as the parameter to the server's send method. The only problem I see in your code is that the send method is an instance method, and you were trying to invoke it as a class method. ie instead of Server.sendData it should be cs1.sendData.
ps. Maybe a good time to mark the previous thread as "solved"?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, break the problem down into small pieces and fix them one at a time. I suggest you leave the whole GUI thing alone until the server and client code are working. Hard-code calls to the server & client code, and use System.out to see what's happening. When it all works properly, add in the user interface.
If your server code is entering the while loop then the connection HAS been made, so you're doing well. You still probably have the problem I described in my last post - so either get the client vto send something to the server, or simplify the server so it sends a response without waiting for the client to send something.
OK, it's now time for pre-dinner drinks here in France, so I'm going to sign off until tomorrow. Good luck, and remember - one thing at a time!

Ezzaral commented: A lot of patient help in this thread. +19
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

could you explain to me how to write to the server from the client using the "PrintWriter output".
.

Same code as server sending to client, but other way round!

Did you add the System..printlines as I suggested to see whether the server gets the connection request?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is the server receiving the connection request?
Did you try using your browser as I suggested to check that the server is working properly?
The server waits for a line of input from the client before it sends its response, but the client code never sends one, so the server never responds, and the client waits forever.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

NO don't give up now! You're almost there!
The exception is caused because previous test runs failed to close the socket properly and so it looks like it's still in use next time you try to open it. For as quick and dirty fix change the socket address to 5001, and keep changing it each time you get this problem. Fix it properly when your code is working a bit better. DON'T GIVE UP.

jasimp commented: Good advice. You have been very helpful in this thread. +9
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry, don't know. Did you put the printStackTrace() in the catch like I said before? Try putting a few System.out.println("step 1 (2,3,4)"); statements into your run method so you can see exactly where it's getting to.
Try making a connection and see what happens. Just type
http://localhost:5000/ into your web browser. It won't do anything useful, but your code should accept the connection and echo the HTTP request for you.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In your code, you set the status to ON or OFF when you process a connection from the client. Without a client to connect, those statuses will never get set, because the run() method will never get past the ss.accept(). Remember, the accept() method will just sit there waiting until there is a connection. Maybe try setting the status to "waiting for first connection" in the run() method just BEFORE the ss.accept()

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

At a guess - your call to getServerStatus() comes immediately after the start(), so it's executed in the current thread immediately. Only when that thread finishes executing will the new thread get to run and initialise the server.
If you declare String serverStatus; as String serverStatus = "Not yet started"; it may become clearer.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Briefly, you need to run your createServer in a new Thread. One way to do that is to make your server class extend Thread, and change the name of the createServer() method to run(). You can then call cs1.start(); to execute the run() method in a new Thread.
Have a look at the API for the Thread class.
There are also lots of good introductory materials for Java Threads on the web.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ss.accept(); blocks its thread waiting for an incoming connection. Because you run that on the Swing thread everything gets blocked, and the whole program freezes until an incoming connection is received. You normally spawn a new Thread to run the accept() in.

ps. Its not a good idea to have a catch(Exception e){} because that guarantees that you won't see any errors! Put an e.printStackTrace(); in the catch so you cen see what's gone wrong after an error.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Check that your code for Couple has a setVisible(true) in its constructor

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Looks to me like the local var is 2 chars vs ~20 for the static value?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe because your txt value exceeds the 16 char max size that you set for the text box?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, SwingWorker is great - does exactly what you need, but it's new to Java 6 (JDK 1.6) so may not be on all systems just yet.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Executing a long-running process in the Swing thread is a big no-no, for exactly the reasons you have discovered - ie the whole GUI hangs up while the process executes. Your only solution is to run the FTP process in a separate thread. Just place the call for your FTP in a run() method, and start a new Thread with it. You'll find a zillion web sites with examples, but if you get stuck I can give you more details.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you want to read/write an object via writeObject / readObject the object's class must implement the Serializable interface. It's a bit weird, because that interface doesn't define any methods! It's a sort of flag to Java that if you define the class (Archive) as implements Serialiable then you expect instances of that class to be able to be written/read from object streams. If your class's instance variables are only simple things like Strings you don't have to do anything else, but if they are more complicated you MAY have to define methods to convert them to a form that can be read/written to a stream. If you're not sure, just add the implements Serialiable and see if that works first.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, try it

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I suspect that 11 factorial is greater than the maximum value of a Java int (about 2,000,000,000), so you'll have to use a different data type (eg long) to go bigger.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So in this instance it did, that doesn't mean it always will. Did you look at what getAbsoluteFilename returns? Does it return the "long" name, or the Progr~1 type name, in this instance?

Yes, it's the long name, with embedded blanks

I accept your first two points - I think we've just got a difference of emphasis, that's all.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Parsing a Jar is not hard however. ...

I agree, but it's not easy for a beginner.

P.S., by paths I mean the PATH environment variable. If that does not contain the proper path, to the proper bin directory "javaw" does not work.

Yes, that's right. I don't know how common that would be with default installations.

Also, if there are spaces in that filename (or the path leading to it) be careful. The spaces (due to the way Runtime.exec operates) will cause problems, whether you use quotes or not. Those quotes are evaluated by system shells, and runtime.exec doesn't have one.

I tested the code with a file on my desktop under XP, where the path includes two spaces. With the extra quotes around the absolute path this does work.

James

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi masijade.
Sorry if the tone of my previous post seemed aggressive, please don't think I'm just here for an argument! You're certainly right about paths (as I said in my previous post), but this will be an issue for any code that tries to access an external jar file. I guess we'll have to agree to disagree about Runtime :-)
Certainly I think the following is a lot easier than parsing a jar and using a custom class loader.

try {
	JFileChooser fc = new JFileChooser();
	fc.showOpenDialog(null);
	File file = fc.getSelectedFile();
	String cmd = "javaw -jar " + "\"" + file.getAbsolutePath() + "\"";
	Runtime run = Runtime.getRuntime();
	Process proc = run.exec(cmd);
} catch (Exception e) {
	e.printStackTrace();
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The Runtime API itself is not system dependent, but the command you execute must be a valid command for the system it's running on, so you may need to test the OS to see whether it's javaw or javaws. Similarly the file path/name will vary according to OS, but that would be true regardless of the approach. Having said that, exec ("javaw -jar thing.jar") should work on any version of Windows with a standard Java installation.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your original approach, using Runtime should work just fine, and has got to be the easiest way to go. What exactly are you passing in as cmd, and what exactly is the error/exception?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This http://djproject.sourceforge.net/ni/index.html may be just what you're looking for.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This will work the way you want if you put your loop in a different thread from the Swing event thread. Replace your call mp.RandomAnimation (); in actionPerformed
with

new Thread(
  new Runnable() {
	public void run() {
		mp.RandomAnimation ();
	}
  }
).start();
VernonDozier commented: Perfect. +11
Ezzaral commented: Bingo. +18
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In the constructor of Away you need to copy the bankaccount that's passed in to the local private variable bankaccount:

this.bankaccount = bankaccount;

ps Java naming convention says it should be called bankAccount

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What value is assigned to cmd when you call this?