I wrote the following code. It's a program which uses Binary Search to locate an element. I try to utilize exception handling to mitigate against the usage of decimal numbers. My intention is for the program to act as follows:
If decimal Number is entered, tell the user that decimals are not allowed and re-run the main method.
What is happening is strange. The main method appears to be running multiple times. Why is this?
My other question is in regards to a bug in my code... If I search for "6000" (the last value in my array) my program tells me that it can't find it. I'm not sure why...
posted 1 month ago
I fixed the small bug in my code. I was incorrectly adding 1 if the mid-point was larger then the key, and incorrectly subtracting 1 if the midpoint was smaller than the key.
Don't create a new Scanner object reading System.in. You need one such Scanner and only one. That means one per application, and the best way to do that (I think) is to create a KeyboardInputs (or similar) utility class. Please search my posts for utility class Scanner and you should find some useful hints.
The loop Carey showed you is a much more elegant way to ensure you get an int into your program, but it ensures you never have an input mismatch exception, so you don't need a try‑catch anywhere. Your line 23 suggests that you would only enter numbers; it is very easy to miss the appropriate keys and enter letters, so your input wouldn't be a number at all. The message might read better as, “only#xa0;whole numbers,” or similar.
Don't declare input mismatch exception with a throws clause; reserve throws XYZException for checked Exceptions.
Why have you marked anything static except the main method?
Campbell Ritchie wrote:Don't create a new Scanner object reading System.in. You need one such Scanner and only one. That means one per application, and the best way to do that (I think) is to create a KeyboardInputs (or similar) utility class. Please search my posts for utility class Scanner and you should find some useful hints.
While I'm trying to find Campbell's utility class you might look at my Scanner utility. Scroll to the end.
Campbell Ritchie wrote:Why have you marked anything static except the main method?
I marked my variables static because I thought that's what I had to do in order to use them in the main method.
I didn't quite get your point... isn't that the only way to use a variable within a static method?
posted 1 month ago
The correct thing to do is to get that code out of the main method and into instance methods.
Static variables completely kill the maintainability of your software, because instances of types that contain static variables can not be used as components for other objects. A component is an object that is completely controlled by the object that is composed of the component. With static variables, by definition the object can not be completely controlled by one object only. Here's an example:
For sake of clarity, I didn't make this code thread-safe. Here, Stopwatch is composed of a Counter. It creates the counter object and never exposes a reference to the counter to the outside world, so it completely controls what happens to the counter. Normally, it could use this to make certain guarantees, for instance that the getMinutes() method never returns a negative value. That's because the Stopwatch is the only thing that has access to the Counter, and it only ever calls the increment() method with a positive value.
However, because every other method in the entire application (including malicious code that was possibly injected somewhere) is able to call Counter.confuseEverybody(), a lot of invariants on Stopwatch no longer hold and the application will start to exhibit very weird behavior.
Only use the static keyword for constant fields, pure functions or factory methods or "extension methods".
A constant field is a final field of a primitive type, or an immutable reference type, such as String.
A pure function is a method whose behavior only depends on method parameters and constant fields. Calling the method twice with the same parameters will return the same value twice.
A factory method is a method that returns a new instance of an object. Factory methods often don't return the same object twice, but if the factory method is called twice with the same parameters, the returned objects should be as similar as possible.
An "extension method" is a method that performs a useful task with side-effects on one of its method parameters. You would only write such a method if you can't add an instance method to the class of the method parameter because you don't have access to the source code.
So how do you access instance fields or instance methods from a static method? You need an object reference to the object that you want to access the member of. The best way to get such an object reference is to declare it in the static method's parameter list, or to retrieve it through an object passed as a method parameter. However, because the main() method is the first method to be called, it is responsible for setting up the application by itself, so the only way to do this is to create the needed objects by calling factory methods or using the new keyword.
The main() method is static, but it's able to call the non-static start() method on Stopwatch, because it first created an instance of Stopwatch with the new keyword. If there are many things to set up, it's better to create a new instance of the main class and call instance methods on the new object:
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database