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 10 of 10

Thread: Linear array search(without Arraylist)

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

    Default Linear array search(without Arraylist)

    I have a method that is SUPPOSE to prompt the user to enter the name of the item to search for. If it exist then say it was found and return the index number. if it doesn't then tell them it doesn't.

    I am not allowed to use Arraylist either.

    I honestly don't know where to start...

    If I run this it gives me my else print statement in a loop until exit condition is satisfied.


     public static void searchList( String[] list,  int currentSize){
     
    		String item;
     
    		System.out.println("Enter the item you want to search for: ");
    		item = in.nextLine();
     
    	for (int n=0; n < list.length; n++)
    		if(list[n] != null && list[n].equals(item))
    			System.out.println ("The item was found, and the index of the array element is" + n);
    		else
    			System.out.println ("The item you enter does not exist in the list");
    	}
    Last edited by Usoda; November 17th, 2011 at 02:10 AM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Linear array search(without Arraylist)

    Looking at your code, there can be three possibilities.
    1. Either the list you are passing to function is has no values. Or
    2. There is no such item.
    3. Case doesn't match. (Either use equalsIgnoreCase() )

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Linear array search(without Arraylist)

    All i need to do is somehow stop the loop from printing my else statement if IF is true. It will run through both if and else and print them and will print only my IF when it finds the index?

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Linear array search(without Arraylist)


  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Linear array search(without Arraylist)

    Quote Originally Posted by Tjstretch View Post
    I have never seen any condition in which if (IF part) executes successfully, (Else part) will execute too. break is for breaking out of the loop. OP doesn't want to break out the loop but to avoid printing the else part if (IF part) is true. And this can never happen that after executing IF part, else part executes too.

    @USODA: Can you provide the values for list and all the variables that need values?

    Hint: You need to have a flag that will keep track if an item is found or not and after completion of your loop, check the flag, if true, it means, item found and if false, item couldn't find.
    Last edited by Mr.777; November 17th, 2011 at 11:00 PM.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Linear array search(without Arraylist)

    This is my working code. It works for what I have tested it for, but I am not sure if I have ran all the conditions for it too pass. Other than that I believe this is solved. Unless you guys see something I will run into later with it that will give me an error?

     public static void searchList( String[] list,  int currentSize){
     
    		String item;
    		int n=0;
    		boolean flag = false;
     
    		System.out.println("Enter the item you want to search for: ");
    		item = in.nextLine();
     
    		for (n=0; n < list.length; n++)
    			if (list[n] != null && list[n].equalsIgnoreCase(item))
    		break;
    		System.out.println ("The item was found, and the index of the array element is " + n);
    		flag = true;
     
    			if(flag != true)
    					System.out.println ("The item you enter does not exist in the list");

  7. #7
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Linear array search(without Arraylist)

    Wow never mind apparently Junky posted right after me.

    You will need to pick some arbitrary value that you return if nothing is found, such as a negative number. I would also recommend not saying anything in the actual searching method, do that all in the main method (unless your teacher specified otherwise of course), because it would be much easier to read.

    /*
     * Assume list is an initialized array of strings.
     */
    int index = searchList(list, list.length);
    if(index < 0)
      System.out.println("Nothing was found");
    else
    {
      System.out.printf("Found %s at %d.", list[index], index);
      System.out.println();
    }
    Last edited by Tjstretch; November 17th, 2011 at 11:31 PM.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Linear array search(without Arraylist)

    Quote Originally Posted by Usoda View Post
    If it exist then say it was found and return the index number.
    Since your method is void it is not doing this. What you need to do inside the if statement is to return the index and get rid of the else. At the end of the method return a negative number (an invalid list index). Then in the calling method capture the returned value and use it in an if statement there to print out the result.
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Linear array search(without Arraylist)

    Sorry guys it isn't suppose to RETURN anything.. However it is suppose to have print statements for the user inside the method as requested by the instructor. This quote below, as posted before - I believe is the working method unless you guys see any errors I will run into down the road?

    If not once again thank you guys for your time and brain power. Much much appreciated!

    Quote Originally Posted by Usoda View Post
    This is my working code. It works for what I have tested it for, but I am not sure if I have ran all the conditions for it too pass. Other than that I believe this is solved. Unless you guys see something I will run into later with it that will give me an error?

     public static void searchList( String[] list,  int currentSize){
     
    		String item;
    		int n=0;
    		boolean flag = false;
     
    		System.out.println("Enter the item you want to search for: ");
    		item = in.nextLine();
     
    		for (n=0; n < list.length; n++)
    			if (list[n] != null && list[n].equalsIgnoreCase(item))
    		break;
    		System.out.println ("The item was found, and the index of the array element is " + n);
    		flag = true;
     
    			if(flag != true)
    		System.out.println ("The item you enter does not exist in the list");

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Linear array search(without Arraylist)

    What if you couldn't find any element?
    What will happen? You code will never go to
    if(flag !- true)
    part. That's why it is recommended to use opening and closing brackets for the beginners. Specify { and } for your if and for. Let's say it doesn't find any thing. It will skip out the loop and print
    "The item was found at the index of the array element is: list.length+1"
    and will mark the flag true.

  11. The Following User Says Thank You to Mr.777 For This Useful Post:

    Usoda (November 18th, 2011)

Similar Threads

  1. Array Search Method
    By Kyuubi426 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 08:31 PM
  2. Search for min and max values as columns position in array
    By susieferrari in forum Java Theory & Questions
    Replies: 3
    Last Post: April 28th, 2011, 07:39 AM
  3. Search for number in array and return index
    By Kevinius in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2011, 12:00 AM
  4. [SOLVED] Help with array of Strings...Linear Search?
    By Stockholm Syndrome in forum Collections and Generics
    Replies: 4
    Last Post: March 22nd, 2011, 03:31 PM
  5. [SOLVED] Couldn't search for a string in an array.. Help please..
    By astrojunk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 09:47 PM

Tags for this Thread