【发布时间】:2016-03-16 21:45:05
【问题描述】:
我是一名从事项目的学生,我有一个方法接受一个人,将其添加到数组 (Admits[]) 并将数组写入文件。该方法当前不执行此操作,因为 numSoFar 值不会增加并且人员不会保存到数组中。 (或者我在我的测试类中犯了一个错误)我把这两个类的编码都放在下面,希望有人能指出我做错了什么的方向。由于这是一个学校项目,请尽量不要太具体,只是告诉我一些关于为什么我的编码可能不起作用的建议。我不想把别人的工作当作我自己的;我只需要一双新的眼睛来看看我没有的东西。
在我的数据库类中编码
//adds a Person to the Database
public void addPerson(Person admit)
{
for(int i = 0; i < numSoFar; i++)//i = -1 when admit's alpabetical spot in Database has been located
{
if((i == numSoFar-1) || (i == numSoFar))//there is no Person in this index, so this is the end of the database
{
Admits[i] = admit;
numSoFar = numSoFar + 1;
}
else
{
if(Admits[i].getLN().compareTo(admit.getLN()) == -1)//last name comes before last name of Person at index i
{
Person current= Admits[i];
Admits[i] = admit;
while(i <= numSoFar)
{
Person next = Admits[i+1];
Admits[i+1] = current;
current = next;
i++;
}
numSoFar++;
}
else
{
if(Admits[i].getLN().equalsIgnoreCase(admit.getLN()))//admit and Person at index i have the same last name
{
if(Admits[i].getFN().compareTo(admit.getFN()) == -1)
{
Person current= Admits[i];
Admits[i] = admit;
while(i < numSoFar)
{
Person next = Admits[i+1];
Admits[i+1] = current;
current = next;
i++;
}
numSoFar++;
}
else
{
if(Admits[i].getFN().equalsIgnoreCase(admit.getFN())) //admit and Person at index i are the same person
{
Scanner in = new Scanner(System.in);
System.out.println("There is already a person with this name in the database.");
int c = 0;
while(c != 1 || c != 2)
{
System.out.println("If you would like to keep that person, enter '1', and if you would like to replace him/her with the person entered, enter 2.");
if(c == 2)
{
Admits[i] = admit;
}
}
}
}
}
}
}
}
this.writeToFile();
}
这是在我的测试人员中添加新人的代码(我在案例 1 后停止
Person[] Admits = new Person[5000];
Database dB = new Database(Admits);
dB.fillFromFile();//fills array with info from text file
Scanner in = new Scanner(System.in);
String c = "0";
System.out.println("Hello, and thank you for using the Notre Dame admitted students friend-finder");
while(!c.equals("6")) //Menu for user to traverse, is exited when user enters a 6
{
System.out.println("Please pick the desired action from one of the options below and enter its number.");
System.out.println(" 1: Enter info for a new admitted student and check matches");
System.out.println(" 2: Change info for an admitted student already in the database");
System.out.println(" 3: Delete an admitted student from the database");
System.out.println(" 4: Log in as an admitted student to check matches");
System.out.println(" 5: View contact info for a certain person in the database");
System.out.println(" 6: Exit the program");
c = in.next();
switch(c)
{
case "1": //create new student and check matches
System.out.println("Enter your first name:");
String firstName = in.next();
System.out.println("Enter your last name:");
String lastName = in.next();
System.out.println("Enter your gender:");
String gen = in.next();
Person p = new Person(lastName, firstName, gen);
//String chracteristics
System.out.println("Are you an a. introvert or b. extrovert? (enter a or b):");
String traitIntroExtro = in.next();
while(!(traitIntroExtro.equalsIgnoreCase("a") || traitIntroExtro.equalsIgnoreCase("b")))
{
System.out.println("Invalid choice.Please re-enter your choice:");
traitIntroExtro = in.next();
}
if(traitIntroExtro.equalsIgnoreCase("a"))
{
p.setTraitIntroExtro("Introvert");
}
else
{
p.setTraitIntroExtro("Extrovert");
}
上面的编码基本上是用不同的变量重复的,但是因为有很多我会切到案例1的末尾
//facebook url to contact matches with
System.out.println("Please enter the url of your facebook profile page:");
String url = in.next();
p.setFacebookUrl(url);
dB.addPerson(p);
p.fillMatches(dB);
boolean first = dB.first();
if(first == true)//the database only has one person in it
{
System.out.println("You are currently the only person in the database.");
}
else//the database has atleast one person in it
{
System.out.println("Your top 2 most compatible people currently in the data base are:");
System.out.println(p.getMatches().getHead().getPerson() + ", who can be found at " + p.getMatches().getHead().getPerson().getFacebookUrl());
if(dB.getNumSoFar() == 2)
{
System.out.println("This is the only other person in the database.");
}
else
{
System.out.println(p.getMatches().getHead().getNextNode().getPerson() + ", who can be found at " + p.getMatches().getHead().getNextNode().getPerson().getFacebookUrl());
}
}
break;
【问题讨论】:
-
请发布
addToDatabase方法的代码,如果这样不起作用。 -
那是 addPerson。很抱歉没有澄清。
-
我可以建议你尝试使用
else if而不是else { if,这样你就不会得到这么深的缩进代码。 -
Admits[i].getLN().compareTo(admit.getLN()) == -1compareTo接口实际上并不要求在第一个小于第二个的情况下返回 -1 - 只是返回值为负。因此,您实际上应该在这里查看< 0,而不是== -1。 -
是的!我只是把我们学到的所有东西都格式化了,但这会让它不那么碍眼
标签: java arrays object insert writetofile