I am creating a program that lets you store 10 items in an array. What I haven't been able to get the program to do is give an error if one of the entered items already exists in the array.
So, for example, if the array looks like [banana, potato, 3, 4, yes, ...] and I enter banana again, it should say "Item has already been stored" and ask me to re-enter the value. The code I currently have is:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int stringNumber = 0;
String[] stringArray = new String[10];
for (int i = 0; i <= stringArray.length; i++) {
out.println("\nEnter a string");
String input = keyboard.next();
stringArray[stringNumber] = input;
out.println("\"" + stringArray[stringNumber] + "\"" + " has been stored.");
PrintArray(stringArray);
stringNumber++;
7 Answers 7
You can use nested loops to go through the array to see if the new input exists. It would be better to do this in a function. Also when doing this you need to make sure that you are not at the first element or you will get a null pointer exception.
for (int i = 0; i <= stringArray.length; i++) {
boolean isInArray = false;
System.out.println("\nEnter a string");
String input = keyboard.next();
if (i > 0) {
for (int j = 0; j < stringArray.length; j++) {
if (stringArray[j].equalsIgnoreCase(input)) {
isInArray = true;
break;
}
}
}
if (!isInArray) {
stringArray[stringNumber] = input;
} else {
System.out.println("\"" + stringArray[stringNumber-1] + "\""
+ " has been stored.");
}
PrintArray(stringArray);
stringNumber++;
}
-
I only used loops and arrays assuming this is for a school assignment and you could not use other data types.Matt Penna– Matt Penna2013年10月24日 12:03:11 +00:00Commented Oct 24, 2013 at 12:03
-
There's a small bug in this code, but I'm working on fixing it! Thanks Matt, this gave me a much better perspective on the direction I should be taking with this program. :)Gregg1989– Gregg19892013年10月24日 12:26:57 +00:00Commented Oct 24, 2013 at 12:26
It's always better to use a HashSet
when you don't want to store duplicates. Then use HashSet#contains()
method to check if element is already there. If ordering is important, then use LinkedHashSet
.
If you really want to use an array, you can write a utility method contains()
for an array. Pass the array, and the value to search for.
public static boolean contains(String[] array, String value) {
// Iterate over the array using for loop
// For each string, check if it equals to value.
// Return true, if it is equal, else continue iteration
// After the iteration ends, directly return false.
}
- For iterating over the array, check enhanced for statement.
- For comparing String, use
String#equals(Object)
method.
When you got the String input, you can create a method that will :
- Go through the entire array and check if the string is in it (you can use
equals()
to check content of Strings) - Returns a boolean value wheter the string is in the array or not
- Then just add a while structure to re-ask for an input
Basically it can look like this :
String input = "";
do {
input = keyboard.next();
}while(!checkString(input))
The checkString
method will just go through all the array(using a for loop as you did to add elements) and returns the appropriate boolean
value.
-
That's exactly what I thought I should do. The thing is that I've been using Java for 2 days and I'm not sure how to write that piece of code :(Gregg1989– Gregg19892013年10月24日 11:52:41 +00:00Commented Oct 24, 2013 at 11:52
Without introducing some order in your array and without using an addition structure for instance HashSet, you will have to look through the whole array and compare the new item to each of the items already present in the array.
For me the best solution is to have a helper HashSet to check the item for presence.
Also have a look at this question.
To avoid you should use an Set
instead of an array and loop until size = 10.
If you need to keep an array, you can use the .contains()
method to check if the item is already present in the array.
while (no input or duplicated){
ask for a new string
if (not duplicated) {
store the string in the array
break;
}
}
You should check the input value in array before inserting into it. You can write a method like exists
which accepts String[]
& String
as input parameter, and find the string into the String array, if it finds the result then return true
else false
.
public boolean exists(String[] strs, String search){
for(String str : strs){
if(str.equals(search))
return true;
}
return false;
}
performance would be O(n) as it searchs linearly.
printArray
-method. useSystem.out.println(Arrays.toString(stringArray));