Results 1 to 6 of 6
  1. #1
    paul1024's Avatar
    paul1024 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default comparing phone numbers

    I discovered that doing a string comparison with 2 phone numbers that appear to be the same do not evaluate as true. I feel like I have tried all the comparison methods and nothing works. If I remove the phone number comparison my method works.

    Java Code:
    if(fName.compareToIgnoreCase(firstName)==0 && lName.compareToIgnoreCase(lastName)==0
    				&& phoneNum.compareToIgnoreCase(phone)==0))
    phone numbers appear like this: 111-111-1111

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: comparing phone numbers

    You're shouldn't be using compareToIgnoreCase when you're testing for equality. Instead use equalsIgnoreCase.

    And if the code still doesn't work, then you should create and post an sscce.

  3. #3
    paul1024's Avatar
    paul1024 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default Re: comparing phone numbers

    When I do the sscce my code works:

    Java Code:
    //search will find the contact in the file that matches the parameters and return the line number
    	//that it is located on
    	public int search(String firstName, String lastName, String phone) throws FileNotFoundException
    	{
    		Scanner input = new Scanner( new File("addresses.txt"));
    		input.useDelimiter(","); //changes default delimiter from whitespace to commas
    		int lineNum = 0; //used to set lineNum to help with deleteContact()
    		
    		//dummy info for java post
    		String firstNames[] = {"john", "sam", "tom"};
    		String lastNames[] = {"doe", "jones", "anderson"};
    		String phoneNums[] = {"111-111-1111", "222-222-2222", "333-333-3333"};
    		
    		for(int i = 0; i < 3; i++)
    		{
    			if(firstNames[i].equalsIgnoreCase(firstName) && lastNames[i].equalsIgnoreCase(lastName)
    					&& phoneNums[i].equalsIgnoreCase(phone))
    				{
    					break; //break out of the for loop. we do not want lineNum incremented
    				}
    			lineNum++;
    		}
    		return lineNum;





    This is the code with the I/O:
    Java Code:
    //search will find the contact in the file that matches the parameters and return the line number
    	//that it is located on
    	public int search(String firstName, String lastName, String phone) throws FileNotFoundException
    	{
    		Scanner input = new Scanner( new File("addresses.txt"));
    		input.useDelimiter(","); //changes default delimiter from whitespace to commas
    		int lineNum = 0; //used to set lineNum to help with deleteContact()
    
    while(input.hasNext())
    		{
    			//scan the name
    			setFname(input.next());
    			setLname(input.next()); 
    		
    		//even though this info may be irrelevant im scanning it to get to the next address and phone
    			//number
    		    setAddress(input.next());
    			setCity(input.next());
    			setState(input.next());
    			setZip(input.next());
    			setPhoneNum(input.next());
    			
    			//debug code
                System.out.println("phone param is " + phone);
                System.out.println("instance var is " + phoneNum);
    			
    			//compare the parameter variables with the IO info. if everything matches we found the contact
    			
    			if(fName.equalsIgnoreCase(firstName) && lName.equalsIgnoreCase(lastName)
    				&& phoneNum.equalsIgnoreCase(phone))
    			{
    				break; //break out of the while loop. we do not want count incremented
    			}
    			
    			lineNum++;
    		}//end while
    		
    		return lineNum;
    	}
    and the code above works if I remove the phone number comparison in the if statement
    Last edited by paul1024; 09-18-2011 at 09:58 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: comparing phone numbers

    A guess... perhaps it's a problem with the phone number String and not with the code above. Could one of the Strings have unknown white space attached to it, such as trailing spaces? Have you tried trimming the Strings before checking them? Or printing them out in quotes?

  5. #5
    paul1024's Avatar
    paul1024 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default Re: comparing phone numbers

    The code works when I trim the phone number from the file. Some how whitespace was in my string and causing my comparison to go false?
    This is what the file looks like:

    ,sam,jones,5712 oak st,tucson,az,85716,111-111-1111

    Thank you for the help.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. comparing Graphs and Comparing Matrix
    By jetnor in forum New To Java
    Replies: 0
    Last Post: 03-27-2011, 01:40 AM
  2. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  3. Comparing 3 or more numbers
    By W00tbeer1 in forum New To Java
    Replies: 8
    Last Post: 10-11-2010, 07:03 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  5. Problem comparing three numbers
    By gammaman in forum New To Java
    Replies: 1
    Last Post: 02-09-2008, 05:43 PM

Posting Permissions

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