Java array question -


sorry, silly question here, have googled search seems returning methods of using binary search etc., need simpler.

i have array of languages. creating scanner, asking input. trying make if language input user isn't in array, displays error , asks again. should simple, have drawn blank.

can please ? here have far !

      scanner scan = new scanner(system.in);       language = scan.next();       while( language not in array languages) {        system.out.print("error!");        language = scan.next();      } 

i understand question better now. should use set sure. want use contains() method of set check if 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:
yo want use set collection type. set not allow duplicate entries. javadocs:

a collection contains no duplicate elements.

set<string> set = new hashset<string>(); // not add entry if set contains language set.add(language); 

also, if want know if value rejected or not, can use return type of add() method. returns true if item did not exist, , false otherwise.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

sql server - python to mssql encoding problem -

windows - Python Service Installation - "Could not find PythonClass entry" -