7,116 Posted Topics
Re: That's good progress - you learn fast! Let's get rid of the duplicated code for getting the value of a card... maybe a method that has all the "if card equals... value = ..." lines `public static int getCardValue(String card) { ...` then you can simplify code like this for(int … | |
Re: If a class has a private attribute, and the developer didn't supply a setter, that means you're not supposed to set it (eg look at the get methods for the String class - you won't find any corresponding set methods because they don't want you to change a String object … | |
Re: You can read your big file into a BufferedImage, then use its getSubimage method to pick out the separate icons, and pass those to the ImageIcon constructor | |
Re: That means you tried to run the program when it still had error(s) that prevent it from compiling. In Eclipse look for the little red x at the left of the code, red underlining under the code in question, and a red block in the vertical scroll bar to show … | |
Re: Suppose class B extends class A and overrides its doIt method. Consided this pseudo code A myObject; while ... { ask the user a question if (user reply is "A") myObject =- new A(); else myObject = new B(); myObject.doIt(); // sometimes this is A's version, sometimes it's B's } | |
Re: Burning the CPU in an infinite loop is a very bad way to look for changes. Add a `TextListener` to your JTextField. It will be called once every time the text is updated in any way, so that's the ideal place for you to check the length. See the API … | |
Re: No, you would normally open the socket connection once, send all your data, then close the connection. Opening and closing for every message is unnecessary unless there are very long time intervals between the messages. You will need to flush the output stream after each message if you expect an … | |
Re: 1. At *compile* time Java checks that the method call is valid. It checks that using the declared type of the reference. You cast the reference to Dog and Dog does not have a sniff method so line 11 will be rejected by the compiler. Dog does have a bark … | |
Re: You need: 1: a reference to the instance of "another class" that has the JPane you are interested in 2: a method in "another class" that returns the JPane Then you can just call that method on that instance to get a reference to the JPane and thus its contents | |
Re: something like ... cus.repairs.get(ref).setNotes(gui.notes.getText)) | |
Re: The Action mechanism was created for exactly this purpose: http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html | |
Re: JToolbar's addSeparator() method adds a standard sized gap | |
Re: Probably because on line 102 you add the file chooser to your window? | |
Re: disp() is not static, so d isn't being used in a static context. If you did use it in a static context you would get a compiler error. The NPE exception message will tell you exactly where in your code the NPE happened. | |
Re: What version of Java are you running? I tried... public void x(String s, int... i) {System.out.println("i");} public void x(String s, double... d) {System.out.println("d");} public void run() { x("abc", 1,2); x("abc", 1.0,2.0); } ... and that works exactly as you would expect - Java 1.7 Windows 64. | |
Re: No, it's not important. You have no control/knowledge of how those two threads will be scheduled; they have the same priority as each other and the thread from which you are starting them - eg it will depend on how many processors you have in your machine. If you need … | |
Re: Work out how to draw that whole thing using ordinary Java 2D graphics methods like drawLine and drawString, then follow this: http://docs.oracle.com/javase/tutorial/2d/printing/index.html | |
Re: 1. You can't define one method inside another - that's just the Java langauge definition. (except when the second method is inside an inner class defined inside a method...) 2. After executing t.start()) you have two threads withe same priority and no synchronisation, so you have no control or knowledge … | |
Re: FYI it's the same thing for static methods - because they cannot be inherited, just like private methods, you can re-define (create a method with the same name) but not override. | |
Re: Right now it looks like your file transfers run from actionPerformed methods. These are called on the Swing Event Dispatch Thread, and no other Swing code will run until the actionPerformed returns. This includes any code to update the screen with progress bar changes. So the first thing you have … | |
Re: ints always have 32 bits. When you convert to a String to display them the string conversion method supresses leading zeros. | |
Re: What format / file type is the predefined document? | |
Re: I doubt very much that your key listener is working properly. Unless you have other code changes it will still have the "updates eveything at once at the end" symptom. Everything to do with Swing (eg painting the screen, running actionPerformed methods) happens on a single thread - the "Event … | |
Re: ++x is 3, then the next ++x is 4 then the last one is 5, so you have 3*4*5 ie 60. Now x+= adds that 60 to the initial value of x (2) to give a result of 62. | |
| |
Re: Have a look at the API doc for JTextField. The intro shows how to create a custom field - in that example one that takes upper-case letters only. You should be able to use that example and its accompanying explanation to create your own numeric-only field. Alternatively, if you have … | |
Re: If you have the key then it's just bMap.remove(ref) - or did I misunderstand your question? | |
Re: Loop thru coustomers, loop thru each customer's repairs looking for the right key value, eg for (Customer c : customers) { for (String repairNo : c.getRepairs().keySet()) { if (repairNo.equals(...)) ... Repair r = c.getRepairs().get(repairNo) (this code can be optimised, but that's the clearest version to understand) | |
Re: 1. You need to determine whether the original letter was upper or lower case before you add the encryptor to it. Eg adding a suitable encryptor to an upper case may give you a valid lower case letter 2. If the encryptor can be >26 then instead of just subtractiong … | |
Re: 90% probability that it's just looking in the wrong place. You can try `System.out.println(new File("xxx").getAbsolutePath());` (with your own file names) to see exactly where Java is expecting to find them. | |
Re: I don't have that much free time, and I'm certainly not a teenager, but if you want someone to review your design and proposed library APIs I'd be glad to help. J | |
Re: Because you have not followed all the instrctions yet. What about "Create the class program that has main() method for execution. This program should create two Student objects and implement all methods listed in the Student class." | |
![]() | Re: > i don't think your clone method works, might wanna check that Can you clarify that? ArrayList certainly implements a shallow clone(), so what do you suspect, and why? Thanks. |
![]() | Re: So you are getting an NPE in this line? this.cards.get(i).getValue() == value you first need to print out the parts of that expression to see what's null, is is cards? or the result of calling cards.get(i)? or the result of calling cards.get(i).getValue()? Once you know what is null you can … ![]() |
Re: Search by name you already have from the map itself. For the others you have to loop through all the Employee values in the map testing each Employee to see if it has the desired values. | |
Re: You must go back and read the assignment carefully - you won't get marks for ignoring it. Start with the first five words "**Create a class named Student**..." Then follow the rest of the instructions one line at a time. | |
Re: `public void main..`. declares its return type as "void" - ie it returns nothing. Do you mean something like this? public int timesTwo(int i) { // method returns an int int j = i*2; return j; } | |
Re: for(int x = 0; x <= count; x++) { myArray[x-1] = myArray[x]; } If you really executed that code the very first array index will evaluate to -1 and it will throw an index out of bounds exception. So what's *really* going on? | |
Re: Every Java class inherits a toString() method from the Object class, so methods like println use that to convert the object you want to print into a String. The inherited method prints the class name and the object's hash (normally the same as its address). If you want to display … | |
Re: When you have an arithmetic expression with all integers, Java does integer arithmetic. The integer result of 85/100 is 0 (fractions are truncated) That's why you get 0 if you do the division first. If you do the multiplication first it's OK. | |
Re: The normal way to copy a file from an input stream to an output stream looks like this: byte[] data = new byte[4096]; int count; // open input and output streams while ((count = in.read(data)) != -1) { out.write(data, 0, count); } // close input and output streams Under all … | |
Re: i=i++; is a no-op - it leaves i unchanged. What happens is: the current value of i is stored i is incremented the stored value is assigned to i See http://skeletoncoder.blogspot.fr/2006/09/java-tutorials-i-i.html | |
Re: After `x=x+encryptor;` you can test to see if `x>'z'` and if it is then subtract 26 | |
Re: > Please help me to how to solve the error. 1. The error message contains the line number where the error happened; go to that line. 2. Immediately before that line add a print statement to print the value(s) of the index variable(s)/expressions(s) to see exactly when they are going … | |
Re: getOutput() is a method, but you call it without its () - yet there's no compiler message? Do you have a variable with the same name? | |
Re: This is a pretty common mystery area, because different layout managers have different behaviour in terms of overriding the size to fit the layout. You may have better luck with setMinimumSize(...) | |
Re: Maybe add a boolean to the Ball class for isPotted. (initially false, set t o true when the ball is potted) Then your paint/collision etc methods can call isPotted() to see if the ball is still in play or not (rather than performRemoval). Then when the black is potted you … | |
Re: Rather than just printing the exception's messgae, do a full e.printStackTrace(); This will give you the details of any errors that caused the exception eg file not found, incorrect priveleges to read file etc etc | |
Re: You can use getText() to get the existing text in the text field, then append your new character, then use setText() to update the field. | |
Re: A couple of quick observations: You repeat the code to get the numeric value of a card a few times - repeating code is never agood idea. You can put all that into a method ` public int getValue(String card) ... ` that you can call whenever you want the … |
The End.