Results 1 to 2 of 2
  1. #1
    tonmachine is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default help using equalIgnoreCase in an array

    I'm supposed to take user input and create a shopping list with it, but the user can't add the same item to the list twice. I'm trying to use equalsIgnoreCase to check user input and see if it is already on the list (an array of items), and if it is, have it output a message and exit the system. The code below is what I have so far, but for some reason when I run it, it automatically outputs the message and exits after the second input, even when that input is not already on the list. I can't figure out what I'm doing wrong - any advice?

    public void itemName()
    {
    boolean found = false;
    for (int i = 0; i < preSortedList.length; i++) {
    System.out.println("Enter the name of an item you want to buy");
    Scanner keyboard = new Scanner(System.in);
    name = keyboard.next();
    for (int j = 1; !found && j < 7; j++) {
    if (name.equalsIgnoreCase(preSortedList[j-1])) {
    found = true;
    System.out.println("That item is already on the list. Please start over and try again");
    System.exit(0);
    }
    else {
    preSortedList[i] = name;
    }
    }

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: help using equalIgnoreCase in an array

    Please wrap your code with code tags like this:
    Java Code:
    public void itemName()
    {
    	boolean found = false;
    	for(int i = 0; i < preSortedList.length; i++)
    	{
    		System.out.println("Enter the name of an item you want to buy");
    		Scanner keyboard = new Scanner(System.in);
    		name = keyboard.next();
    		for(int j = 1; !found && j < 7; j++)
    		{
    			if(name.equalsIgnoreCase(preSortedList[j - 1]))
    			{
    				found = true;
    				System.out.println("That item is already on the list. Please start over and try again");
    				System.exit(0);
    			}
    			else
    			{
    				preSortedList[i] = name;
    			}
    		}
    	}
    }
    Now, I suggest making a method that checks if the value is in the array:
    Something like this:

    Java Code:
    public static boolean arrayContains(String[] arr, String val)
    {
    	for(String str : arr) //for every string in the array
    	{
    		if(str.equalsIgnoreCase(val)) //if they are equalIgnoreCase
    			return true; //return true if there is a match
    	}
    	return false; //return false if there is no match
    }

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 09:39 PM
  2. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  3. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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