7,116 Posted Topics
Re: public boolean isNorth(Location c) { if(north!=null) return true; else return false; } ... is a long version of public boolean isNorth() { return north!=null; } ie (1) `north!=null` is a boolean expression, so you don't need to convert it to true or false and (2) since you don't use the … | |
Re: You're heading in a right direction, but there's something missing between line 41 (where you prompt the user) and line 42 where you test the user's input (that you haven't read yet!). | |
Re: The Image class has a getScaledInstance method that scales the Image to whatever size you want (eg the size of your JPanel) | |
Re: Line 26 closes the method definition, so the if statement is not in any method, which is illegal and has confused the compiler. | |
Re: Stack overflow is almost certainly caused by you recursively calling a method from inside itself (or from inside a method that it calls). The error message should also tell you the line number where it happened, so that's where you start looking. | |
Re: Is this solved, or would you like more help with the recursive solution? | |
Re: Yes, the default appearance of buttons is not good for this application, but it's easy to change that. For example a quick `button.setMargin(new Insets(0, 0, 0, 0));` will leave you with a simple plain rectangle. Then you can still use `button.setText("X")`, or `setIcon` for a snazzier look wthout any need … | |
Re: Your {} are all messed up - eg on line 49 you have the } that closes the class definition, but there is obviously more code following that which should be in the class. The best way to sort out this kind of problems is to go through the code … | |
Re: Do you know how to write a loop that traverses the list from the first node to the last? | |
Re: In eclipse just File/Export select Java/Runnable Jar, fill in all the blanks (including the name of the class that contains your main method) and that should be OK. | |
Re: You are missing some essential pieces to display your barcode. 1. To display something on the screen you need to put it some kind of window, eg a JFrame. 2. The thing you put in the window is a JPanel, but you need a special one because you are going … | |
Re: 1. You can combine multiple tests in one `if`, in this case ORing them together, as in `if ( site.next(1) == null || site.next(2) == null ...` 2. The contents of the `if` must be a boolean expression, so you don't need an `if` to turn that into a booelan … | |
Re: I don't know of any way to make an application "crash" by entering stuff into a dialog, but things like invalid dates can cause an Exception to be thrown when you try to parse them- and if you don't catch and handle that properly your program will terminate. | |
Re: Instead of an ActionListener you need a ListSelectionListener, which will be called whenever the user selects a city in the list. `list.addListSelectionListener(new MyListSelectionListener())` Check out the API JavaDoc for details of the ListSelectionListener interface and the ListSelectionEvent that it uses. | |
Re: / means divide! % means remainder 1/3 = .33333, truncated to 0 in int arithmetic 1%3 = 1 (3 into 1 goes 0 times with a remainder of 1) | |
Re: 1. Norm is giving you good advice. Real programmers test early and test often. Adding more code to 200 lines that are probably full of errors is just making your life hard. 2. `Collections.sort` sorts objects that implement the `List` interface. Since your class doesn't implement it you can't use … | |
Re: Sure. Suppose I had an array of Car objects, I could find the Fords with something like for (Car c : myArrayOfCars) { if (c.getManufacturer().equals("Ford") ... } | |
![]() | Re: Norm's advice makes perefect sense. Alternatively, if you are going to be doing a lot of these drawings (eg animation) then you can create a new BufferedImage, get its Graphics, and draw your Shapes into that Graphics. Now you have your creature as a simple Image that you can draw … ![]() |
Re: Here's the problem: you are writing your data to the output stream as ASCII text, but you are reading from your input streams as raw bytes. It's essential that you read and write using the same format, eg if you print to the output then you should use readLine to … | |
Re: anand01 Please don't throw code like that at someone who is trying to learn. At the very least explain it properly. Even better explain enough for the OP to write it himself. That's the best way to learn. | |
Re: This is easier if you implement a full MVC (model-view-controller) architecture. In that case the controller handles the "logical" meaning of the user input by performing the appropiate method calls to the model (your spreadsheet). It's a bit tedious, but not difficult, to create a log of the calls that … | |
Re: In your paint method, draw your images with an alpha (opacity) less than 1.0 Something like: float alpha = 0.5; composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha); Graphics2D g2D = (Graphics2D) g; g2D.setComposite(composite); g2D.drawImage(... | |
Re: You may want to look at line 29. The value returned for EOF is -1. According the the JavaDoc a value of zero will never be returned, but I have a fuzzy memory of seeing a problem where read did return zero sometimes from an input stream that was network-based. … | |
Re: The priority queue sorts your orders according to the comparator that you supply. The comparator must take two orders and determine which is highest priority - that's just some if tests using the urgent and booked in advance fields from the two orders. The StringLengthComparator in your code is just … | |
Re: The standard Java GUI library is called "Swing", which is built on, and still uses many classes from, an earlier version called "AWT". Swing classes have names that begin with a J, as in JFrame, JLabel, JButton etc. Swing GUIs are portable across all platforms where Java runs. SWT was … | |
Re: You need to wait for input from the local user and at the same time you need to wait for input from the remote end, and at the server you also need to wait for new client connections. Although it is technically possible to do that in a single thread … | |
Re: Yes, it's very likely that the heap overflow is caused by an excess of recursion, however I don't think your max recursion depth should increase so fast with array size (shouldn't it scale with log(n)?). Try putting one or more print statements in each of the methods to show what … | |
Re: The keyword "new" is wrong here. You are just calling the static method "forName" in the Class class. It's not a constructor. `Class ourClass = Class.forName("com.app.something");` | |
Re: The API doc for removeAll says: > This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes. | |
Re: You extend an abstract class but implement an interface. You can only extend one class, so that limits their usefulness. In general an abstract class makes most sense when it contains a larg-ish amount of code that its subclasses can inherit and use. | |
Re: Can you confirm the exact message and the line it refers to? The line you quoted (38) will only throw an NPE if hp is null, but to get there it must have executed line 24, so hp cannot be null (or I'm mis-reading your code, in which case, apologies) | |
Re: What's the complete exact error message from the compiler? | |
Re: Why not do this in AppleScript? - lots of examples and suggestions on the web. | |
Re: > Is there any way to make the method accepts any length from the input? Tecnically yes, you can use `Array1(int... n)` which declares a variable nunber (>=0) of int params - inside your method you will see n as an int array of the appropriate size. But in practice … | |
![]() | Re: The easiest way is just to use a "enhanced" for loop, as in for (Integer i : list) { sum += i; // etc } ![]() |
Re: Put a load of print statements into your code, printing the values of the key variables at each stage so you can see where it's going wrong. | |
Re: I wouild expect that to print "ping" and "pong" (or "pong" and "ping"), then wait 10 secs, and repeat. What does yours do? How ***exactly*** does that differ from what you expect? | |
Re: On line 10 you have a { where you should have a } | |
Re: You can use your text field's ` getText()` method to get the String that was typed in. You can compare that to any other String by using its `equals` method. Then you can use an if test to set the text of the label. | |
Re: > The output of the following line of code is: Not Done End Not according to Java. I just copied and executed the code you posted and it outputted Not End, exactly as expected. Have you tried it? | |
Re: You haven't gone wrong, the strange String you see is the default toString method that everything inherits from Object ( [ = array, I = int, @xxx = hash code ). The easiest way to print the contents of an array is with the `Arrays.toString(myArray)` method. | |
![]() | Re: I built a lttle training demo on exactly this theme a while back, and it's not easy for someone who has "just started learning Java". You're going to have to build some specific skills and cod techniques first before you try to put them all together into this app. I … ![]() |
Re: > I am suppose to just construct a Room with the possible components and then create a 5x5 TwoD array of type Room in the Dungeon class and then create a Dungeon object in the DungeonAdventure class. Looks like you are heading in the right direction. You need an array … | |
Re: Perhaps it's a task switch thing? You could try a sleep(1000) after taking the screenshot but before accessing the clipboard? | |
Re: You may have "enough braces", but you don't have an equal number of { (five) and } (four). Proper indntation would make the error immediately obvious. | |
Re: Looking at your code I can't see code that would display anything on the screen/in the browser window. You just print things to System.out, which is the java console. To display things on the screen/in the browser window you will need to useSwing components such as JLabel or JTextArea | |
Re: On line 6 you declare, but do not initialise an array variable called className, so its initial value is null. That's the one you try to use on line 72, which is why you get an NPE. Your code on lines 24-29 has absolutely no effect on this because that … | |
Re: You don't have to use Strings to populate a JList - you can use any Objects. JList by default will use the objects' toString() method to get the text to display in the list. . So you can add Book objects to the JList - Then `list.getSelectedValue()` will return the … | |
Re: > Use an enhanced for loop to sum the values in the array. Your loop is the traditional kind, not an enhanced for loop https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with | |
Re: Fortunately Sun/Oracle have already written 99% of the code you need. It's all in the standard API. Use the ImageIO class to *read* the file into memory, ImageIO will handle all the details of decompressing the jpeg for you.That gives you an in-memory BufferedImage, so you can use its getRGB … |
The End.