我想檢查一個對象是否存在於一個鏈表中,並根據它是否存在來執行一個動作,但是,無論我做什麼,java都將所有對象視爲不同。主代碼在下面提供,我很確定邏輯中的錯誤在這個代碼中。文章和客戶類是非常標準的。如果列表包含具有標題的文章,則標誌變量應該爲真,但始終爲false。任何幫助將非常感激。java對象比較
import java.util.*;
import java.io.*;
public class Proj1 {
public static void main(String[] args) throws FileNotFoundException {
LinkedList<Article> Articles = new LinkedList<Article>();
LinkedList<Customer> Customers = new LinkedList<Customer>();
ListIterator<Customer> it = Customers.listIterator();
int id = 0;
String command = "";
if (args.length == 0 || args[0] == null) {
System.out.println("Please give a valid command file");
} else {
try {
Scanner reader = new Scanner(new FileInputStream(args[0]));
while (reader.hasNext()) {
String arg = reader.nextLine();
arg.split(" ");
String[] commands = arg.split("\\s+");
if (isInt(commands[0])) {
id = Integer.parseInt(commands[0]);
command = commands[1];
Customer temp = new Customer(id);
if (Customers.size() == 0) {
Customers.add(temp);
} else {
boolean flag = false;
for (int i = 0; i < Customers.size(); i++) {
if (id == Customers.get(i).getId()) {
flag = true;
}
}
if (flag == false) {
Customers.add(temp);
}
}
} else {
command = commands[0];
}
// System.out.println(id+" "+command);
if (command.equalsIgnoreCase("borrow")) {
String title = "";
int x = commands.length;
boolean flag = false;
for (int j = 2; j < x; j++) {
title += commands[j] + " ";
}
Article Article = new Article(title);
System.out.println(Articles.size());
if (Articles.size() == 0) {
Articles.add(Article);
} else {
for (int i = 0; i < Articles.size(); i++) {
if (Article.getTitle() == Articles.get(i).getTitle()) {
flag = true;
}
}
if (flag == false) {
Articles.add(Article);
}
}
System.out.println(flag);
for (int i = 0; i < Customers.size(); i++) {
if (Customers.get(i).CustomerList().contains(title) && flag == true) {
Article.addToQ(Customers.get(i));
} else {
Customers.get(i).CustomerBorrow(Article);
}
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("return")) {
String title = "";
int x = commands.length;
for (int j = 2; j < x; j++) {
title += commands[j] + " ";
}
Article Article = new Article(title);
if (Articles.size() == 0) {
Articles.add(Article);
} else {
boolean flag = false;
for (int i = 0; i < Articles.size(); i++) {
if (title == Articles.get(i).getTitle()) {
flag = true;
}
}
if (flag == false) {
Articles.add(Article);
}
}
for (int i = 0; i < Customers.size(); i++) {
if (id == Customers.get(i).getId()) {
Customers.get(i).CustomerReturn(Article);
}
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("list")) {
for (int i = 0; i < Customers.size(); i++) {
if (id == Customers.get(i).getId()) {
System.out.println("Customer " + id
+ " currently has: "
+ Customers.get(i).CustomerList());
}
}
} else if (command.equalsIgnoreCase("whohas")) {
String title = "";
int x = commands.length;
for (int i = 1; i < x; i++) {
title += commands[i] + " ";
}
boolean flag = false;
int tempId = 0;
for (int i = 0; i < Customers.size(); i++) {
tempId = Customers.get(i).getId();
if (Customers.get(i).CustomerList().contains(title)) {
flag = true;
tempId = Customers.get(i).getId();
}
}
if (flag = true) {
System.out.println(tempId + " currently has "
+ title);
} else {
System.out
.println("Currently no one has checked out "
+ title);
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("waitlist")) {
String title = "";
int x = commands.length;
for (int i = 1; i < x; i++) {
title += commands[i] + " ";
}
for (int i = 0; i < Customers.size(); i++) {
if (Customers.get(i).CustomerList().contains(title)) {
Articles.get(i).printQ();
}
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("listCustomers")) {
System.out.println("Customers include: ");
for (int i = 0; i < Customers.size(); i++) {
System.out.println(Customers.get(i).getId());
}
} else {
System.out.println("Command not recognized");
}
}
reader.close();
}
catch (Exception e) {
System.out.println("command not formatted correctly");
}
}
}
public static boolean isInt(String string) {
try {
Integer.parseInt(string);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
}
命令,如
29借用 「紐約時報」
29借用 「紐約時報」
允許重複,我試圖避免這種情況。謝謝。
你需要清理大量的代碼,比如'Article.getTitle()== Articles.get(i).getTitle()'(假設'getTitle()'返回'String')。你應該使用'.equals()'。另外,'if(flag == false)'可以替換爲'if(!flag)'。 – 2013-03-09 01:59:37