226 Posted Topics
Re: You can go row-by-row down the rows, starting with all `false`, then for each row after that you go from right-to-left along the row filling the entries based on the previous row's entries. Until you find a `false` in the previous row, make each entry `false`, then where there is … | |
Re: Choose a unit of time, something that divides all the important time durations you will be dealing with, such as how long it takes to process food, to pack food, and to unjam a machine. Then all you have to do is make a loop where each iteration represents one … | |
Re: It seems to me that `e.getKeyCode() == ((KeyEvent.VK_UP) | (KeyEvent.VK_DOWN))` represents a shocking misunderstanding of Java operators on some fundamental level. When the event is for the "up" key, `e.getKeyCode()` will be equal to `VK_UP`. When the event is for the "down" key, `e.getKeyCode()` will be equal to `VK_DOWN`. I … | |
Re: I would start with an `enum` of months that can translate numbers to months and gives each month a length, like: enum Month { JANUARY(1,31), FEBRUARY(2,28) /* TODO: Other months */; private static Map<Integer,Month> monthNumbers = new HashMap<Integer,Month>(); static { for(Month m: values()) monthNumbers.put(m.number, m); } public static Month forNumber(int … | |
Re: Ignore anyone who tells you that extending a class is a good way to reuse the implementation of that class. The only good reason to extend a class is so that code can use objects of your new class as though they were objects of the class you are extending. … | |
Re: I notice you make your frame visible before you finish setting it up. That's a strange decision. Why allow the user to see it before it is ready to be seen? And making it visible before you set the default close operation is asking for there to be a moment … | |
Re: > The part that I don't understand are the action listeners. > I've managed to get a java.util.timer to update the jLabel, but you said that's wrong so I'm trying to understand the javax.swing.timer. An action listener is a class that implements `java.awt.event.ActionListener` and has a `actionPerformed(java.awt.event.ActionEvent e)` method. This … | |
Re: > Ok, why is it returning false at the very end? Because most years are not leap years. For example, what would `isLeapYear(2013)` return if not `false`? | |
Re: I think you can see why you get 0 as the answer if you trace your code by hand. It doesn't take many steps to find the problem. Your `main` calls `characterCounter(test, searchChar, test.length)`. The first thing `characterCounter` does is check if `index == t.length`, but we know that `index` … | |
Re: The `add` methods of `JFrame` will add to the content pane, so there is no need to do things like `frame.getContentPane().add(temptext)` when `frame.add(temptext)` will do the same thing. The `remove` and `setLayout` methods also automatically work on the content pane. You only need `getContentPane` when you are doing something more … | |
Re: It is important to be clear about the distinction between *objects* and *classes*. `Life` is a class. The `new` operator creates a new object, not a new class. `lifeClass` is misnamed because it is a reference to an object, not a class. I won't pretend to understand the purpose of … | |
Re: A client is someone or something that uses some services. Servers naturally have clients, and sometimes the word "client" means some specific technical thing, but it will always be a thing that is on the outside of whatever you are talking about using the services that are being discussed. If … | |
Re: You should post a [SSCCE](http://sscce.org/). It not only makes it easy for people to help you; it is also a powerful step toward solving your problem yourself. In preparing your SSCCE, you may find that you don't really need help. Threads can be tricky. You can rarely depend on them … | |
Re: You may have added your `java.awt.event.ActionListener` to your button twice. Nothing you have posted shows you adding it even once, but adding it twice would cause `actionPerformed` to be called twice for each button click. An SSCCE would make finding a problem like that easy. | |
Re: The internal stack that you mention must be the [call stack](http://en.wikipedia.org/wiki/Call_stack). Every time a method is called a new element (called a stack frame) is pushed onto the call stack to hold all the local variables of the method and the place where execution will resume when the method returns. … | |
Re: You don't need to store subexpressions on a stack. You can do this with just a stack for values and a stack for (operator, arity) pairs. It would be nice to use simply a stack of operators, but you need some way to remember when each operator should be applied, … | |
Re: Print a list of the courses to `System.out`. Create a `Scanner` for `System.in`. Go into a loop where you print a prompt and wait for the user to enter a command so you can read it with your `Scanner`, then obey the command, then do it all again repeatedly until … | |
Re: According to that method a tree is a min heap if there is no node that has a value greater than one of its children. When the index is off the end of the array then it has no nodes at all, so it can't have any node with a … | |
Re: When you use `==` between two object references it will only be `true` if those two references refer to the same object. Two `String` objects can easily contain the same `char`s without being the same object. For example, `"NaN" == new String("NaN")` will always be `false` even though both sides … | |
Re: You use `-1` on line 66 when I'm sure you actually mean `NULL`. They may be represented using the same `int`, but `NULL` is more meaningful to someone reading your source code. It looks like `size` is just counting the number of items in the list. Why doesn't it just … | |
Re: The character class `\D` means anything that is not a digit 0 through 9, just like `[^0-9]`. I can't guess what you intend with `\\D+` at the start of your expression. You have forgotten to allow for spaces between your numbers and dots inside your numbers. You could represent numbers … | |
Re: Ever since I first saw people complaining about resurrecting an old thread, I've always felt that that the obvious solution is to modify how the age of a post is displayed. As it appears now, there's very little difference at a glance between "4 Hours Ago" and "4 Years Ago." … | |
Re: > You've run into a very common problem with Scanner. I often wonder why it seems to be such a common problem. The name *is* misleading, but only because `nextLine` parallels `nextInt`, `nextFloat`, and the other methods so that you might think they have parallel meanings, when actually `nextLine` means … | |
Re: The user won't be able to see any change unless you draw the change. Just doing `camera_pos_x += camera_speed` is invisible to the user. I'm sure that the change to `camera_pos_x` happens immediately when the key is pressed; the delay that you are seeing is only a delay in the … | |
Re: If you get the exact RGB value of the background then you can easily filter it out using `java.awt.image.FilteredImageSource` and a custom `java.awt.image.RGBFilter` like this: final class BackgroundFilter extends java.awt.image.RGBImageFilter { @Override public int filterRGB(int x, int y, int rgb) { if((rgb & 0xffffff) == 0x0000ff) return 0; else return … | |
Re: [Sokoban](http://en.wikipedia.org/wiki/Sokoban) is my favorite example of a game that is very easy to write but also quite fun. It is naturally graphical, but the graphics can be very simple. If being nongraphical is really important then [interactive fiction](http://en.wikipedia.org/wiki/Interactive_fiction) deserves a mention. It's a huge genre of games with no graphics, … | |
Re: All security issues are some variety of design failure. No one can sneak into your computer through the internet without an invitation, so the big questions are what design failure in the JVM makes this theoretically possible, and what is being done about it? The good news that I want … | |
Re: Is `getPasswordAuthentication(URLName name)` your method or are you overriding a method of `javax.mail.Authenticator`? If you are overriding then you should indicate that by using the `@Override` annotation. It gives you some protection from making a mistake about the signature of the method that you are overriding. | |
Re: I don't see a stack trace in the output you listed. If `background_class.paint(g, this)` caused a `NullPointerException` then your flag2 `println` would never have happened, so then you must have changed something which fixed the problem. If the exception isn't being thrown then why are you surprised that `background_class` is … | |
Re: The exercise doesn't say so explicitly, but it is clear by implication that it expects you to present the 1 or 2 option repeatedly. It certainly doesn't want you to assign many seats for each input as you are doing, because it explicitly says "assign **a** seat" (emphasis mine). So … | |
Re: The place you should look is here: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawString%28java.lang.String,%20int,%20int%29 That is the Javadoc for the the `drawString` method, and it says that the (x,y) point that you give it is "the baseline of the leftmost character." You seem to expect the (x,y) to be the top corner of the leftmost character, … | |
Re: Also, if you want to generate a large amount of random numbers (so that duplicates would happen often) and you don't have a list of elements to choose from, or don't want to copy your list so you can shuffle it, then you can also choose `n` without duplicates from … | |
Re: The problem you describe doesn't exist in the code you have provided. When I run that code, instead of having 1s where the 0s were in `ans`, `ans` has all the values from `pS` properly copied. The only problem I see is that `pS` contains numbers which are totally inappropriate … | |
Re: > i am not able to draw more than 1 image on the frame. Are you really trying to draw more than one image? Look at lines 264-269: if `b.getBIndex()` return 1 you draw one of the images, and if it returns 2 you draw the other image. In order … | |
Re: You forgot to describe the error. It seems that you expect great feats from this forum if you hope it can give you advice without first knowing your problem. Fortunately, your problem is quite straight-forward. Perhaps you are not aware of this, but `BufferedReader.readLine` takes a line from the file … | |
Re: The point of extending a class is not to reuse the code in that class. If that is all you want then you should just create an instance of that class. The real benefit of extending a class `A` is when there exists code like a method `doSomething(A a)` and … | |
Re: Don't forget to close your output writer. If you fail to close it then your output file may be incomplete or otherwise corrupted because the writer expects to be closed before it needs to finish writing the file. `PrintWriter` can take a file name directly. You don't need to wrap … | |
Re: Your loop has 79 iterations, not 78, so you might be wrong about how many numbers it generates. I will just call the total count of generated numbers `NUMS`. As you go through the loop keep of how many numbers you still need to extract. I will call that number … | |
Re: My manual count of the swaps using an insertion sort is 7. Since the array is only 6 items long, there is clearly a way to sort it using at most 6 swaps, but every number in the array is out of its correct position and there are no two … | |
Re: >Is this the shortest/easiest way of doing it or will the string override be better? Overriding `toString` in `Pair` would be much easier. Then you could just print the list with `System.out.println` as you were trying to do before, and it would give you the result you expected. | |
Re: If you want a run an exe file by running a jar file, then you might want to consider [JPC](http://jpc.sourceforge.net). It is an x86 PC emulator made purely out of Java and it supposedly works quite well. You would need to have your exe and JPC in the jar at … | |
Re: I think autocompletion for JTextComponents is a great idea, but I'm slightly disappointed by that implementation. With this one, you just press tab and it instantly finishes your word for you. It doesn't display a list, but you can press tab repeatedly to get new guesses or type more letters. … | |
Re: Unless the file must be readable by a text editor, you are going to want `java.io.DataOutputStream` instead of `FileWriter`. Just go like this: java.io.DataOutputStream out = new java.io.DataOutputStream( new java.io.BufferedOutputStream(new java.io.FileOutputStream(file))); Then you can use the `getModel` method on your `JTable` to get the content of your table and write … | |
Re: Traditionally a mediator has at least two colleagues and it has pointers to its colleagues. You seem to have three mediators (`BaseMediator`, `Producer`, and `Consumer`) and only one colleague (`Driver`), and none of your mediators have a pointer to the colleague. This seems like a group of three highly unconventional … | |
Re: Why do you modify `hS` and `hE` even when those variables will never be used again? Why do you call `highestSalaryHelper` and not use its return value? Isn't its return value supposed to be an employee with the the highest salary? I thought you wanted that employee. Why does `highestSalaryHelper` … | |
Re: Normally when someone says "random access" the meaning is that you can access any element you want, not that the element you get is chosen for you by some random process beyond your control. Since you actually do mean to give the client no control over which element gets dequeued, … | |
Re: I have done some experiments. I never encountered an `IndexOutOfBoundsException`, but it seems that `ImageIO` is unreliable when there are multiple images on a single stream. I found that `ImageIO.read` would return `null` a few times after each image when it should have been blocking to wait for the next … | |
Re: You need to remember that the `synchronized` keyword is the thing that protects you from having two threads running at the same time. As long as you are in a `synchronized` block there is only one thread, but as soon as you leave that block the threads may both start … | |
Re: You should not be using possibly incorrect conditions. When you check for preconditions on your methods you are doing the only thing that can keep your methods safe from unruly clients; it is your only line of defense. Test for exactly the conditions that you want. I would say that … | |
Re: This is how I like to implement a calculator. It's longer, but it gives you more flexibility for adding operators. I've included some extra operators to illustrate that. I've kept comments minimalistic for brevity, but I hope the idea is clear. import java.util.*; /** Evaluator for expressions of type V … |
The End.