JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No. That would have thrown an InputMismatchException. His output is just the result of calling toString on a Scanner object when trying to print it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's something deeply suspicious about the data redundancy here that makes me unsurprised that your swap method has problems.

As I understand it theres a Tile[][] array that says what Tile is in what position, and each Tile has its x,y position as instance variables. Change one and you must be certain to change the other. One small code error and they will get out of step, with chaotic results.

In your swap routine it looks like there are 4 Tiles:
tiles[from.getPosX()][from.getPosY()], tiles[to.getPosX()][to.getPosY()], from, and to, but in reality there are only 2 and you are swapping around their references as well as their data. I tried to follow what was really happening in your swap, but between the redundant references and the redundant data it just made my brain hurt.

Now the only reason ever to duplicate info like that is to fix a performance problem. I know you are a fan of premature optimisation, but you are paying a high cost here. Your attampt to fix a non-existant performance issue is resulting in code that you can't write correctly or fix.

IF this was my code I would be thinking about getting rid of those instance variables, and just keeping the array. (Based on a guess as to how often you need to know which Tile is in a given position, as opposed to where a given Tile is. If I'm wrong the keep the instance variables and can the array.) IF there are cases where you just …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I can't read all that code, but it sounds like you should be using Key Bindings.
http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you declare a variable its scope is from the immediately preceeding { to the immediately following }. So your excel variable is in scope from line 211 to 213 only. Outside that scope the variable cannot be used. You have to declare variables somewhere where their scope includes all the places where you want to use them (just looking at your code fragment, anywhere that's NOT in the if or else blocks would work).

ps: The approach you have taken is not certain - in particular the exists test followed by create new file is not an atomic operation.
You are on much safer grounds using the modern Files class and its createFile method https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#createFile-java.nio.file.Path-java.nio.file.attribute.FileAttribute...-

Edit: following Peter's note... the nio package (including Files) was always a standard part of Java SE 7, it's just that it wasn't pushed very hard until later.

peter_budo commented: Java 8, uhhhh, ohhhh.... Can't do it yet on Android ;-) +16
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK - maybe you want to have some kind of response to the user when the guess is right? Maybe you don't want the change the random number every time he guesses - seems it a bit unfair!

ps:
That actionListener code is how we did things before Java 8. It works, but the syntax was horrible and confusing and error prone - you start a method call on line 38 and don't finish it until line 55, and even then it ends with }}});

Now there's a much simpler way that looks like this:
First, put the code to handle your button click in its own method, eg

public void buttonClicked(ActioneEvent e) {
   // code just like your actionPeformed
}

then add that to the button by

button.addActionListner(this::buttonClicked);

If you have multiple buttons then just give each its own method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 31 you declare the parsedUserGuess variable, OK, but you also give it its value by parsing the text field at that point. This happens before the user has been able to type anything, so the result is just 1.
You need to perform the parseInt and assign that to parsedUserGuess after the user has enetered his guess - eg in the actionPerformed method.

ps That code is really not bad for a first app. You are on the right track!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, if you really really want..
Here's an excellent tutorial on client/server messaging. It' part of the Oracle tutorials, which are by far the most complete, accurate and up-to-date tutorials for Java. Get started here.
Many other tutorials on the web are either out of date or downright wrong, so be careful out there.

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

You create a frame and a VectorDraw panel, but you don't add the panel to the frame!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Have you tried redirecting the new process's output stream? There are various options for that (see the API doc). This one looks particularly interesting...

public ProcessBuilder inheritIO()
Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You did not say what help you need! Please ask specific questions.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Either have an "Add" button, or wait until the user presses enter in the textfield to trigger adding the item to the queue

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, I've written it, and it does compute the average of these numbers and then tell for each number whether it is above, below or equal to the average. What now?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have a single blank character at the start of the string.
Ie it's " 2", but it should be "2". The blank character is not valid when parsing an integer.

Maybe you are initialising out to " " instead of "" somewhere?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, here's a very simple but completely runnable example that shows how to update an animated simulation at a steady speed and update the GUI to match. The important parts are the last two methods. YOUou can use a simple java,util.Timer instead of the ScheduledThreadPoolExecutor if you like.
If you are required to use Thread.sleep then use that instead of the Timer, but don't expect reliable results.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import static java.util.concurrent.TimeUnit.*;
import javax.swing.*;

public class Animation0 extends JPanel {
    // absolutely minimal example of how to structure animation in Swing.
    // updateSimulation steps the simulation model through time
    // paintComponent updates the screen with the latest data

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Animation0::new);
    }

    // this is the "model" - just a single object moving at constant speed
    int xPos = 0, yPos = 35;    // current position of animated object
    int xSpeed = 1, ySpeed = 0; // current speed

    Animation0() {
        // create and display the animation in a JFrame (boring)
        final JFrame frame = new JFrame("Animation 0 (close window to exit)");
        setPreferredSize(new Dimension(400, 150));
        frame.add(this);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        // start scheduled executor to update the model every 30 milliseconds...
        new ScheduledThreadPoolExecutor(1).
                scheduleAtFixedRate(this::updateSimulation, 0, 30, MILLISECONDS);
    }

    public void updateSimulation() {
        // reliably called by executor every 30 milliseconds
        xPos += xSpeed; // update position of object
        yPos += ySpeed;
        repaint(); // notify that screen refresh is now needed
    }

    @Override
    public void paintComponent(Graphics g) { 
        // …
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's because the model contains the state info for the spinner.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm confused.
If you want them to change together use one model.
If you wnt them to change independently use two models.
What's the problem?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Oh wow, if you can't query the size of the queue that makes it harder.
Maybe you could keep your own counter of the number of elements in the queue?
Alternatively you could use the second approach (add new items at the front of the queue by adding it at the end and cycling the queue until it reaches the front). In that maybe case you could use a sentinal value, so you would know when you have reached the front while cycling the queue. Something like:

add(item) {
  enqueue sentinal value
  enqueue item
  dequeue entries and re-enque them until you find the sentinel value
  // item is now the first entry in the queue
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The problem is not with the tokens.
~s.o.s~ gave you the hint 21 hours ago, but you ignored it.
You create a variable called postFix, but you don't give it a value until line 44, so on line 23 it still has its default value of nulland when you try to use it, you get an NPE.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You misread the question.
I didn't ask what was in the DeQueue, I asked about the variable called postFix.
If youy are still confused, try printing postFix at line 33

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, it's not the token that's null.
What is the value of the reference variable postfix on line 23 in the code you posted?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Applications need a "master" or "controller" class that start up, manages and controls evrything else. It also acts a central point of reference that is accessible to the whole application (you often pass it as a parameter to the constructors of the various component classes). In this case it could have a getConnectionManager() method that anyone can call. Kinda like...

public static void main... {
   new Controller().start();
}

class Controller {

    private DefaultOptions defaults;
    private ConnectionManager connections;
    ...

   start() {
      defaults = new DefaultOptions();
      connections = new ConnectionMananger(defaults.getWorkingDirectory());
      Engine e = new Engine(this);
      ...

public ConnectionManager getConnectionManager() {
   return connections;
}   
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

int = 32 bits
short = 16 bits
byte = 8 bits
byte[n] = 8*n bits
etc

But check out the API - writeByte, writeShort, writeInt all take an int as parameter, so it's the method, not how the vars are declared that matters

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I question the need for your own special file format ("much faster" probably means you will save a few microSeconds), but anyway...
You can use a DataOutputStream to write java primitives, byte arrays, and Strings to a binary file as a sequence of bytes in their normal internal binary format. And DataInputStream to read them in again.

DataOutputStream out = new DataOutputStream...
out.writeByte(0x4D);
out.writeByte(0x41);
...
out.writeShort(width);
...
out.writeUFT(id);

...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

... and
3. Since you have a try-with-resources, your FileOutputStream is guaranteed to be closed when you exit the block, either normally or abnormally - which is more than your explicit close calls will do!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It worries me that much of this discussion is about the difficulty or efficiency of inheriting either way. And although Liskov is 100% relevant, it's a heavyweight way of expressing the absolute basic English definition:
"inheritance" is an is-a relationship
If a Vector is-a Matrix then Vector is a subclass of Matrix, and vice-versa. If neither is true then there is no subclass relationship.
Obviously a Matrix is-not-a Vector, so Matrix cannot possibly be a subclass of Vector.
However you can assert that a Vector is-a Matrix (single column or single row) so you could, if you thought it useful, have Vector as a (specialised) subclass of Matrix.

ddanbe commented: Thanks for your tip! +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, and similarly for reading it back in.

ps: It's easier for us to read your code if you stick to Java naming conventions - connectionList, not Connectionlist (looks like it's a class)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No need to change that! A List is a Collection.

(interface List extends Collection, All Known Implementing Classes:
AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector)

I was just making the point that you can read/write the whole thing as a single object, as long as it's some kind of Java Collection, eg ArrayList, as opposed to a class of your own.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe just loop through the keys of one of the Maps, deleting all the entries that don't have a corresponding key in the second Map?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your post's title says "in Java", but the requirement is "in VB script".
Either way, when you have worked out what language you are using, you will find many comprehensive tutorials on the web. There's no need for anyone here you write a new tutorial just for you.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"under utility" tells us nothing, especially when the text utility does not appear anywhere in the original program.
You need to post the complete exact error message (not just your version of it) and the exact line of the source code that it refers to.

jwow commented: do you know how to fix the restart button. its not working +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I was suggesting java.util.Timer to update the model, not the GUI.
In general, the model may be CPU intensive (eg checking collisions between arbitrary shaped sprites) so I avoided javax.swing.Timer to keep that processing off the Swing EDT.
Yes, you could use firePropertyChange to trigger a GUI update. I just think repaint() is easier. In this case the OP is using GDX, so the method to invalidate the GUI will be different.

@centenoid. If you want to run on screen with different pixel sizes you need to build your model in some independent units (eg millimeters) then scale that to actual screen resolution when painting/updating the GUI.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Normal practice for Java animation is to use a java.util.Timer scheduleAtFixedRate to update the state of the model, then invalidate the GUI state to force a re-draw. That way the animation proceeds at a constant speed and the screen gets updated as often as possible.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe you could render all your graphics at some fixed resolution (eg HDTV) and use a scale(double sx, double sy) to scale all the contants to whatever the actual screen resolution is?

As for speed... what Timer are you using?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Wrap the code in a no-args method, eg

public static void doMyStuff() { ...

then

new Thread(Client::doMyStuff).start();

Easy or what?

ps: I don't like seeing all that in static methods, but that's another question. You will presumably need multiple instances of the class (different instance variable values) for your multiple threads. In which case doMyStuff should not be static, and you can start the Thread like this:

Client c = new Client(...
new Thread(c::doMyStuff).start();
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Some of the cases do have breaks, but that's not the real problem.

(sorry Ant695 but you're going to kick yourself when you read this...)
You should be switching on the chosen planet, not the person's weight!
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Because bytes are integer values. Doubles are 8 byte floating point values. When you assign a float value to a byte it's rounded down to the nearest integer value.
You also have a problem if the value is >127, becuase that's the largest value a byte can hold.

Either way, the answer is to use more than 1 byte to send the value.
You can use an nio ByteBuffer to convert doubles etc to byte arrays abd back again.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This sounds like the "premature optimisation" anti-pattern!

I would concentrate on getting it working properly with a simple implementation (eg re-draw everything each tick). If you try to optimise too early then
1) You may well be wasting time fixing a non-problem
2) Your optimisation may well clash with things you have to do later to complete the functionality

I'm not saying ignore efficiency totally. You should try to start with simple clean sensible data structures and algorithms, but do not complicate things until/unless you know you have a performance issue and you have understood all the constarints of the full functionality.
If you have a strong suspicion that a particular area will be a perfomance bottleneck then define it as an interface and provide a basic implementation. That way if later it is confirmed to be a problem you can swap in different implementations.

Finally, remember you have a machine that processes thousands of instructions in a microsecond, and most of those instructions are OS and JRE, not your code.

Doogledude123 commented: Awesome recommendation, always the best answers from you. +3
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In the same vein...
Unicode also has characters for displaying chess pieces, starting at u2654

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's the compiler being a bit smart. It's checking that you always return a value as specified in the method signature. Suppose your data is such that none of the cases is true... In that case it will drop through the switch and hit the end of the method without returning anything.
You need to add a return statement at the end of the method to handle that situation. It can return a value that indicates a problem, if you want.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You just copy/pasted your assignment without even a moment taken to explain what help you need. That's highly disrepestectful to the many people who give their time to help others here.

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, there are UniCode symbols that need more than 2 bytes, but frankly they are a mystery to me.
For substrings >= 2 chars I can't think of anything that wouldn't require many searches of the entire String, which is horrible. (So maybe it's always worth trying the single letter case first because that's so very quick and would surely work in 100% of all real-life situations.)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't know Python, but can't you just create an array of 2^16 booleans, corresponding to the 2^16 basic UniCode characters, do one pass of the string and set the corresponding boolean as you encounter each character.

Gribouillis commented: good suggestion +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The OP said "display", which may imply that its not an updatable view - in which case its a lot lot simpler to do!
To handle updates, rather than a timer, you can listen for update events in the JTable and save the changes as they happen. Alternatively, you may prefer the kind of user interface where the user edits the table, then presses OK to commit the update, in which case you just listen for the OK button.

amogh.max commented: These data are coming from a sensor,so the database is updated every second;how would i implement listen for update in that case. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can do this by using JDBC to read the data and a JTable to display it. It's a fairly common thing to do, so you'll find many examples and tutorials on the web - just Google JDBC and JTable together

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Integer.parseInt(... takes the string that the user entered and converts it to an int. You can put that into an array of doubles, but you will have lost everything after the decimal point. If you want to parse a string for a double value it's a very similar method... Double.parseDouble(...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You are using + to concatenate stuff for printing, OK, but you have left some out. You need a + between every expression..
"there are " + weeks + " in " + (etc)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

How many times have we said this in the Java forum...
Never NEVER NEVER do this when writing new code:

} catch (Exception e) {
}

If/when there is an error you just told Java that you didn't want to know anything about it, and please discard the detailed error message that Java just created for you.
ALWAYS put an e.printStackTrace(); in your catch blocks until/unless you have a good reason to do something else.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Nobody will send you the code. Sending you the code will only help you to cheat. Either do the work or drop the class.

If you want to take this seriously, post what you have done so far and someone will hel you from there

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.