• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Element exists Exception  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this is my task:

Create a method: static int elementExists(int[] elements, int number)
The method has to check if in an array: elements, exists element of the value: number.
If the value does not exist, method will return an exception of the type Exception with a comment: "There is no such element".
Complete the method signature with information that it can return the Exception.

I know how to handle an ArrayIndexOutOfBoundsException when the int number points at index. I also have no problem with building method that will check if an array contains an element. But in this case I'm clueless.

So I'm asking a kind person for help.
Bart


 
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

That seems to be a fairly simple task, however, instructions are well confusing I'd say, at least how they are written/expressed currently.

Reasons:
Such method name "elementExists" I'd expect to have a return type boolean in the first place, that's not the case, so it is an int.

instructions wrote:Complete the method signature with information that it can return the Exception.


Neither return type, nor what exception method possibly throws are part of method signature. So this quote doesn't make sense at all. Might be the problem in translation. And I'm very doubted you'd ever want to return exception from such method in general.

So what I think this task is asking you are one of these two:

version 1 wrote:Implement given method, so it would return the searchable element from the given array in case it is found, otherwise throw an exception.


version 2 wrote:Implement given method, so it would return the searchable element's index (first) from the given array in case it is found, otherwise throw an exception.


My wording also might be a bit off in terms of English, but at least a bit clearer in terms of Java terminology.

Do you have an ability to verify requirements with somebody?
 
Bart Wtoras
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.


Complete the method signature with information that it can return the Exception.


I assume they want me to use throws Exception for the elementExists method.

version 1 wrote:
Implement given method, so it would return the searchable element from the given array in case it is found, otherwise throw an exception.


Version 1 looks like the function I'm looking for.


Here's my method looking for a number in an elements array . By the first occurrence of the number it breaks the for loop and returns: 1, else it returns: 0. I have no idea how to implement throws Exception when method does not find any matching number.



 
Liutauras Vilda
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain why do you assign 1 to a result (line 13) and later return that? That is not what you confirmed as wanted functionality.
 
Bart Wtoras
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is important in this task is throwing an Exception in case when nothing is found in an array.

I have found a solution, throw new Exception solved my problem. Here's my code slightly modified:



Is there a more solid way to build this method?
 
Liutauras Vilda
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bart Wtoras wrote:What is important in this task is throwing an Exception in case when nothing is found in an array.


Well, important is to accomplish the task as required, not just implement the parts which are important. So I suggest to consult with someone who gave this task to you in order to understand requirements well.

Now, whether the program is correct..

Bart Wtoras wrote:I have found a solution, throw new Exception solved my problem.


In order to verify that your solution is indeed correct, or at least to have a good amount of evidence of that, or more precicely - to not find the case on which your program fails, you need to test it against various inputs. You don't need to use modern technologies for testing, you can simply try to execute your program against several of different kind inputs, i.e.: negative number, positive number, zero (which is a number too).

What do you think would happen if I were try to invoke:
?

That supposed to give you a good idea whether your program is correct or still needs some coding. In general you are on the good track
 
Marshal
Posts: 60130
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a version of an indexOf() method? If so, you might consider returning the index of the element sought (starting 0), or −1 for “not found”.
 
Bart Wtoras
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for pointing out that error, and wasting your time helping me.
Have a good day/night.


 
Campbell Ritchie
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have been given a poor assignment if you are supposed to return 1 for found and 0 for not found; that is like C programming, not the Java┬« way to do things. If you are retutning 1/0, however, I think you shouldn't throw an exception for “element not found”.
 
Liutauras Vilda
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP's lately posted code contains bug I just mentioned. Well, OP needs to tell exact requirements, otherwise there is a risk to mislead.
 
Bart Wtoras
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a 100% accurate description of the requirements:

version 1 wrote:
Implement given method, so it would return the searchable element from the given array in case it is found, otherwise throw an exception. 

 
Liutauras Vilda
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bart Wtoras wrote:This is a 100% accurate description of the requirements:

version 1 wrote:
Implement given method, so it would return the searchable element from the given array in case it is found, otherwise throw an exception. 


So your method doesn't do that.
 
Bart Wtoras
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right, it does not return number I'm looking for, now it returns 1. I should have read more carefully.
This is accurate description: It has to check if an array contains a number, and if not throw an exception.
 
Campbell Ritchie
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Last night, I wrote:You have been given a poor assignment . . .

If you have been told to throw an exception if you don't find the element, that makes the assignment even worse
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!