在java中只接受唯一的输入

在java中只接受唯一的输入,java,validation,input,Java,Validation,Input,我有下面的java代码,它接受10个字符串输入。我只需要接受唯一的输入。我的意思是说,如果第一个设备地址是“123”,第二个设备地址也是“123”,那么它应该显示一个错误 这是示例代码,我缩短了它 InputStreamReader istream = new InputStreamReader(System.in) ; BufferedReader bufRead = new BufferedReader(istream) ; for (int i=0; i<10; i++

我有下面的java代码,它接受10个字符串输入。我只需要接受唯一的输入。我的意思是说,如果第一个设备地址是“123”,第二个设备地址也是“123”,那么它应该显示一个错误

这是示例代码,我缩短了它

  InputStreamReader istream = new InputStreamReader(System.in) ;
  BufferedReader bufRead = new BufferedReader(istream) ;

  for (int i=0; i<10; i++)
  {
  try {
    System.out.println("Device Address: ");
    String DevAdd = bufRead.readLine();

    System.out.println("Device address:" + DevAdd);
     }
     catch (IOException err) {
          System.out.println("Error reading line");
     }
     catch(NumberFormatException err) {
          System.out.println("Error Converting Number");
     }     
     }
InputStreamReader istream=新的InputStreamReader(System.in);
BufferedReader bufRead=新的BufferedReader(istream);

对于(int i=0;i如果值存在并抛出错误,则将其存储在集合检查中。
使用
Set
也可以做到这一点,但即使不检查(请求抛出异常)该值是否存在,也只允许添加唯一值。可以做以下两件事

1) 使用类似于波希米亚人提到的
if(!set.add(input))


2) 使用
集合。包含

最好将其存储在集合中。每次向集合添加条目之前,请检查该条目是否属于集合。如果没有添加,则抛出错误。

请检查以下内容:

Set<String> inputSet= new HashSet<String>();

for (int i = 0; i < 10; i++) {
    try {
        System.out.println("Device Address: ");
        String DevAdd = bufRead.readLine();

        // Check if already exists. If yes, throw exception
        if(inputSet.contains(DevAdd)) {
            throw new Exception("Already exists");
        }
        //  Indicates input does not exist in the set. Add it       
        inputSet.add(DevAdd);

        System.out.println("Device address:" + DevAdd);
    } catch (IOException err) {
        System.out.println("Error reading line");
    } catch (NumberFormatException err) {
        System.out.println("Error Converting Number");
    }
}
Set inputSet=new HashSet();
对于(int i=0;i<10;i++){
试一试{
System.out.println(“设备地址:”);
字符串DevAdd=bufRead.readLine();
//检查是否已经存在。如果是,则引发异常
if(inputSet.contains(DevAdd)){
抛出新异常(“已存在”);
}
//指示集合中不存在输入。请添加它
inputSet.add(DevAdd);
System.out.println(“设备地址:+DevAdd”);
}捕获(IOException err){
System.out.println(“错误读取行”);
}捕获(NumberFormatException错误){
System.out.println(“错误转换编号”);
}
}

将这些值保存在映射中作为
映射
,这样,当您尝试验证地址是否唯一时,只需访问这些映射,您就可以轻松知道地址是否唯一

使用
,如下所示:

// before the loop:
Set<String> set = new HashSet<String();

// inside loop:
String id = // read id;

if (!set.add(id)) {
    // error - id has been used before
}
//循环之前:

Set Set=new HashSet尽管Set是最适合此senario的数据结构,因为它不允许重复的数据结构(请参见Set的ankur答案)。您也可以使用如下列表

public class HelloWorld{

     public static void main(String []args){
        InputStreamReader istream = new InputStreamReader(System.in) ;
  BufferedReader bufRead = new BufferedReader(istream) ;
  List<String> list=new ArrayList<String>();
  for (int i=0; i<10; i++)
  {
  try {
    System.out.println("Device Address: ");
    String DevAdd = bufRead.readLine();
    if(!list.contains(DevAdd))
    list.add(DevAdd);
    else
    throw new Exception("Already exists");
    System.out.println("Device address:" + DevAdd);
     }
     catch (IOException err) {
          System.out.println("Error reading line");
     }
     catch(NumberFormatException err) {
          System.out.println("Error Converting Number");
     }     
     }
     }
}
公共类HelloWorld{
公共静态void main(字符串[]args){
InputStreamReader istream=新的InputStreamReader(System.in);
BufferedReader bufRead=新的BufferedReader(istream);
列表=新的ArrayList();

对于(int i=0;i为什么不使用HashMap?读取值,检查它是否在映射中,如果它是-抛出异常;如果不是-添加到映射显示一个示例。另外,一个集合会更好(map.containsKey()而不是containsValue())是的,但是如果你给它一个重复的值,Set不会抛出异常。但是它确实返回一个可以检查的布尔值。你可以做一个
集。包含
来检查这个值是否存在。更新我的答案,Set肯定是一个更好的选择。你可以在ankurs的答案中看到这个例子。那么你的答案有什么用呢我