• Post Reply Bookmark Topic Watch Topic
  • New Topic

If condition matches multiple times  RSS feed

 
Johnny Venter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this is my first post here.

I'm working on a problem that requires me to generator all possible word combinations based on a 7-digit number as input. Many of the generated "words" will be nonsense, but some with be "NEWCARS", "TAKEOUT", etc... This problem mimics the phone number a company would use to help clients remember that number.

I completed the exercise, but I would like to explore more elegant solutions. Specifically, I've used an IF-THEN-ELSE condition inside of a FOR loop. Here is my working code:




I'ved tried replacing the it with this more "elegant" code:



I receive StringIndexOutOfBoundsException exceptions. I it's because multiple conditions are matched. For example, the indexSix is reset to 0 when row is a multiple of 9. Because row is also a multiple of 3, this condition also executes and then increments "indexSix"

I'm not sure how to fix this problem.

Thank you for taking the time to read this post and hopefully providing feedback.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried replacing your if's with else-if's?
 
Johnny Venter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I fixed it. I reversed the order of my IF statements:



@Carey - thanks for the suggestion, the IF-ELSE's did not work.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johny, why do you assign in your if statements indexOne, indexTwo... indexSeven to zero?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try using two loops and going through a dictionary file. You can find one in the Java Tutorials. Unfortunately you are unlikely to find NEWCARS TAKEOUT etc in that file.
... and welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!