• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested for Loop Returning ArrayOutOfBoundsException  RSS feed

 
Louis Standish
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am doing a problem from Codewars that returns certain responses depending on the string in the method. I created two separate lists for the words to be searched and created a for loop to go through each one. However, I receive the error message of java.lang.ArrayIndexOutOfBoundsException. I am not sure how I screwed it up in the index.

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch! Hint: Just check whether you misplaced i and j
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We used to call this a 2:00AM error when I was in college:

Louis Standish wrote:Hello,

I am doing a problem from Codewars that returns certain responses depending on the string in the method. I created two separate lists for the words to be searched and created a for loop to go through each one. However, I receive the error message of java.lang.ArrayIndexOutOfBoundsException. I am not sure how I screwed it up in the index.

 
Louis Standish
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thank you. The syntax errors can trip me up.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Standish wrote:The syntax errors can trip me up.
It is code logic error, not syntax error. Syntax error: "In computer science, a syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language."
 
Louis Standish
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have not been able to work on the problem for a while, but have encountered another roadblock when I started again. The error "reached end of file while parsing" came up when running tests on the Code Wars website. I am not sure where the issue is as it works in Eclipse.

 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you please tell what precisely do you want this method to do for you? 
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • .indexOf(String str) method of String just returns the index within this string (Means String s) of the first occurrence of the specified substring (Means String literals in if statement ex. "water"), if there is no such occurrence. then returns -1.
  • Suppose if String s contains "water slime" then for loop will execute 11 times.
  • In  first execution of for loop, If statement having s.indexOf("water") will return 0 because "water" is present at index 0 of String s, so If condition becomes true and assigns answer = "Water" then rest of else if and else will not get executed.
  • Same thing will happen for next remained 10 loops as length of String s is 11.
  • Finally answer will have value "Water" and returns to the invoker of this method.
  • If String s doesn't contain any String literal of any if statement then else block will get executed for 11 times and answer will have "Air" and retuns to the invoker of this method.

  • Is this what you want this method to do?
     
    Campbell Ritchie
    Marshal
    Posts: 55753
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why are you using −1? There is a simpler (and less error‑prone) way to do that, which you will find if you go through the String class.
    You usually get a “reached end of file” compiler error if you have more { than }; that error does not occur in the bit of code you posted. Are you sure the Eclipse code and the CodeWars code is exactly the same?
     
    Louis Standish
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello,

    The method is supposed to take the phrase entered under Main and search for those words. With the nested loop, it would try to find the word "wet," producing "water" if that came through and so on. The original problem said to test for variations on "water" as well as "wet."

    I initially tried to do a series of arrays and iterate through them, but I could not get that to work.

    I am looking at the String class and will try to get the answer there.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!