226 Posted Topics
Re: I would prefer `myMap.values().iterator().next()`. It is also a bit tricky and potentially confusing, but it has the benefit of being type-safe. With a comment to explain what I'm doing, I wouldn't feel too bad about it. On the other hand, it seems that most of the awkwardness comes from you … | |
Re: In a normal binary search you take a section of the array and search halfway between the start and end of that section, then discard one half of the section and take the other half for the next iteration. It looks like you are trying to do that, but instead … | |
Re: All parameters work the same way, passing the value of the argument. That means you always get a shallow copy, but when the value is a reference to an object, that means nothing more than a copy of a reference, not a new reference to a copy of the object. … | |
Re: Any container can always contain any component. That is why Swing and AWT are organized into containers and components. | |
Re: To display things on a screen you need to ask for it explicitly, for example using some of the many tools available in [Swing](http://docs.oracle.com/javase/tutorial/uiswing/start/index.html) or using `System.out` which is a [java.io.PrintStream](http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html). If you only tell Java to do a thing, it will only do it and report nothing back to … | |
Re: Floating the ends of the array surely means nothing more than allowing the start and end of your queue to move within the array. Just use a field to store the index of the `head` of the queue and another to store the index of the `end` of the queue, … | |
Re: You want `java.lang.String.format` as in: jl.setText(String.format("The answer is: %.2f", ans)); | |
Re: You have an extra `)` near the end of line 7. | |
Re: Calling `addObserver` is not optional if you want want observers to be notified of updates. Also, `c.addObserver(new A())` will not accomplish much because `new A()` is a `JPanel` and that means it needs a visible container or else it cannot be visible. Since you never do anything with `new A()` … | |
Re: `java.io.RandomAccessFile` is naturally used for binary files, not text files. The problem is that text files are normally formatted according to how humans read files, not with data aligned to addresses within the file. It seems to me that it is probably a mistake to try to make a file … | |
Re: It seems that you want to translate by (1, -1) and then scale by (200, -200). For example, (1, -1) would translate to (2, -2), and then scale to (400, 400). | |
Re: This seems more like an algebra problem than a programming problem. You really don't need much more than a subtraction and a division; the only trick is figuring out exactly what to subtract and divide to find the result you want. You can work that out with a pencil, paper, … | |
Re: The way to be absolutely certain about that sort of thing is called *encapsulation*. Wrap the thing you want to sort in a class and make it private so nothing can ever touch it except through methods that you design. If you are sorting an int array, you could do … | |
Re: You should never call `Thread.sleep` in the event dispatch thread. I don't know if `afrojackActionPerformed` is being called in the event dispatch thread, but from its name I guess it is, and that is surely the problem. Events should be handled quickly so that your GUI can handle other events … | |
Re: Is this GUI generated by a tool? I think this problem is more proof that creating a GUI in this style should not be done by hand. You should either use a tool to lay out the GUI visually or use a `java.awt.LayoutManager` to arrange your components. If you don't … | |
Re: My first thought is to make a guess about the width of the grid and allocate that for the first line, and if that's not enough allocate a larger array and copy what you have read so far into it. Repeat that until you have read the entire first line, … | |
Re: You code would be easier to read if you broke it up into smaller methods. I think it is important that loops never be long. If you can't see the top and bottom of the loop at the same time without scrolling, that is a huge warning sign. It's hard … | |
Re: I don't see what you mean by "issues relating to scope." At least there is no problem with the scope of `perC`. It looks like you haven't decided which `perC` you want, since it is given many values, one for each run. If you really haven't decided which run's `perC` … | |
Re: After a few experiments I find that I get good results with implementing a custom `JPanel` to hold a `JTextField`. Instead of putting the text in the `JTextField`'s `Document`, I override `paint` on the `JPanel` so that I can draw over the children of the panel and use that to … | |
Re: Your `STACK` array screams "stack" at me. If you must not use stacks then that may mean trouble for you. Surely your `MAX` should be large enough to allow `input` hold your input file, so why is it only 50? Why are you filling the output file with the first … | |
Re: If it were my project I would choose to write a compiler for a programming language that I designed myself. The complexity of the language would depend upon how much time I had. Thinking in that same direction, I might also want to create a library for Java virtual machine … | |
Re: By default `DecimalFormat` uses nonlocalized patterns, which means you need to use `','` as your digit group separator in the pattern. It is done this way so that you can use your pattern anywhere, but it is also possible to use localized patterns. For example, this works fine for me: … | |
| |
Re: The problem is here: a= kb.nextDouble(); operator = kb.nextLine(); b = kb.nextDouble(); Getting the first `nextDouble()` reads up to the next delimiter (which means whitespace), but leaves that delimiter in the stream to be read again later. Then `kb.nextLine()` reads and returns everything up to the end of the line, … | |
Re: I understand that you are new to the language, but for something like this you need to use an array or something similar. If you haven't learned about arrays or `java.util.List`, then you should either learn one of them now or choose a different project to learn on. You should … | |
Re: Tracking down the cause of a `NullPointerException` is a simple matter of looking at the top of the stack trace which indicates the exact place where a null pointer was encountered. If the top method isn't one that you wrote, then go down the stack until you find a method … | |
Re: You can only use the notation for an array index on an array. When you do `list[i]`, it is only allowed if `list` is an array, and `list` is a `java.util.ArrayList` according to your error message. Even with its suggestive name, `java.util.ArrayList` is not a kind of array. Fortunately, `ArrayList` … | |
Re: Starting a number with `0` means that it is an octal number which means that all digits are at least `0` and at most `7`. An octal number looks like a decimal number but it has a different meaning. You must be wary of accidentally using an octal number when … | |
Re: I don't think you should be creating a new player every time you paint. I'm not certain I fully understand what you are trying to accomplish, but painting and creating players seem to be completely unrelated concepts to me. On the other hand, perhaps creating the player doesn't matter since … | |
Re: Do you really want to draw these rectangles at (`x`, `y`)? That's only going to put one pixel between the top-left corner of the first rectangle and the top-left corner of the second rectangle. I think you would get better results by drawing each rectangle at (`x * 200`, `y … | |
Re: I recommend that you give your variables more meaningful names than `y` and `z`. It will help you keep track of them when you are trying to find a problem. If `y` and `z` were simply normal index iterators in the `for` loops then they wouldn't need meaningful names because … | |
Re: Your `switch` statement is brutally repetitive. I'm curious about what inspired you to do everything five times. You can stop your loop with a `break` statement. But if you insist on doing that five times in the `switch` like you do everything else, then you will probably want to use … | |
Re: You are wise to try to break up your application into smaller methods, but you need to remember that you cannot use something from one method in another method. The variables `input`, `base`, `height`, and `area` are all owned by the `main` method and you cannot use them in any … | |
Re: The natural way to meet the requirements of the project is option C: create two separate search functions; one that has the required signature and returns true or false for success or failure, and another search function that returns the index. | |
It's not important, but I wish there were a way to control the line numbers when posting code. As it is, I've never seen code that doesn't start at line 1 and count up from there, but that's not always ideal. Sometimes there is boilerplate at the top of a … | |
Re: I recommend that you choose more meaningful variables names than `a`, `b`, `c`, `e`, `g`. It would make your `merge` method easier to read and would make finding the problem easier. You haven't included the exact line number of your exception, but I notice that you do `a[0]` without checking … | |
Re: Setting `serviceTime` to a new random number every time through the loop does not work just fine. It means that service might take any amount of time, even much more than 5 times through the loop. And to prove that you don't understand what you are doing, you are adding … | |
Re: Changing the value of `input` partway throught the loop can only lead to confusion. What if the original value of `input` is "0101A000" and then the new value is "ABCDE111"? The start of the new String won't be tested again and to the user it will look like it was … | |
Re: Almost all programs read binary data, but it's not all the same binary data and they don't all use it in the same way. No matter how you generate the binary data it may not work in this case because the program needs some specific binary data to do what … | |
Re: Your `for` loop will go forever because you set `i` to 0 or 1 at the start of each iteration of the loop. 1/1 is 1, and 1/2 is 0. So `i` will never reach `number` unless `number` is one of those two values, and the loop won't stop until … | |
Re: I highly recommend Java. Small Basic seems like an amazing teaching tool for someone who knows nothing about programming, but when it's time to start a serious project you don't want to be using a language for beginners. When you no longer need to learn what Small Basic can teach … | |
It's surprisingly how little documentation `java.io.InputStream` has for a class of its complexity and importance. Almost every Java application uses it in one way or another, but how one uses it correctly is not completely spelled out. Naturally the documentation allows most of the methods to throw an `IOException` if … | |
Re: You already have an `if` statement for adding new customers to the queue, so why not print your message there? You should probably also increment a counter in there so you don't have to give every customer the number `1`. You probably also want an `if` statement for removing customers … | |
Re: That sounds like a fun project. I would have a `GateBuilder` class and a `Wire` class. The `Wire` class is most important because it would hold everything together and everything depends upon it. It would have a state which would be `true` or `false` (or `ON` and `OFF` if you … | |
Re: The simplest solution to align the `'$'`s would be to merely insert a few spaces, like this: System.out.printf("\nRegular pay: $ %.2f",regularPay); System.out.printf("\nOvertime pay: $ %.2f",overtimePay); System.out.printf("\nTotal Pay: $ %.2f",overtimePay+regularPay); You could also do something like this which you might prefer: System.out.printf("\n%-15s$ %.2f", "Regular pay:", regularPay); System.out.printf("\n%-15s$ %.2f", "Overtime pay:", overtimePay); … | |
Re: The capacity 44 is two plus twice the old capacity of 21. In other words `sb.capacity() = 44 = 2 * 21 + 2`. That's what the documentation for `ensureCapacity` says it should be. I hope everyone is aware that `StringBuilder` is superior to `StringBuffer`. | |
Re: You haven't told the compiler what to return if there are no seats available. Most people would guess that `false` should be returned, but the compiler needs that to be spelled out explicitly. It is complaining because if the outer loop finishes then it will run into the end of … | |
Re: I think this should solve that problem: http://en.wikipedia.org/wiki/Reservoir_sampling | |
Re: Just like a random number, the seed of a random number generator is supposed to be meaningless. Since we have pseudorandom numbers instead of actual random numbers it might be possible to work out some meaning in the seed you give to `Random`, but unless you find that the pseudorandom … | |
Re: Start by counting the number of substances involved, including both reactants and products. That will be the number of columns of your matrix. Since your equation is already parsed, I expect that number is easily obtained. Next you create a list of the elements involved. For example if H2O is … |
The End.