• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception handling problem and counter issue  RSS feed

 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I found a place that focuses on being friendly to rookies. Have been slammed and shamed for asking simple questions on some other sites.

I am working on an assignment working with exception handling and have two unrelated issues.

First, I need to catch exceptions if the array that is passed is null and a different exception if the length is 0 (which is the case if the array is null, but I'm supposed to throw an exception for both.

This is what I am using at the moment:



The other issue is searching an array for a value (which is just a secondary part, the exception part of this section of the assignment isn't the issue.) I don't understand why the return value is always off by 4 using this:

 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Array being null, and the Array being present with no items in it are actually two distinct cases for the computer. Maybe not so much for humans (either way there are no items in the list) but that is a very important thing to remember.
Looking for the length of a null array will throw an exception :-)

Regarding the "off by 4" issue, the code you have posted looks ok to me.

Can you give an example of your using it, and show the output it presents?
Why do you say it is off by 4? 4 too much or 4 too little?

 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the data that you are provinding the method? Your code seems sound.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When working on the assignments for this class I have found it helpful for me to use a class I call testArea. This is the content of that class at the moment.


and this is the output of the last run

run:
Array is Null
Array is empty
3
4
5
-4
7
8
9
10
11
12
13
5
12
13
5
Searching for 12
0 1 2 3 4 5 6 7 8 9
Location = 9
Minimum value in array = -4
BUILD SUCCESSFUL (total time: 0 seconds)
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is working perfectly. Your copyRange() is what is throwing you off. Change your start variable to 0 and you will get the correct location returned.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help with the counter issue. Not sure why the start variable had any effect, it wasn't part of the call, guessing the call to the copyRange method had some bearing. When I commented out the copyRange call the search method worked fine.

Any suggestions on the exception issue?
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 2 of the copy is why. When you do an assignment '=' it is not creating a new array you are telling to point to where list is at in memory. Java works by reference not value. If you would like more on this topic i suggest you ask a new question in the forums about it.
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the exception issue. What is your question? The code is good as far as acomplishing what you described you wanted to do. Or maybe i dont understand the problem you are trying to solve.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the exception issue, I need the a different response for the array being null and list.length being 0.

As the code is currently written once it determines the array is null it exits that block of code (as I am aware that it should). But I still need to find a way to get the other exception thrown.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Tracy wrote:Line 2 of the copy is why. When you do an assignment '=' it is not creating a new array you are telling to point to where list is at in memory. Java works by reference not value. If you would like more on this topic i suggest you ask a new question in the forums about it.


Thank you for the explanation, that answered my question on that issue.

I truly appreciate the assistance.
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, if i understand what you are asking. What you need to do os replace "Array is empty" with e.getMessage(). Maybe? Your question is still vague if that did not answer your question.
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, sorry I reread the initial post and we need more info. Inside your catch for badarray. Can you put e.printstacktrace() and post the stacktrace that print?
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this is what you were looking for

Array is Null
testarea.BadArrayException: Array is Null
at testarea.TestArea.testArray(TestArea.java:47)
at testarea.TestArea.main(TestArea.java:33)
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is working. I see no problem with it. The exception functioned perfectly.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, part of the problem I am having is that the code that works with the classes we were assigned to create was provided by the instructor. It's more difficult for me to see how something is going to work when I didn't write all of it. I guess I should get used to that and learn to read other people's code better.
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is his code expecting to receive the exceptions? If so remove the try catch for the badarray.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were right, the code was working as it should when run from the file provided by the instructor.

I want to thank all of you for your help
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:Glad I found a place that focuses on being friendly to rookies. Have been slammed and shamed for asking simple questions on some other sites.

I don't see that you were either slammed or shamed in what was probably your last forum thread before this: Array Test questions.

Jos is going to feel so hurt...
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
I don't see that you were either slammed or shamed in what was probably your last forum thread before this: Array Test questions.

Jos is going to feel so hurt...


Sorry if there was a misunderstanding, everyone here was very helpful and pleasant, there are other places that are not as friendly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!