英文:
Check for duplicate values for ip address (user input) using hashmap in java
问题
以下是您要翻译的代码部分:
// 主类用于输入所有域名详情,如域名和IP地址。我需要检查输入的IP地址是否重复。我需要一个用于检查重复值的哈希映射代码。
// 输入将是 www.gmail.net 195.116.254.154
// 输出将是 - 如果IP地址是 195.116.254.154,则输出应为 www.gmail.net
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DomainBO dBO = new DomainBO();
Map<String, String> domainMap = new HashMap<>();
dBO.setDomainMap(domainMap);
String ip, Dname;
int flag = 0;
int input;
int count = 0;
String DomainName = null;
do {
System.out.println("1. 添加DNS详情");
System.out.println("2. 查找匹配的域名");
System.out.println("3. 退出");
System.out.println("输入您的选择");
input = sc.nextInt();
sc.nextLine();
if (input == 1) {
count++;
System.out.println("输入域名");
Dname = sc.nextLine();
System.out.println("输入IP地址");
ip = sc.nextLine();
if (isValid(Dname) || IPAddressValidator(ip)) // 检查有效的IP地址和名称
{
dBO.addDNSDetails(Dname, ip);
flag = 0;
} else
flag = 0;
}
if (input == 2) {
if (count > 0) {
System.out.println("输入要查找域名的IP地址");
ip = sc.nextLine();
DomainName = dBO.findDomainName(ip);
if (DomainName == null) {
System.out.println("未找到匹配的域名");
flag = 0;
} else {
System.out.println(DomainName);
flag = 0;
}
}
}
if (input == 3) {
System.out.println("感谢使用应用程序");
flag = 1;
break;
}
} while (flag == 0);
}
// DomainBO.java 类用于输入域名详情并通过输入IP地址检索域名。
public class DomainBO {
private Map<String, String> domainMap;
public Map<String, String> getDomainMap() {
return domainMap;
}
public void setDomainMap(Map<String, String> domainMap) {
this.domainMap = domainMap;
}
// 此方法应将域名作为键和它们的IP地址作为值添加到 Map 中
public void addDNSDetails(String domainName, String ipAddress) {
if (domainMap.containsKey(ipAddress)) {
return;
} else {
domainMap.put(domainName, ipAddress);
}
}
public String findDomainName(String ipAddress) {
String domain = null;
if (domainMap.isEmpty()) {
return domain;
} else
for (Map.Entry<String, String> entry : domainMap.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
if (ipAddress.equals(v)) {
domain = k;
} else {
domain = "未找到匹配的域名";
}
}
return domain;
}
}
请注意,我已经将代码部分进行了翻译,如您所请求。如果您需要任何其他帮助,请随时告诉我。
英文:
Main class to enter all the domain details like Domain name and ip address. I need to check for duplicate values entered for ip address. I need a code for hashmap to check for duplicate values.
Input would be www.gmail.net 195.116.254.154
output would be like - if the ipAddress is 195.116.254.154 the output should be www.gmail.net
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DomainBO dBO = new DomainBO();
Map<String, String> domainMap = new HashMap<>();
dBO.setDomainMap(domainMap);
String ip, Dname;
int flag = 0;
int input;
int count = 0;
String DomainName = null;
do {
System.out.println("1. Add DNS details");
System.out.println("2. Find matching Domain Name");
System.out.println("3. Exit");
System.out.println("Enter your choice");
input = sc.nextInt();
sc.nextLine();
if (input == 1) {
count++;
System.out.println("Enter the domain name");
Dname = sc.nextLine();
System.out.println("Enter the IP address");
ip = sc.nextLine();
if (isValid(Dname) || IPAddressValidator(ip))// check for valid ipaddress and name
{
dBO.addDNSDetails(Dname, ip);
flag = 0;
}
else
flag = 0 ;
}
if (input == 2) {
if (count > 0) {
System.out.println("Enter the IP address to find the domain name");
ip = sc.nextLine();
DomainName = dBO.findDomainName(ip);
if (DomainName == null) {
System.out.println("No matching domain name found");
flag = 0;
} else {
System.out.println(DomainName);
flag = 0;
}
}
}
if (input == 3) {
System.out.println("Thank you for using the Application");
flag = 1;
break;
}
} while (flag == 0);
}
The DomainBO.java class is used to enter domain details and retrieve the domain name by inputting Ipaddress.
public class DomainBO {
private Map<String,String> domainMap;
public Map<String,String> getDomainMap() {
return domainMap;
}
public void setDomainMap(Map<String,String> domainMap) {
this.domainMap = domainMap;
}
//This method should add the domainName as key and their ipAddress as value into a Map
public void addDNSDetails (String domainName,String ipAddress)
{
if(domainMap.containsKey(ipAddress))
{
return;
}
else
{
domainMap.put(domainName, ipAddress);
}
}
public String findDomainName(String ipAddress) {
String domain=null;
if(domainMap.isEmpty()){
return domain;
}
else
for (Map.Entry<String, String> entry : domainMap.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
if (ipAddress.equals(v)) {
domain = k;
}
else
{
domain = "No matching domain name found";
}
}
return domain;
}
}
答案1
得分: 1
您说您想要将域名映射到IP地址。但是您检查了domainMap.containsKey(ipAddress)
。这是不正确的,因为您的键是域名,而不是IP地址。
如果您需要在值(IP地址)中检查重复项,您可以使用以下代码:
for (Map.Entry<String, String> set : domainMap.entrySet()) {
if (set.getValue().equals(currentIp))
System.out.println("Duplicate ip"); // 在此处执行您想要的任何操作
}
英文:
You say that you want to map domainName to ip addr. but you check for domainMap.containsKey(ipAddress). That is not right because your keys are domain names, not ip addresses.
You can use a code like this to check for duplicates in values(ip addr.) if that is what you need
for (Map.Entry<String, String> set : domainMap.entrySet()) {
if (set.getValue().equals(currentIp))
System.out.println("Duplicate ip"); // Do whatever you want in here
}
答案2
得分: 0
在addDNSDetails
方法中,您编写了以下代码:
domainMap.put(domainName, ipAddress);
第一个参数是用于查找数据的键,第二个参数是值。您将域名作为键,但根据您的描述,想要使用IP地址来查找域名,而不是相反的方式。因此,请将此代码更改为:
domainMap.put(ipAddress, domainName);
英文:
In the addDNSDetails method you've written
domainMap.put(domainName, ipAddress);
The first parameter is the key used to find the data, the second parameter is the value. You're putting the domain name as the key, but by your description want to use the IP address to find the domain, not the other way around. So change this code to:
domainMap.put(ipAddress, domainName);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论