Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

How to gracefully catch exception and re-run method?  RSS feed

 
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Mark Richardson
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I fixed it like so:



but my exeception handling problem still remains.
 
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am told the searches I told you about aren't working. Try here for an incomplete utility class, another similar example here (you might be able to use that example unchanged) and an example of a method to deal with the nextLine() method here (Beware: you must read the whole thread). I think I would deal with nextLine() differently now.The strip() method only exists in Java11+; for older versions try the trim() method.
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Mark Richardson
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Campbell Ritchie
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct thing to do is to get that code out of the main method and into instance methods.
 
Saloon Keeper
Posts: 9986
206
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!