[英]How can i verify if i'm adding atributes to a list that are equal?
import entidades.*;
public class Main {
public static void main(String[] args) {
Profissional prof = new Profissional(null, null);
List<Profissional> profissional = new ArrayList<Profissional>();
Scanner sc = new Scanner(System.in);
boolean loop = true;
while(loop == true) {
String comando = sc.next().toUpperCase();
if (comando.contentEquals("RP")) {
String nomePro = sc.nextLine();
String categoriaPro = sc.nextLine();
prof.NomeVerificacao(profissional, nomePro, categoriaPro);
}
if(comando.contentEquals("SAIR")) {
break;
}
}
for(Profissional pro : profissional) {
System.out.println(pro);
This is my Main, it's running fine but i don´t think it is adding the atributes to the list and not verifying either.这是我的 Main,它运行良好,但我认为它没有将属性添加到列表中,也没有进行验证。
i want to add the atributes to a list so i can create different objets but they can not have at least the name equal.我想将属性添加到列表中,以便我可以创建不同的对象,但它们至少不能具有相同的名称。
public class Profissional {
private String nome;
private String categoria;
public Profissional(String nome, String categoria) {
this.nome = nome;
this.categoria = categoria;
}
public void NomeVerificacao(List<Profissional> profissional ,String nome, String categoria) {
if(profissional.isEmpty() == true) {
profissional.add(new Profissional(nome, categoria));
}else {
for(Profissional pro : profissional) {
if(pro.nome.contentEquals(nome)) {
System.out.println("Já Exite esse nome");
}else {
profissional.add(new Profissional(nome, categoria));
}
}
}
}
@Override
public String toString() {
return "nome=" + nome + ", categoria=" + categoria;
}
}
this is the Profissional Class.这是专业课。
I'm almost there i think but the output keeps saying that the name exists even though it is the first name i'm inserting.我想我快到了,但输出一直说这个名字存在,即使它是我插入的第一个名字。
I ran your code on my machine and made 3 changes into it, and it's working for me now,我在我的机器上运行你的代码并对其进行了 3 次更改,现在它对我有用,
1) 1)
String nomePro = sc.next(); String nomePro = sc.next(); String categoriaPro = sc.next();
String categoryPro = sc.next();
2) In professional class just changed this function a bit: 2)在专业课上只是稍微改变了这个功能:
public void NomeVerificacao(List<Profissional> profissional, String nome, String categoria) {
if (profissional.isEmpty() == true) {
profissional.add(new Profissional(nome, categoria));
} else {
int i = 0;
for (; i < profissional.size(); i++) {
if (profissional.get(i).nome.equals(nome)) {
System.out.println("Já Exite esse nome");
break;
}
}
if (i == profissional.size()) {
profissional.add(new Profissional(nome, categoria));
}
}
}
3) At the end of the class Main, wrote sc.close(); 3)在Main类的最后,写了sc.close(); to close the scanner.
关闭扫描仪。
i/p and o/p :
1) RP
red
color
2) RP
orange
color
3) RP
orange
paint
Já Exite esse nome
4) SAIR
nome=red, categoria=color
nome=orange, categoria=color
As you can see in above i/p and o/p, nome=red and nome=orange with categoria=color are added in the list but when we tried to add the same nome=orange again but with different category as paint it didn't add it and printed the message "Já Exite esse nome".正如您在上面的 i/p 和 o/p 中看到的,在列表中添加了带有类别 = 颜色的 nome=red 和 nome=orange,但是当我们尝试再次添加相同的 nome=orange 但与油漆不同的类别时,它没有't 添加它并打印消息“Já Exite esse nome”。 and after entering SAIR, the toString();
进入SAIR后,toString(); printed the list content at the end.
最后打印列表内容。 So the message will be printed only if we try to add the object with the same name again int list (not the first or any other times).
因此,只有当我们尝试再次添加具有相同名称的对象时,才会打印消息 int list (不是第一次或任何其他时间)。
Further optimizations are possible but for now, it will work!进一步优化是可能的,但就目前而言,它会起作用!
package javaapplication8;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JavaApplication8 {
public static class Profissional {
private String nome;
private String categoria;
public Profissional(String nome, String categoria) {
this.nome = nome;
this.categoria = categoria;
}
}
public static void main(String[] args) {
try {
List<Profissional> profissionalList= new ArrayList<>();
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("\r\nEnter comando:");
String comando = sc.next().toUpperCase();
if (comando.contentEquals("RP")) {
System.out.print("nome: ");
String nome = sc.next();
sc.nextLine(); // wait enter
System.out.print("categoria: ");
String categoria = sc.next();
sc.nextLine(); // wait enter
// access constructor of Profissional
Constructor profCtor = Profissional.class.getConstructor(String.class, String.class);
profCtor.setAccessible(true);
// create instance of Profissional
Profissional newItem = (Profissional) profCtor.newInstance(nome, categoria);
// avoid duplicate nome in profissionalList
boolean isExist = false;
for(Profissional pro : profissionalList) {
if(pro != null){
if(pro.nome.toLowerCase().equals(newItem.nome.toLowerCase())){
isExist = true;
break;
}
}
}
if(!isExist){
profissionalList.add(newItem );
}
}
if(comando.contentEquals("SAIR")) {
break;
}
}
for(Profissional pro : profissionalList) {
if(pro != null) {
System.out.println("nome: " + pro.nome + " categoria: " + pro.categoria);
}
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
I can propose the following solution:我可以提出以下解决方案:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Set is a data structure that makes sure you don't have a duplicated elements
// in this case we use TreeSet structure that accepts comparator which tells that
// we need to compare elements only by professional's name
Set<Profissional> profissionals = new TreeSet<>(Comparator.comparing(Profissional::getNome));
while (true) {
String comando = sc.next().toUpperCase();
if (comando.contentEquals("RP")) {
String nomePro = sc.next();
String categoriaPro = sc.next();
// add function returns true in case the element we're going to add
// was not presented in Set structure yet. False otherwise.
boolean isNew = profissionals.add(new Profissional(nomePro, categoriaPro));
if (!isNew) {
System.out.println("Professional with name " + nomePro + " already exists");
} else {
System.out.println("Professional with name " + nomePro + " was added");
}
} else if (comando.contentEquals("SAIR")) {
break;
}
}
// just prints all professionals at the end of the program
profissionals.forEach(System.out::println);
}
public static class Profissional {
private String nome;
private String categoria;
public Profissional(String nome, String categoria) {
this.nome = nome;
this.categoria = categoria;
}
// getters and setters
@Override
public String toString() {
return "nome=" + nome + ", categoria=" + categoria;
}
}
The output will be the following:输出如下:
RP
RP
test test
测试一下
Professional with name test was added
添加了名称为 test 的 Professional
RP
RP
test1 test1
测试 1 测试 1
Professional with name test1 was added
添加了名为 test1 的专业人士
RP
RP
test test3
测试测试3
Professional with name test already exists
名称为 test 的 Professional 已存在
SAIR
SAIR
nome=test, categoria=test
名称=测试,类别=测试
nome=test1, categoria=test1
名称=test1,类别=test1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.