• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

copyEnds Challenge

 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on below challenge
https://codingbat.com/prob/p130124

We'll say that a positive int n is "endy" if it is in the range 0..10 or 90..100 (inclusive). Given an array of positive ints, return a new array of length "count" containing the first endy numbers from the original array. Decompose out a separate isEndy(int n) method to test if a number is endy. The original array will contain at least "count" endy numbers.


copyEndy([9, 11, 90, 22, 6], 2) → [9, 90]
copyEndy([9, 11, 90, 22, 6], 3) → [9, 90, 6]
copyEndy([12, 1, 1, 13, 0, 20], 2) → [1, 1]





I am failing some tests with ArrayIndexOutOfBoundsException. Can you please point me to what mistakes i am making here?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the report constantly points to line 9, with that IOOBException. What could be the problem here, you think?
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When i debug in eclipse getting below error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at CodingBat/com.codingbat.CopyEnds.copyEndy(CopyEnds.java:20)
at CodingBat/com.codingbat.CopyEnds.main(CopyEnds.java:8)



Looks like something getting assigned beyond size of array at below line i believe
         arr[arrIndex] = n;
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Looks like something getting assigned beyond size of array at below line i believe
         arr[arrIndex] = n;



Yes, that's correct. That's the typical off-by-one error which happens because using arrays forces you to start counting from zero.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a Stream-version that you may not understand yet, but by looking at it you may discover what you are forgetting in your method:
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am starting at 0 like String substring method also going until nums.length (not including)
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:


I limit the number of endies found to a maximum of count. Where are you setting a maximum?
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Above is working with while loop





I could not make my original for loop approach to work yet though. I am checking on that limit as well in for loop
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
once added limit it worked with for loop also





i am not that used using more than one indexes. can we do this with just one index instead of 2 indexes(one to iterate given input index to check each element, other to put elements into result array to display finally)?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:...The original array will contain at least "count" endy numbers.

No. Still need two indexes, but this tells you that you only need to test one of them in your for() loop.
 
reply
    Bookmark Topic Watch Topic
  • New Topic