• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Handling  RSS feed

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my task:

Revise exercise 5.3, which prompts for four integers and finds the area of a circle. Create a new project for this named AreaCircleException and copy your methods from lesson 5.3.

In that lesson, you should have used the scanner class to obtain your integers.  The scanner nextInt method throws the exception InputMismatchException if it fails to find an integer in the input.

Write ONE method that takes a string as a parameter and returns an integer.  This method should use the scanner class to get an integer and catch the exception.  Be sure to prompt the user again for the integer if an exception is caught (this means loop!).  The String parameter will contain the prompt for the user.  Declare your method like this:

public static int getScannerInt (String promptStr)

and call it like this:

int x1 = getScannerInt("Enter x1");

You don't need to worry about handling negative x and y values unless you want to.  It should be similar to the getAge method above, except you will not use JOptionPane.  Using the scanner, to get a number, use code similar to this:

number = reader.nextInt(); 

where number is defined as an integer.

Test, debug, and when done, submit to the drop box.   



This is my previous code that I have to change.



My problem is I do not know where to start. I get what I have to do, but I am not sure how to do it. Any help is appreciated.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, you've been given the signature, and the description implies that the method should be used for all the places where you currently have this sort of code:


So, that's a prompt and then the read.

That would be my first step.

The next step would be modifying the method to handle the exception and reprompt the user if it's not a valid int.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a method of avoiding that Exception altogether which only works for keyboard input, but if you have been told to catch the Exception, you will have to catch it.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:OK, you've been given the signature, and the description implies that the method should be used for all the places where you currently have this sort of code:


So, that's a prompt and then the read.

That would be my first step.

The next step would be modifying the method to handle the exception and reprompt the user if it's not a valid int.


Could you break that down a bit more? I don't entirely get what you mean.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A step at a time.
First write the method, which can be simply an empty shell that returns a 0.

Then add in the prompt and nextInt calls, similar to the lines I quoted above, but the prompt text will be from the String parameter (promptStr).

That will give you a number you can return instead of the 0.

Don't worry about the exception until you have that lot in place.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:A step at a time.
First write the method, which can be simply an empty shell that returns a 0.

Then add in the prompt and nextInt calls, similar to the lines I quoted above, but the prompt text will be from the String parameter (promptStr).

That will give you a number you can return instead of the 0.

Don't worry about the exception until you have that lot in place.


I have updated my code:



NetBeans tells me that getScannerInt(String promptStr()); is incorrect. What did I do wrong?
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't include the type of the method when it's an argument.  In simpler terms, remove the "String".
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
NetBeans tells me that getScannerInt(String promptStr()); is incorrect. What did I do wrong?


Well, basically, it is invalid syntax. It doesn't make sense to declare what a method returns when you are calling the method. The only time the return type needs to be specified is when the method is declared -- and not when it is used.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And BTW ...
Lexi Turgeon wrote:



This, when called, will be an infinitely recursive method call. You will end up with a stack overflow error condition.

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the example in your requirements they have this:


Now, compare that to the call you've made.

Also, let's look in your method:

This will result in the method calling itself.

The method (according to the requirements) is supposed to replace the prompt/read lines, so I would expect to see a println() that prints the promptStr, and then a scanner.nextInt() call to read in the int.

That last part will raise an issue, though, in that you either need to create a Scanner each time this method is called, or create a single global Scanner.  Not knowing where you are in your learning it's going to be hard to make a proper suggestion for that one.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:. . .. . .
Leave one empty line between successive methods.
You can replace the first method by one which simply calls a method of the Math class. I'll give you a hint: you sound as though you were calculating the hypotenuse of a triangle even though you are actually looking for radii of circles and the method in the Math class is intended for radii for rectangular→polar coordinate conversion. Unless you have been told to do things the hard way, that is.
I suggest your area method shou‍ld return the area rather than printing it. And don't call the parameter distance: call it radius. The () in line 28 are redundant; it makes no difference whether they are there or not, because of the properties of the multiplication operator *
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still very confused as to what I should be doing to my code. Can anyone really dumb it down for me? Sorry I am very new to this.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instructions wrote:
Write ONE method that takes a string as a parameter and returns an integer.  The String parameter will contain the prompt for the user.  Declare your method like this:

public static int getScannerInt (String promptStr)

You've got that.

This method should use the scanner class to get an integer and catch the exception.  Be sure to prompt the user again for the integer if an exception is caught (this means loop!). 

You don't have this yet.

and call it like this:

int x1 = getScannerInt("Enter x1"); 

This is where you're confused, I think.  In the main() method, instead of this:

...you should just have this:

Now back to your getScannerInt() method.  Remove all those "int x1 = ..." lines (four of them).  You should just have this:

Now we want to put this code inside the method, but with improvements:

First, let's correct something.  x1 should be an int, not a double.  Notice that the reader method is readInt().  Now we need to display (the println() statement) the prompt that's passed in.  That's promptStr in public static int getScannerInt (String promptStr) and it's called a parameter.  You can use promptStr instead of "x1: " in the println().

Do this much and post the code.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My code has been updated. The only issue is the part reader in the reader.nextInt(); lines of the code is being claimed as incorrect by NetBeans.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
My code has been updated. The only issue is the part reader in the reader.nextInt(); lines of the code is being claimed as incorrect by NetBeans.


Local variables are only in scope in the block where it is declared. You can't share local variables like that.

Perhaps you should consider using variables with a bigger scope? Such as instance or static variables?

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Lexi Turgeon wrote:
My code has been updated. The only issue is the part reader in the reader.nextInt(); lines of the code is being claimed as incorrect by NetBeans.


Local variables are only in scope in the block where it is declared. You can't share local variables like that.

Perhaps you should consider using variables with a bigger scope? Such as instance or static variables?

Henry


How would I do this?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
Henry Wong wrote:
Perhaps you should consider using variables with a bigger scope? Such as instance or static variables?

How would I do this?

The Java tutorial on variables, from Oracle, is a good place to start regarding the different variable types ... https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Lexi Turgeon wrote:
Henry Wong wrote:
Perhaps you should consider using variables with a bigger scope? Such as instance or static variables?

How would I do this?

The Java tutorial on variables, from Oracle, is a good place to start regarding the different variable types ... https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Henry


Let me rephrase my question, which variables of the ones I have now should I change?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update:



This is my updated code. My only problem is the line that is double area = areaCircle(x1, x2, y1, y2);. NetBeans tells me that it is incorrect but I do not see what is wrong with it. Below is my compiler error.

 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You are calling areaCircle() with four parameters but you have defined it as taking a single parameter.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, your getScannerInt() method is completely ... well ... not used.

Yes, you were able to solve the compiler error by declaring another scanner instance -- and avoid using an instance or static variables completely. This is not very efficient, but at least, it worked.

However, I am going to speculate that you encountered a similar issue, and you tried to fix it by moving the code from the getScannerInt() method into the main() method. Unfortunately, this violates the requirement of your homework assignment. Additionally, creating another local variable, named getScannerInt, does not mean you are using the getScannerInt() method. The method is not used. You are simply using a local variable with the same name as the method.

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Also, your getScannerInt() method is completely ... well ... not used.

Yes, you were able to solve the compiler error by declaring another scanner instance -- and avoid using an instance or static variables completely. This is not very efficient, but at least, it worked.

However, I am going to speculate that you encountered a similar issue, and you tried to fix it by moving the code from the getScannerInt() method into the main() method. Unfortunately, this violates the requirement of your homework assignment. Additionally, creating another local variable, named getScannerInt, does not mean you are using the getScannerInt() method. The method is not used. You are simply using a local variable with the same name as the method.

Henry


How could I apply what is used in my main method that I want to be used in my getScannerInt and have it so that my getScannerInt works?
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's back up a bit.  In one version, you correctly had:

Here you are getting x1, x2, y1, and y2 from the method getScannerInt() in main().  The getScannerInt() method is called four times, sending the correct prompt each time.  Is that concept clear?

So in the getScannerInt() method we want to put the code for each one of those calls.  You had this:

There are two problems with this call.  First, it prompts and reads the variables four times.  We want to get one variable and return it to the caller, so:

But we are still returning 0 to the caller.  We want to return the value in x1.  So,

Does this make sense to you?  Make these changes and only then go on to put your loop that checks for invalid entries in getScannerInt(), not in main().
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Let's back up a bit.  In one version, you correctly had:

Here you are getting x1, x2, y1, and y2 from the method getScannerInt() in main().  The getScannerInt() method is called four times, sending the correct prompt each time.  Is that concept clear?

So in the getScannerInt() method we want to put the code for each one of those calls.  You had this:

There are two problems with this call.  First, it prompts and reads the variables four times.  We want to get one variable and return it to the caller, so:

But we are still returning 0 to the caller.  We want to return the value in x1.  So,

Does this make sense to you?  Make these changes and only then go on to put your loop that checks for invalid entries in getScannerInt(), not in main().


All of that does make sense now, thank you. Now I do not know what my loop should be. Where would I start with figuring out my loop?

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start like this, either thinking about it or writing it down:

  • I want to prompt the user for an integer
  • I want to get the integer
  • If there's an error, I want to loop back and start at the prompt again
  • If there's no error, I want to exit the loop


  • Next, write some pseudocode:  (Do you know about do/while loops?  If not, you can use a while loop.)

    Turn this input Java code.  Is it clear now where this should go?
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lexi Turgeon wrote:. . . . Where would I start with figuring out my loop? . . .
    You don't. You write down in words of one syllable what you want the loop to do. When you know what the loop is to do, then you can start to write it. If you use nice small words to tell us the loop's use, you will find it not at all hard to write the code for the loop.

    Look up rubber‑duck programming, too.
     
    Lexi Turgeon
    Ranch Hand
    Posts: 156
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:Start like this, either thinking about it or writing it down:

  • I want to prompt the user for an integer
  • I want to get the integer
  • If there's an error, I want to loop back and start at the prompt again
  • If there's no error, I want to exit the loop


  • Next, write some pseudocode:  (Do you know about do/while loops?  If not, you can use a while loop.)

    Turn this input Java code.  Is it clear now where this should go?


    This was as far as I was able to get with that information.



    Is any of what I have correct?
     
    Lexi Turgeon
    Ranch Hand
    Posts: 156
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    *Update* I have another question, does my try and catch loop go in my main method?
     
    Dave Tolls
    Ranch Foreman
    Posts: 3056
    37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No.
    From what I can see in your requirements the try/catch block, and the loop it will sit in, goes in the getScannerInt method.

    The code that Knute put up, note, is pseudo-code.  It is intended to show the flow of logic.  It is not Java code.
    You'll have to translate that into Java syntax.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!