Results 1 to 2 of 2
- 02-24-2013, 10:03 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
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;
}
}
- 02-24-2013, 10:12 PM #2
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Re: help using equalIgnoreCase in an array
Please wrap your code with code tags like this:
Now, I suggest making a method that checks if the value is in the array: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; } } } }
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
-
[Problem] Enhanced for-loop with 2D arrays (or array in array)
By thewrongsyntax in forum New To JavaReplies: 0Last Post: 10-07-2012, 08:49 PM -
Reading a text file into an Array and spliting the content into another Array
By jtothemax in forum New To JavaReplies: 15Last Post: 05-14-2012, 12:42 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks