Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Couldn't search for a string in an array.. Help please..

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Couldn't search for a string in an array.. Help please..

    boolean found=false;
    		System.out.print("\n\tEnter name to search: ");
    		String namesearch=in.nextLine();
    		int i=0;
    		while(i<ctr && !found)
    		{
    		if(namearray[i]==namesearch)
    		{
    		found=true;
    		}
    		i++;
    		}
    		if(found)
    		{
    		System.out.print(namesearch+" is at index "+i);
    		}
    		else
    		{
    		System.out.print("Not found");
    		}
     
    		break;
    Last edited by astrojunk; February 1st, 2011 at 07:47 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Couldn't search for a string in an array.. Help please..

    Hello and welcome to the forums.

    This is a common newbie mistake.

    if(namearray[i]==namesearch)

    Needs to be:

    if(namearray[i].equals(namesearch))

    In future, please post some text along side your code. We need as much information as possible.

    It's also worth reading Common Java Mistakes. The link is in my signature.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Couldn't search for a string in an array.. Help please..

    please marked it solved if solved.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Couldn't search for a string in an array.. Help please..

    Got it.. Thanks so much..

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. Text File into String Array
    By mathanv in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 05:30 AM
  3. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  4. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM
  5. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 10:24 PM