Closed Thread
Results 1 to 5 of 5

Thread: String comparing help

  1. #1
    G1TN3RD is offline Newbie
    Join Date
    Jan 2010
    Location
    College Station, TX.
    Posts
    8
    Rep Power
    0

    Exclamation String comparing help

    I am writing a program for school and need some help, i am supposed to create a method in which will find an int and tell me how many times it took to find it...
    I have three methods in this one big method... One for a String, one for a Double, and one for an Int... The trouble is there is a part where i start an if statement and compare an int to a String using the greater than symbol (>) and it is giving me the incompatible types error, which makes sense, because i am comparing an int to a string...

    here is the code
    Code:
    public static boolean BinarySearch(String list[], int item)  
    	{
    		boolean found=false;
    		int lo = 0;
    		int hi = list.length -1;
    		int counter = 0;
    		
    		while(lo<=hi && !found)
    		{
    			counter++;
    			
    			int mid = (lo + hi) / 2;
    			if(list[mid].equals(item))
    			{
    				found = true;
    			}
    			else
    			{
    				if(item > list[mid].length)
    					lo = mid +1;
    				else
    					hi = mid -1;
    			}
    		}
    	    System.out.print("Number of searches:"+counter+" ");
    	    
    	    return found;
    	}
    The second if statement in the while loop is giving me trouble. so as you can see i tried doing item>list[mid].length so that it compares the length, but its still giving me an error... I cant figure out how to compare the two so that the if statement will work... Im sure there is some extremely simple way of doing this and im an idiot and missing it but please tell me it....


    -git

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: String comparing help

    change it to list[mid].length()

    in String class length is a function

  4. #3
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: String comparing help

    Method names are always starting with lower case:
    Code:
    binarySearch(String list[], int item)
    Code:
    if(list[mid].equals(item))
    What's that? You are comparing String to an Integer. That will always return "false".

  5. #4
    curse is offline Newbie
    Join Date
    Mar 2010
    Location
    Ottawa, Canada
    Posts
    21
    Rep Power
    0

    Re: String comparing help

    To convert a String to an integer:

    Code:
    String myString = "42";
    int myInt = Integer.parseInt(myString);
    Then you can go ahead and compare them, since they are both the same data type.
    Last edited by curse; 03-05-2010 at 10:35 PM.

  6. #5
    G1TN3RD is offline Newbie
    Join Date
    Jan 2010
    Location
    College Station, TX.
    Posts
    8
    Rep Power
    0

    Re: String comparing help

    Thanks everyone! i got it running, i am using it as a method to call into a sorting int program later...
    /thread

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help comparing variables
    By mickey1989 in forum PHP Development
    Replies: 0
    Last Post: 03-10-2011, 09:26 AM
  2. Replies: 5
    Last Post: 11-02-2009, 03:05 PM
  3. Comparing passwords with php
    By Vswe in forum PHP Development
    Replies: 8
    Last Post: 08-23-2009, 11:47 PM
  4. Comparing 4 numbers...C++
    By mindsurfer in forum C and C++
    Replies: 6
    Last Post: 11-07-2007, 05:44 PM
  5. Comparing Values
    By John in forum PHP Development
    Replies: 4
    Last Post: 07-25-2006, 04:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts