3

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++;
Rohit Jain
214k45 gold badges417 silver badges534 bronze badges
asked Oct 24, 2013 at 11:48
8
  • 1
    I don't understand the question. WHat exactly prevents you from going through the array and checking for duplicates? Commented Oct 24, 2013 at 11:51
  • 1
    there is missing some code Commented Oct 24, 2013 at 11:51
  • add them to a set, if it already exists, it wont allow you to do so. convert to an array at the end (should you really need an array specifically?) Commented Oct 24, 2013 at 11:52
  • don't write your own printArray-method. use System.out.println(Arrays.toString(stringArray)); Commented Oct 24, 2013 at 11:53
  • lngo, the problem is that I don't know how to loop through the array and see if the input string value already exists. (I'm a beginner) Commented Oct 24, 2013 at 11:54

7 Answers 7

3

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++;
 }
answered Oct 24, 2013 at 12:01
2
  • I only used loops and arrays assuming this is for a school assignment and you could not use other data types. Commented 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. :) Commented Oct 24, 2013 at 12:26
3

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.
}
answered Oct 24, 2013 at 11:52
1

When you got the String input, you can create a method that will :

  1. Go through the entire array and check if the string is in it (you can use equals() to check content of Strings)
  2. Returns a boolean value wheter the string is in the array or not
  3. 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.


answered Oct 24, 2013 at 11:51
1
  • 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 :( Commented Oct 24, 2013 at 11:52
0

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.

answered Oct 24, 2013 at 11:51
0
0

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.

answered Oct 24, 2013 at 11:53
0
while (no input or duplicated){
 ask for a new string
 if (not duplicated) {
 store the string in the array
 break;
 }
}
answered Oct 24, 2013 at 11:55
0
0

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.

answered Oct 24, 2013 at 11:51
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.