• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error and dead code warning  RSS feed

 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, I am trying write code for a question in an assignment I have. There are 2 methods and a main one. The first is a void that creates a 3x4 2d array and the second is to search for a value in the array, if found it will return the number of the row that contains that value. If not, it will return -1, but I am getting 1 error and 1 dead code warning. can you please help? I will put comments at the lines that contain the problems

Thank you
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These errors are actually closely linked.

Lets deal with the first: "This method must return a result of type int"

Take a look at your method: is there a path through your code that lets you get to the end without returning anything?
A: Yes. if rowsize is 0, you skip the loops - and reach the end of the function without returning. Which is what java is complaining about.
Solution - put a 'return' statement at the end of the method and it will be happy.


For the second one: Dead code.
Java has found no values for rowsize/colsize that lets you ever execute the line "counter2++"
That means that it will always return before the end of your loop.
In what circumstances were you expecting to keep looking?


Both of these issues go away by moving one line of code.

 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your answer, the code worked so I will set the question as solved.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!