JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you send a file it goes in two stages
1. You read the file into an array, one block at a time
2. You write those blocks to the socket
... similarly at the receive end.
If you don't have a file then the data will be in some kind of buffer/array in memory, so you just skip step 1 and write the array to the socket.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You could try creating the input stream before you write anythung to the output stream - maybe there was a response but you missed it before you opened the input stream?
Anyway, add print statements at key points in that code so you can see exactly where it is hanging - exactly which statement?

You may also want to get the errorStream and see if that contains anything

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That code you found on the internet looks like a longer, more error-prone variant of the code I posted for you, and your first small code sample uses. It's not good. I strongly suggest that if you get code off some random internet site, and you don't understand exactly how every line works, then just delete it.
Sending different files: use the same code with a different File as input! I don't know what methods are availabe for selecting Files on Java ME, sorry.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's a standard problem! Easiest solution is to use substring to take all the characters from the string except the last one.

jalpesh_007 commented: Solved +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Once you have your socket connection you can send the file over it by simply reading it in sensible sized blocks and writing them in a single stream of bytes to a DataOutputStream via the socket.
This is the standard code for doing that kind of thing, which you will find all over the web:

    DataOutputStream outbound = new DataOutputStream(clientSocket.getOutputStream());
    byte[] data = new byte[4096];
    int count;
    while ((count = file.read(data)) != -1) {
        outbound.write(data, 0, count);
    }
    file.close();
    outbound.close();

similarly at the receiving end you open a DataInputStream via the socket and do whatever you need with the inbound bytes

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

the String class is automatically copied if you use get() from the ArrayList

Have you any reference or demo for this? I would be very surprised if get did anything other than just return a ref to the original object.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

line 34 you initialise aspStr as null, but then on line 37 you try to append text to it. You can't append anything to a null. Initialise it to an empty String ie "" instead.

jalpesh_007 commented: really good suggestion +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can get the contents of any cell in the table, so you just need a loop that accesses every row in one column. When the data is changed you can add a listener to the table that will be called so you can recalculate the sum.
Study the API documenation foir JTable and you will find the info you need. (Plus, you will develop essential skill in using the API doc.)
Then try to put what you have learned into code. If/when you get into difficulties you can post your code here and someone will help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The second [0] is the error.
If there was a 2D array then that would be a valid index, eg

int[][] a = {{1,2,3},{4.5.6};
a[1][2] == 6

but because its a 1 D array ( new int[]{0} ) then the second index is invalid.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The {} in this context define a literal array, as in

int a = 1;
int[] b = {1,2,3};
int[][] c = {{1,2,3},{4.5.6}}; // 2x3 "2D" array (array of arrays)

so {{1}} defines a 2D array 1 row, 1 column, containing the single value 1.
{{1}}[0] selects the first element of the first/outer array, ie the array {1}
So
int[][]{{1}}[0] defines a 2D array, selects the first element of the outer array, and thus returns the inner array {1}, which is a valid value for int[] it

(ps I wrote 2D array, but this isn't exactly correct, Java [][] arrays are arrays of arrays. The inner arrays can al be different lengths, so the [][] does not have to be rectangular)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That was the quick tuto!
I have no time now to write a new one, maybe later... ?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes there is:
http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html
... but when you read that multi-part tutorial you will discover that (a) it's far from easy and (b) depending on what the user does it still may not work as you want.

Trust me, it's easier to go with the subsystem designed specially to deal with all this - just follow the link from my first post and set your bindings for WHEN_IN_FOCUSED_WINDOW

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Probably because the keyboard focus is on another compoenet.
Better to use key bindings to avoid this
http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Connect server to client seems a contradiction of the terms client and server.
Once a client is connected to a server they can both send info to each other.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
public class QueueImpl<E extends Comparable<E>> 
    ...
    private E info;

So info is type E where E is defined as <E extends Comparable<E>> so, by definition, it does have a compareTo method.

@shanki himanshu "it's not working" is not enough info.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't understand why the multiple port numbers. YOur server should listen on a single port, and all clients connect to that port.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Like I said: The way your classes are defined the info variables will always be of a class that has an implementation of compareTo. If your info variables are from the standard Java API (eg Integer, like the code you posted earler) then you do not need to write a compareTo method.
You code your call to the method just like I showed. That method returns a value that you can test in an if test. The API doc tells you all about that return value.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The Comparable interface API doc defines the compareTo method in terms of its parameter, return value, and necessary logical function (in great detail!). Yes, every class has its own implementation, but I would certainly consider the Comparable interface doc as "defining" the method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In the definition of the Comparable interface. See the Java API documentation.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The info field in your Node class is defined as type E, and the class definition for QueueImpl restricts E toE extends Comparable<E> You can confirm that yourself by trying to create a QueueImpl for a type that does not support Comparable (eg new QueueImpl<Thread> - the compiler will reject it.
That means that whatever type info is, it will always support the compareTo method
So you can use temp.info.compareTo(max.info)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

i have to define compareTo method itself?
isnt it inbuilt function?

Node is a class that you have created, so there's no way Java can know how you want to compare or sort its instances. Eg for a Person class you may want to compare/sort by family name/given name, for a HousesForSale class it may be on price, etc etc.
That's why you have to define your own method when you create your own class.
(The standard API classes like String do come with their own compareTo methods, of course)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why synchronised? Why interrupt the server - that means it won't be listening when you start the client. Just start the server then start the client in two different threads and let them both run.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Mike. That's a very big question, and I doubt that anyone has the time to write a new document covering all those topics. You'll find loads of stuff on the web for beginners. Please chekc that out first, then come back with specific questions.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@James, i read the link,as i understand i need to include the directory of my images?but i did not put the images in the folder.my java class and images are inside in one directory.

You will find that it's easier to update and maintain your application if you separate unchanging resources like images from the code itself. This is even more important when you start to use packages properly because they require multiple code directories. It's a very common convention to have an "images" directory that lives at the same level as the top-most code directory. I strongly recommend that you put your images in their own directory before starting to package it all into jar.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This explains how to do it

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry, I'm out of ideas. Your code looks fine - that's the standard way to do it - so I'm going to put all my money on a network problem, with a firewall as the prime suspect. I don't think there's a Java solution to that!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK.
Are you trying to connect machines on the same LAN (same subnet), or from the WAN via the router's NAT etc?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The fact that it works on localhost does seem to suggest a network problem such as firewall, but your code doesn't show your exception handling. Do you catch and printStackTrace() all possible exceptions?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You havr a nunber of options - redraw ther background is one, put the robot in it's own swing component is another. This tutorial is essential reading, not too long, and will give you the info you need.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just what has already been said... since you only store Characters in your arrays it would be better to make them Character[] rather than Object[]. That way you won't need all those casts.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi ezekal
Don't worry. I was not saying your code was wrong. Taywin's post was possibly confusing for some readers so I tried to set it right. Just concentrate on getting your program to work for now.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This could be miseading. Strings in Java are immutable, you can never change or add to them. "appending to a String" is actually creating a new String with the contents of the old string plus the appended char(s), so it's a horribly inefficient way to process any real quantity of appends.
If you want to append to a string repeatedly, use a StringBuilder rather then a String. StringBuilders are mutable and were designed for that purpose.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Don't worry, you are not violating any rules!
In Eclipse select your project, then export... from the File menu
That displays a "Choose export destination." dialog from which you chose Java/Runable Jar, which takes you (first time) into a new Jar wizard.
YOu can save your jar settings back into your project, so next time you just right-click the jar description and select Create Jar.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, excellent!
Yes, please use that code for study and inspiration, then write your own better version. If you get stuck I'll be happy to explain, but if you keep the imaginary n*m array in mind, and grok the variable names, it shoudn't be too hard.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thank you :)
Yes, we're having guess what the OP wants for non-integer size multiples. Are you there @turt2live?

ps: is this what is required?

Data is 4 elements
 1.00  2.00  1.00  1.00 
total is  5.00
Result is 7 elements
 0.57  0.71  1.14  0.86  0.57  0.57  0.57 
total is  5.00
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The method I suggested works for input & output arrays of any (sensible) size - no need for one to be a multiple of the other.
It's my birthday (really), so here's a gift...

 double[] spread(double[] in, int outSize) {
      // Algorithm is equivalent to:
      // If you have an array size n that you want to change to size m
      // by distributing the values, then logically you can:
      // 1. create an array of size n*m
      // 2. populate it from the first array by dividing each entry by m and 
      //    writing that to the next m positions
      // 3. sum the array by summing each block of n entries to give 
      //    a result of size m
      // This version is coded for clarity rather then performance.

      double[] out = new double[outSize];
      int inSize = in.length;
      int inIndex = 0, outIndex = 0, inCount = 0, outCount = 0;
      double temp = 0;
      for (int i = 0; i < inSize * outSize; i++) {
         temp += in[inIndex] / outSize;
         if (++inCount >= outSize) {
            inIndex++;
            inCount = 0;
         }
         if (++outCount >= inSize) {
            out[outIndex] = temp;
            temp = 0;
            outIndex++;
            outCount = 0;
         }
      }
      return out;
   }

Use something like this to test it:

   public void run() {

      double[] data = { 1, 2, 3, 2, 1 };

      System.out.println("Input: " + Arrays.toString(data));
      double temp = 0;
      for (int i = 0; i < data.length; i++)
         temp += …
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A [][] array is an array of arrrays. First you need to initialise the first/outer array, then you need to initialise the inner/second arrays, although you can do both at once provided all the inner arrays are the same length egint[][] blocklist = new int[6][3]
Your code just initialised the outer array to have no elements - {} - in which case there are no inner arrays at all. If that array is zero length, then any index will be out opf range.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Update:
I've now had a chance to think about the loop version (no n*m array).
Yes, it works really well, <20 lines of code, and just a single loop.
Give it a try. I won't sspoil your fun by posting the solution, at least not before you've had a go!
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You example has the expanded array an integer multiple of the size of the input. I gueaa that's not always the case.
There's a simple approach that I have seen used to spread accounting data (eg sales) from a weekly source to a calendar monthly output or v.v. (and all kinds of other overlapping time frames). I think it's the same problem?

If you have an array size n that you want to expand to size m like you said, then logically you could:
create an array of size n*m
populate it from the first array by dividing each entry by m and adding that to the next m positions
sum the array by summing each block of n entries to give an result of size m

Obviously that array could be large, but logically it's not really needed; you could achieve exactly the same result with acouple of nested loops that I don't have time to work out right now.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Provide an example: eg I have an array {1,2,3} that needs to be "extended" to length 8. What should the result look like?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@JamesCherill @NormR1...May i excuse?...i just want to ask what is boxing and unboxing?Thank you.

Sorry for the delay in replying (nighttime here!). It's when the compiler sees you using a char when it should be a Character, or vice-versa. The compiler will "box" your char into a Character, or "unbox" the Character into char for you without you having to write any code. It was new in Java 1.5, and enhanced in 1.7 to include the cast situation that Norm & I were discussing.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That demo program was worth every minute you spent on it! I was able to run it and reproduce your problem.
If you don't add the ActionListener ast all, so the button has none of your code attached to it in any way, then you still have the problem. So it's nothing to do with the ActionListener. My guess is that it's a focus problem - clicking the button moves the focus.

You are using the default input map, which is the "when focussed" map. Try adding your binding to the WHEN_IN_FOCUSED_WINDOW input map - that fixed it for me.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So it's a 1.7 thing? I thought all the boxing/unboxing stuff was completed with 1.5, but obviously not (the JLS I quoted was 1.7 of course). Problem solved!
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Did you try that with line 4?
The cast is a checked cast because oac is an Object reference, so it will compile without error, and at runtime it is found to contain a Character and unboxes it without error. That cast is OK, both at compile time and at runtime.
I just ran it (line 4 included) on Java Windows 64 Java 1.7 and it compiles correctly and runs correctly - just like the JLS says it should.
If you change line 3 to Object oac = "abc";
then it compiles correctly, but throws a runtime java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Character, also just as specified by the JLS for a checked cast.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Norm's answer was correct up to Java 1,5 Since then boxing and unboxing casts (ie between primitives and the appropriate wrapper class, eg char and Character) are explicitly defined and allowed in Java language spec:

5.5 Casting Conversion
Casting conversion is applied to the operand of a cast operator (§15.16): the type
of the operand expression must be converted to the type explicitly named by the
cast operator.
Casting contexts allow the use of one of:
• an identity conversion (§5.1.1)
• a widening primitive conversion (§5.1.2)
• a narrowing primitive conversion (§5.1.3)
• a widening and narrowing primitive conversion (§5.1.4)
• a widening reference conversion (§5.1.5) optionally followed by either an
unboxing conversion (§5.1.8) or an unchecked conversion (§5.1.9)
• a narrowing reference conversion (§5.1.6) optionally followed by either an
unboxing conversion (§5.1.8) or an unchecked conversion (§5.1.9)
a boxing conversion (§5.1.7) optionally followed by a widening reference
conversion (§5.1.5)
an unboxing conversion (§5.1.8) optionally followed by a widening primitive
conversion (§5.1.2).

...
** • An expression of a primitive type may undergo casting conversion to a reference
type without error, by boxing conversion.
• An expression of a reference type may undergo casting conversion to a primitive
type without error, by unboxing conversion.**

(my highlighting). So the answer for current Java versions is that casting between primities and referfence types is just like any …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Its really hard to guess without the code, but maybe your ActionListeners (new game, help) are opening new windows which capture the keyboard focus, and you don't have key bindings that include the new windows?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If this is a Swing app that needs its GUI refreshed every second then I would advise a Timer rather than doing your own thread. (Either will work, but a timer is less likely to give you problems.)
Have a look at javax.swing.Timer You create one that runs every 1000 mSec. In its actionPerformed you update your clock. Check the standard API doc for details and an example - NB there are 3 timer classes in the API, you need the javax.swing one

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You don't say which action listener you are talking about - the empty one or the not implemented one. And what exactly does "keybindings fail" mean?
ps Don't use System.out.println(e.getMessage()); , use e.printStackTrace(); instead so you can see where the error happened as well as what it was

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

a*b^c+d translates to a*Math.pow(b,c)+d; and you can make the order of evaluation explicit like this (a*Math.pow(b,c))+d; (because Math,pow is a method it always bew called before any other operator that uses its result).
you can work out the rest

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry, I don't understand that question. The example I gave used letters (?)