tags:

views:

87

answers:

2

Hello,

I am a newbie Computer Science high school student and I have trouble with a small snippet of code. Basically, my code should perform a basic CLI search in an array of integers. However, what happens is I get what appears to be an infinite loop (BlueJ, the compiler I'm using, gets stuck and I have to reset the machine). I have set break points but I still don't quite get the problem...(I don't even understand most of the things that it tells me)

Here's the offending code (assume that "ArrayUtil" works, because it does):

import java.util.Scanner;
public class intSearch
{
   public static void main(String[] args)
   {
       search();
   }

   public static void search()
   {
       int[] randomArray = ArrayUtil.randomIntArray(20, 100);
       Scanner searchInput = new Scanner(System.in);
       int searchInt = searchInput.nextInt();
       if (findNumber(randomArray, searchInt) == -1)
       {
           System.out.println("Error");
       }else System.out.println("Searched Number: " + findNumber(randomArray, searchInt));
   }

   private static int findNumber(int[] searchedArray, int searchTerm)
   {
      for (int i = 0; searchedArray[i] == searchTerm && i < searchedArray.length; i++)
      {
          return i;
      }
      return -1;
   }
}

This has been bugging me for some time now...please help me identify the problem!

+3  A: 

I don't know about the infinite loop but the following code is not going to work as you intended. The i++ can never be reached so i will always have the value 0.

for (int i = 0; searchedArray[i] == searchTerm && i < searchedArray.length; i++)
{
    return i;
}
return -1;

You probably mean this:

for (int i = 0; i < searchedArray.length; i++)
{
    if (searchedArray[i] == searchTerm)
    {
        return i;
    }
}
return -1;
Mark Byers
My style is trying to show the student the bug and then let them try to figure it out and fix by themselves rather than fixing it for them :). Anyway, it is up to you:)
vodkhang
Yes, how dare you answer the question, he has a style thing going on over here! Just kidding @vodkhang, I appreciate your style.
RandyMorris
+1 for blatant disregard of style.=p
Precision
I see your point...I changed that part according to your suggestion but it still has the same Java-machine-is-still-executing problem.Thanks for the input.
Joshua
+1  A: 

I don't know what is the class ArrayUtil (I can not import is using my Netbeans). When I try to change that line with the line int[] randomArray = {1 , 2, 3, 5, 7, 10, 1 , 5}; It works perfectly.

And you should change the loop condition. I will not tell you why but try with my array and you will see the bug soon. After you see it, you can fix it:)

vodkhang
I have tried it before with a normal Array like that (I even tried yours just in case) because I did suspect ArrayUtil was at fault. However, same problem. Beginning to think my BlueJ is somehow screwed up...
Joshua
ok, in that case, tried with netbeans http://netbeans.org/ . Sometimes, blueJ had problems, it is never used for commercial projects
vodkhang
Okay, will try with different compilers. Thanks for the suggestion, it did seem odd for me because my code looked perfectly fine (except for the loop which is now fixed with an if statement).
Joshua