2

Sorry, silly question here, I have googled it but anything I search for seems to be returning methods of using binary search etc., but what I need is actually much simpler.

I have an array of languages. I am creating a scanner, asking for input. Trying to make it so that if the language input by the user isn't in the array, it displays an error and asks again. Should be simple, I have just drawn a blank.

Can anyone help please ? Here is what I have so far !

      Scanner scan = new Scanner(System.in);
      language = scan.next();
      while( language NOT IN ARRAY languages) {
       System.out.print("error!");
       language = scan.next();
     }
5
  • That is not even valid Java code... Commented Jan 25, 2011 at 18:04
  • So the issue is trying determine whether a value is in an array already, i.e. searching for a specific value in an array? Commented Jan 25, 2011 at 18:05
  • I realise this, its psuedo code. Point out where I said it was valid code ? Commented Jan 25, 2011 at 18:06
  • 1
    Bert F - Yes, that is it ! :) I have a string, which the user inputs, I then search the array to see if it is contained within it. Commented Jan 25, 2011 at 18:07
  • Is this homework? Do you need to use only array operations? Commented Jan 25, 2011 at 18:12

4 Answers 4

4

I understand your question better now. You should use a Set for sure. But you will want to use the contains() method of the set to check if the language exists.

Scanner scan = new Scanner(System.in);
language = scan.next();
while(!set.contains(language)) {
    System.out.print("error!");
    language = scan.next();
}

Old answer, still relevant info though:
What yo want to use is a Set collection type. A set does not allow duplicate entries. From the Javadocs:

A collection that contains no duplicate elements.

Set<String> set = new HashSet<String>();
// will not add another entry if set contains language already
set.add(language);

Also, if you want to know if the value was rejected or not, you can use the return type of the add() method. It returns true if the item did not exist, and false otherwise.

Sign up to request clarification or add additional context in comments.

2 Comments

strange way to use set; why not just use the contains() method? Also this way he will accumulate all the inputs in the set which is not what he wants.
This is almost a complete answer. I would also point out the contains( "xyz" ) api on the Set class, which can be used to drive that loop.
1

You can do something like:

public static boolean contains(String language, String[] lang_array) {
    for (int i = 0; i < lang_array.length; i++) {
        if (lang_array[i].equals(language))
            return true;
    }

    return false;
}

public static void main(String[] args) {
    String[] lang_array = {"Java", "Python", "Ruby"};
    Scanner scan = new Scanner(System.in);
    String language = scan.next();
    while(!contains(language, lang_array)) {
        System.out.print("error!");
        language = scan.next();
    }
}

Comments

1

You can do this is you really need to use an array:

   Arrays.sort(languages);
   Scanner scan = new Scanner(System.in);
   language = scan.next();
   while( Arrays.binarySearch(languages, language) < 0) {
    System.out.print("error!");
    language = scan.next();
   }

1 Comment

Would that be while( !Arrays.binarySearch(languages, language) < 0 ) ? Since you can't use a not on an int and that method returns int ?
0

There are two things you can do:

Either iterate through the array and compare the contents on each element to what you're trying to see if it's there every time you add a language.

OR

Use a LinkedList or some other kind of Java Structure that has a .contains() method, this will, in a way, do something similar to what I mentioned for the Array.

Comments

Your Answer

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.