226 Posted Topics

Member Avatar for JamesCherrill

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 …

Member Avatar for JamesCherrill
2
345
Member Avatar for gronkite

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 …

Member Avatar for valdez25
0
250
Member Avatar for cmps

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. …

Member Avatar for cmps
0
235
Member Avatar for aabbccbryanmark

Any container can always contain any component. That is why Swing and AWT are organized into containers and components.

Member Avatar for aabbccbryanmark
0
2K
Member Avatar for michelleruth

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 …

Member Avatar for stultuske
0
85
Member Avatar for somjit{}

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, …

Member Avatar for somjit{}
0
194
Member Avatar for milkman93

You want `java.lang.String.format` as in: jl.setText(String.format("The answer is: %.2f", ans));

Member Avatar for milkman93
0
5K
Member Avatar for slygoth
Member Avatar for solomon_13000

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()` …

Member Avatar for solomon_13000
0
432
Member Avatar for wschamps42

`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 …

Member Avatar for bguild
0
270
Member Avatar for M4trixSh4d0w

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).

Member Avatar for M4trixSh4d0w
0
190
Member Avatar for abzksm

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, …

Member Avatar for rubberman
-1
132
Member Avatar for HankReardon

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 …

Member Avatar for HankReardon
0
3K
Member Avatar for StefanRafa0

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 …

Member Avatar for mKorbel
0
286
Member Avatar for iciaguevara

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 …

Member Avatar for edensigauke
0
198
Member Avatar for waqas.zafar.125

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, …

Member Avatar for ravenous
0
155
Member Avatar for uknown2

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 …

Member Avatar for bguild
0
2K
Member Avatar for somjit{}

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` …

Member Avatar for somjit{}
0
299
Member Avatar for StefanRafa0

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 …

Member Avatar for bguild
0
439
Member Avatar for connor.wells.7946

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 …

Member Avatar for rubberman
0
386
Member Avatar for engrjd91

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 …

Member Avatar for stultuske
0
218
Member Avatar for richard.haines.39

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: …

Member Avatar for bguild
0
720
Member Avatar for VengefulToast
Member Avatar for Fernando_1

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, …

Member Avatar for Fernando_1
0
105
Member Avatar for DevilDog22

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 …

Member Avatar for DevilDog22
0
116
Member Avatar for ladybirdchance

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 …

Member Avatar for ladybirdchance
0
178
Member Avatar for fonzi

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` …

Member Avatar for fonzi
0
267
Member Avatar for sh4rif

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 …

Member Avatar for Moschops
0
315
Member Avatar for hwoarang69

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 …

Member Avatar for bguild
0
257
Member Avatar for hwoarang69

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 …

Member Avatar for hwoarang69
0
261
Member Avatar for djcrab

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 …

Member Avatar for bguild
0
242
Member Avatar for piliff82

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 …

Member Avatar for JamesCherrill
0
764
Member Avatar for carrl00

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 …

Member Avatar for stultuske
0
299
Member Avatar for helloworld1234

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.

Member Avatar for helloworld1234
0
166
Member Avatar for bguild

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 …

Member Avatar for Ancient Dragon
0
449
Member Avatar for godzab

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 …

Member Avatar for JamesCherrill
0
214
Member Avatar for tricket_7

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 …

Member Avatar for tricket_7
0
909
Member Avatar for nova4005

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 …

Member Avatar for bguild
0
549
Member Avatar for erms

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 …

Member Avatar for erms
0
605
Member Avatar for peymankop

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 …

Member Avatar for IIM
0
98
Member Avatar for dadquacker

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 …

Member Avatar for ToddRLittleton
0
294
Member Avatar for bguild

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 …

1
134
Member Avatar for tricket_7

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 …

Member Avatar for bguild
0
785
Member Avatar for maxbummber

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 …

Member Avatar for maxbummber
2
294
Member Avatar for Fernando_1

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); …

Member Avatar for bguild
0
171
Member Avatar for jalpesh_007

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`.

Member Avatar for jalpesh_007
0
194
Member Avatar for adecker0033

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 …

Member Avatar for stultuske
0
207
Member Avatar for michelleruth

I think this should solve that problem: http://en.wikipedia.org/wiki/Reservoir_sampling

Member Avatar for radhakrishna.p
0
24K
Member Avatar for Violet_82

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 …

Member Avatar for Violet_82
0
532
Member Avatar for requimrar

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 …

Member Avatar for bguild
0
1K

The End.