This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

error with code...apparently!

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There
I have written the below code and am being told that it will not always work by my lecturer but i dont understand how? every format of array i have tried comes back with "YES".....  the condition of the method was that num1 index is always lower than num2 index and both num1 and num2 always must exist in the array i input.

Anyone have any advice, it would be appreciated!

 
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Safi Habib wrote:...and both num1 and num2 always must exist in the array i input.

Initialize you indices to -1 to indicate not found then check for that before returning true.

Because zero is a valid index.
 
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you are having problems is that you are testing whether theindex of the first number is less than that for the second number. You would fail to find the numbers in the other order. Your lecturer is correct. You need to test that both indices are non‑negative.
Alternatively, you can use booleans.Line 6 looks complicated, but it gets you out of using break; You can write line 6 in different ways. Work outseveral why I didn't say else if ... in line 12. You may find the variables difficult to read, in which case you should change them. Note it is not necessary to have two separate loops.
 
Saloon Keeper
Posts: 3407
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful though. If i = j, I would interprete that as whether i appears twice (at least) in the array. The above code would fail in the case that i appears only once.
 
Bartender
Posts: 708
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code will throw a NullPointerEexception if you pass null as the first argument to the method ( trying to evaluate myArray.length).
 
Carey Brown
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting problem when you consider num1 and num2 of 31 and 12, or 12 and 31.
 
Campbell Ritchie
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . I would interprete that as whether i appears twice (at least) in the array. . . .

Good point. Also Fred's about whether null is permissible, but I think it would be appropriate to throw the exception.
 
Piet Souris
Saloon Keeper
Posts: 3407
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP
I like Campbells code, but as you see it has two slight problems. How would you tackle Freds remark?  And if you find an element that is equal to i, does it matter whether j has been found or not found already?
 
Safi Habib
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all!

After looking at it soem more with other peers it seems that the solution is that if the 2nd number appears twice (once before num1 and then again after) it will return NO.  So to fix i have changed the 2nd loop so it starts from the end of the array and goes backwards so that num2 index is always higher than num1 index
 
Piet Souris
Saloon Keeper
Posts: 3407
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Safi,

does your new solution also works if the array = {1, 2} and n1 = 2, n2 = 1? Or when the array = {1} and n1 = n2 = 1?

 
Campbell Ritchie
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not convinced that running the loop backwards really helps you with your searching problem.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!