JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, just like that! Only thing to worry about is what happens if the user doesn't type a vialid number, but maybe you can ignore that for now.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, there's a "Mark Question Solved" at the bottom of the page.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Excellent! Please mark this "solved" for our knowldge base.
See you next time... ;)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, I got the virtual array idea. Its just that for a single ship all the virtual indexes would be contiguous, but your program allows the user to select 8 at random. So "shipSize" is really "numberOfShips", etc.
Sorry to bang on about names, but mis-naming a variable is the very best possible way to obscure how the code may differ from the intent... OK, enough alteady. J.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Seriously - you need more prints. Eg print the shipCell array after you have initialised it.
Some line of your code is not doing what you think it is - so trying to think your way out of this is unlikely to work. Use prints to check every stage in detail until you find the error. By definition the problem will be in one of the things you haven't checked yet, no matter how obviously right it may seem to you.
It may sound tedious but that's how real programmers find real bugs in real life - they may use a debugger to see the variables rather than using prints, but the process is the same.

(I did have a quick look at the logic, but nothing jumped out at me. I was confused by the naming vs the implementation - the naming suggests that there is one ship of length 8, but the logic seems more like 8 disconnected one-cell ships)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Rather than just trying different things, add lots of print statements to your code to display the values of all the important variables and arrays wherever they are changed. Then you will be able to see exactly what is happening and where it's going wrong.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Reading a line from the file via a scanner is exactly the same as reading stuff from the console via a scanner, and you already have done that at least once! Just use an ordinary Scanner method like nextLine, nextInt etc. to read the info from the scanner and use that info to specify the size of the array. This is a lot easier than you think!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thta's just one error, with a complete list of where it came from. In theis case the first two lines tell you everything - on line 48 you try to access an array element [-1]
Almost certainly because paint is being called before you select something in "colors", ie selected index == -1

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

String and StringBuffer have no "extends" relationship, so you can't assign or cast either to the other.
Case 1 works because StringBuffer has a constructor that takes a String as parameter.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, that's right. A new array of objects is full of nulls.
jalpesh: the required method signatere returns Student[], but it any case you cannot assign nulls to an int, or any other primitive.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You are aware that this is the Java forum... ?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
int num = Integer.parseInt(args[0]);

... is the only array referred to, so there's the problem (the exception message will have told you that line number). It's trying to access the first element of the array of args, and that array is empty, so the index is "out of bounds". args is an array containing all the arguments passed to the program when it was invoked. It's in the signature of the mandatory "main" method.
It's expecting you to pass in a number as a runtime parameter/argument - that's the number it tests for being prime.
I'll leave you to find out how to pass a runtime parameter.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't know how to make it any clearer than this link - the 4th in the Google search.

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

Just this one time...
I just replaced the stupid int with a boolean, and gave it a meaningful name. If this code still isn't clear then add some print statements to show how the values of the main variables change, and execute it yourself.

class PrimeNo{
    public static void main(String args[]){
        int num = Integer.parseInt(args[0]);
        boolean numberHasADivisor = false;
        for(int i=2;i<num;i++){
            if(num%i==0)
            {
                System.out.println(num+" is not a Prime Number");
                numberHasADivisor =  true;
                break;
            }
        }
        if(! numberHasADivisor)
            System.out.println(num+" is a Prime Number");
    }
}
jalpesh_007 commented: really good... +2
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's rubbish coding by someone who hasn't heard of boolean variables.
Replace it by a boolean called numberHasADivisor, initially false, set true on line 9, and all should become obvious.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You fix it by looking at the API documentation for JList and using method names that are documented there. You didn't post the error messages like you should, but each of those 3 lines tries to call a method that doesn't exist for JList. You can't just guess or make up methoid names for API classes.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Check boxes are independent - you can select as many or as few of them as you want. It makes no sense to group them. Radio buttons come in groups, and only one can be selected in any group. AWT got the terminology mixed up, which was very confusing, but Swing does it right.
So:
If you want independently selectable options, use JCheckBoxes, no grouping.
If you want a group in which only one can be selected at a time, use JRadioButton buttons in a ButtonGroup.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Swing was built on top of AWT, so there are a few AWT classes that you still use with swing because there was no need to replace them. Container is one that you still use; your code is OK.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

use JRadioButton buttons in a ButtonGroup

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The Swing equivalent to the old CheckBoxGroup (only one can be selected at any time) is to use JRadioButton buttons in a ButtonGroup

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Looking more closely at your code there ia a small problem that may reval some confusion in your thinking:
The if test in the static block is totally redundant because emp is an unitialised static variable, which will always be null when the static initialiser block is executed.

Provided you have no methods that update the emp variable your singleton creation/access code is completely thread-safe. It would help guarantee this if you also declare emp as final.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The answer to your first two questions is "no problem, that's perfectly OK"
The output you get is what you asked for on lines 82/83. When you print the order object o1 Java has no information on how to print that, so you get a default printout which shows the the object's class (stellarstationary.order) and its hash (8dc8569). If you want to print someting more useful you define a toString method in your order class. This overrides the default one you inherit from Object, and allows you to specify how to print an order, eg

@Override
public String toString() {
   return "This is an order, its number is " + OrderNum;
}

ps Java naming conventions are that classes should begin with an upper case letter, variables should begin with a lower-case letter. It's easier for others to understand your code if you stick to the conventions.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The static block will be executed before a constructor, or any other method, can be accessed, so I believe that there should be no thread-related problems (JLS 12.4.1)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't know any way to do that, someone else may know?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, you can't stop it! Swing will call that whenever it thinks the panel needs painting, including the very first time it's made visible. You can either test some boolean flag you set to see if you want to paint, or you can keep the panel invisible until you are ready.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, that's it. You define the subclass, create an instance of it, and use that instance just like any other JPanel, so yes, all the code is OK, including line 12.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have 4 connections throiugh one ServerSocket, but only one per Socket - the difference is important.
It's always a bit messy clsing a Socket connection when you have input/output streams going in both directions. Whichever end closes first will trigger a SocketException at the other end. ALl you can do AFAIK is to send 999 and close the socket. At the other end set a flag when you get the 999, then catch the exception and then ignore it if the flag is set.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Java Swing painting doesn't work like that, no matter how reasonable that looks. Swing is in charge of app drawing, and has all kinds of hidden double-buffering etc behind the scenes. If you simply update that Graphics "behind Swing's back" there's no guarantee about when or even whether that will find its way onto the screen.
This tutorial explains the right way to do it - by overriding paintComponent as Taywin said.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There are lots of people here who will freely give their time to help you become the best Java programmer you can be.

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

Details depend on what you are removing from (ArrayList, HashSet etc?), but re-starting the find from the beginning each time is what gives you the O(N^2).
Use an algorithm that just searches once through the data, removing as it goes. (You may need to start at the end of the data and iterate backwards towards the beginning, depending on what happens when you delete an item.)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Schol-R-LEA gave you the answer. GregorianCalendar. Look it up in the API doc.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Check the API doc for Integer.parseInt
It tells you it throws a NumberFormatException if the string isn't a valid representation of an integer value

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

public String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

So substring(i,i) will return a string of zero characters length. substring(i, i+1) would be better.

Also line 13 <= intNumbers.length() - that will give you values from 0 to length, which is one value too many.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Never say "it gives an error". Always post the complete exact text of your error messages.

Anyway, charAt returns a char, so that's not a valid parameter for parseInt. YOu can keep the parseInt but use substring instead of charAt, or you can keep the charAt and use the fact that char is a numeric type, so, for example
myChar - '0' returns 0 if myChar is '0', 9 if myChar is '9' etc

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Problem's on line 15. Obviously misunderstood what valueOf does.
You need to use either substring or charAt there to pick out each char of the user's input string, then you can use parseInt to convert that one character to a number.
There are better ways to handle each single character, but maybe it's best to get this working with what you already know, then later we can move on to discuss better ways to do it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's a good swap - I can't comment on the rest of the code now

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
int numbValues;  // not initialised, will be zero
int[] values = new int[numbValues];  // array of zero elements, any index will be out of bounds
 Object(int numbValues) {
    this.numbValues = numbValues; // updates numValues, but too late to affect size of array
}

.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Card swap = deck[k];
deck[N] = deck[k];
deck[k] = deck[N];

Here's a problem. You don't use "swap", you just put the original entry back.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's so long since I started using Eclipse that I haven't looked at any current tutorials. So I can't suggest or recommend anythning; all I could do is Google - which I'm sure you can do just as well. Sorry. J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
 public Image getPlayerImage()
{ return mario.getImage(); }

mario already is an Image, so you just need to return it "as-is"

 public Image getPlayerImage()
{ return mario; }

When you initialise mario you have specified both a document base, and a fully-qualified path/file starting at c:
YOu should have one ofr the other of those, not both. For an applet it's the document base you need, the other paramter should just be the file name (and it's dir if that's a sub-dir of the document base directory). As jalpesh suggested, if you are not sure you should print the document base to find out exactly which dir it refers to

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Root dir (inthis context) would be a dir referenced in the class path - a dir where javac will start to look for package/class. Current dir is an OS dir that will be a root dir if . is in the classpath.

Yes, it's A.class. Sorry, that was me typing too fast! It looks for .class files in the classpath, not source files.

At this point I have to admit that I haven't compiled a multi-package app at the command line for about 15 years. Nowdays I'm 100% on Eclipse, where all these issues are handled differently. If you want to get any more into it, I'm probably not the best person to get involved.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, I'm sure quite a few of us did. If the OP had followed my suggestion he would have seen the resulting N.P.E. and debugged that for himself. And worked out his own solution.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What does the printStackTrace tell you?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Have you added the e.printStackTrace(); yet? I just spotted your mistake, and you will be surprised by what the stack trace reveals.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Parsing "10/10/2012" and "20/10/2012" with a SimpleDateFormat of "dd/MM/yyyy" works perfectly, so your code isn't doing what you think it is...
To find out what's really happening, try putting a lot more print statements into the code, eg print s2 and s3 (with a delimiter immediately before and after so you can see any leading/trailing blanks). Do an e.printStackTrace(); in your catch

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, just thought that was worth checking.
Your code uses some complex-looking data structures, whose purpose isn't immediately obvious. Maybe it's time to back off a bit and do this is easy stages? Personally I would create small test file with a few blocks of solid RGBCMY colour, then try hard-coding swapping two of the colours before moving on to the full logic and the full file.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, I know what a simple date format is. The question is what format are you using? What is the string in quote marks inside the parenthesis after the text SimpleDateFormat in your code?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That parsing looks right, depending on how sdf has been defined. Can you post the definition of sdf, and an example of the text that's not parsing properly?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You use getData().getDataElements(... which can return all kinds of different formats, depending on the image source. Your apparent assumption that it's 3 bytes/pixel may be wrong? (Most commonly you see one 32-bit int per pixel containing ARGB values that you can unpack with shift/mask operation).
Have a look at the PixelGrabber class for a predictable consistent way to get pixel data from an image.