1

Hey guys im working on an ulam's spiral problem and I want to search through the 2D array I have created and find the number entered by the user (numb). I don't get why the if statement (marked with a comment) is always returning false

Edit: Here is the partial code, I have run my genUlam function through the debugger and it DOES in fact return the number, unfortunately this is an assignment so i cannot post the entire code.

public static void main(String[] args)
   {
    //create scanner object
    Scanner input = new Scanner(System.in);

    //get input for dimensions
    //System.out.print("Enter dimension: ");
    int dimension = 3;

    //check if dimensions are odd
    if (isOdd(dimension) == false)
    {
       //if input for dimensions is even, get the next odd dimension 
      dimension += 1;
    }

    //get input for number in center
   // System.out.println("Enter number in spiral: ");
    int numb = 2;


    //check that number in center is in range of 1 to dim^2
    if (numb > Math.pow(dimension,2) && numb < 1)
    {
        System.out.println("Number not in range");
        System.exit(0);
    }

    //create 2D array using my function
    String[][] ulam = {{"1", "2", "3"},{"4","5","6"},{"7","8","9"}};

  //check to see if the number user entered does exist, then print array 
  for(int r = 0; r < dimension - 1; r++)
  {
     for(int c = 0; c < dimension - 1; c++)
     {
     //String current = ulam[r][c];
     /*the line below should be returning true when match is found, but it is returning false no matter what*/
     if(ulam[r][c].equals(Integer.toString(numb)))
       {
       printArray(ulam, r, dimension);
       }
       else

       {

       System.out.println("match not found");
       }
     }
  } 

}

   public static void printArray(String[][] array, int row, int dimension)
  {
     for (int i = 0; i < dimension - 1; i++)
     {
        System.out.println(array[row][i] + " ");
     }
  }


  }
Parth
  • 255
  • 4
  • 13
  • 4
    It seems you are comparing Strings with `==`. Don't do it: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Pshemo Jul 10 '15 at 21:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83003/discussion-between-ziggy-and-pshemo). – Parth Jul 10 '15 at 22:20

1 Answers1

3

You should not compare Strings with == as you would do with ints.

Change

if(ulam[r][c] == Integer.toString(numb))

to

if(ulam[r][c].equals(Integer.toString(numb)))

Even better, move the Integer.toString(numb) out of the for loops, so it gets executed only once:

String[][] ulam = genUlam(dimension,1);
String number = Integer.toString(numb);

for(int r = 0; r < dimension - 1; r++)
{
    for(int c = 0; c < dimension - 1; c++)
    {
        if(ulam[r][c].equals(number))
        {
            printArray(ulam, r, dimension);
        }
    }
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • that didnt work, it is false – Parth Jul 10 '15 at 21:38
  • 2
    @ziggy It means that content of `ulam[r][c]` is not equal to `number`. Are you sure it should work? Can you update your question with proper example which would let us copy-paste your code and reproduce its problem? – Pshemo Jul 10 '15 at 21:40
  • Sure i'll update the question with the full code; and a deeper explanation. – Parth Jul 10 '15 at 21:41
  • @ziggy Generally we try to avoid posting full code since it tends to contain a lot of code unrelated to your problem. Instead try creating [SSCCE](http://sscce.org/). – Pshemo Jul 10 '15 at 21:43