• 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

Quick Help Please?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program is suppose to print 1 if the first number set has 3 same numbers in succession and seond set has 2 numbers in succession.
I am having a index out of bounds error


 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the exact error message and the stack trace (everything after the error message). Do you know which line is getting the error?
 
Marshal
Posts: 79174
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please have a look at the formatting of your code. You have too much on each line. You aren't using {} after if, etc. Your variable names (digis, igits, etc.) are confusing. That makes your code hard to read, and it is you who will have the most difficulty reading it. It makes it difficult to see the + 1 in line 14 which will cause you problems. It also makes it difficult for you to see that you aren't testing whether you have 3 and 2 in line 26.
Reduce lines 26‑28 to something like,See the old Sun Style Guide.
 
Ari Ellie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Error message is I have a JUnit tester and the print is always 1. Even if it is suppose to be 0 for false the result is still 1.

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you don't have an error message, you have test failure. The test fails because your logic is wrong. To make the test pass, you need to fix your logic.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking over your code, any test that passes will pass by pure coincidence rather than because of correct logic. Given the requirements, none of the code you've written actually satisfies those requirements.

For example, the conditions on lines 14 and line 20 will never be satisfied. Never. That means value++ will never get executed. That means that lines 15 and 21 will always put 0 into your map. Which means the conditions on line 16 and 22 will never be satisfied.

So basically, your program always ends up executing lines 26 and 27, no matter what the input is. In other words, your program will always return 1, regardless.

If you replaced lines 13-28 with this single statement:

you'd get the same test results you are getting from all that code.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're using JUnit, what tests do you have now? Did you write those tests or were they provided to you by your instructor? Post the test code so we can have a look.
 
Ari Ellie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This are the test cases my instructor gave:

 
Campbell Ritchie
Marshal
Posts: 79174
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ari Ellie wrote:The Error message is I have a JUnit tester and the print is always 1. . . .

That is different from what you said at first. Junilu is right; you have lots more logic errors than I first noticed.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me guess: your code passes test1 and test4 but fails all the other ones. Like I said, any pass you get with that code is purely coincidental.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing about your logic is that you're not really tracking consecutive repeated digits. You're only counting how many occurrences of each digit there are in given number. That is, given a number like 101431, the scheme you're using would see this as having three 1 digits. That's not what the requirements are asking for though. Rather, you're supposed to see if there are three of the same digit in succession. For example, 422290133 has three consecutive 2s and two consecutive 3s. On the other hand, 2323234949 has repeated digits but none of them appear in the number consecutively.
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic