所以我有一个打开文件的程序" input.txt"其中包含一个名称列表。我有多种方法来组织名称,相应地大写,并显示它们。我遇到麻烦的是程序的输出方面。当我使用我将在下面显示的countOccurrence方法时,它会正确计算每个名称在列表中出现的次数,无论它是名字还是姓氏。问题是它没有以我想要的方式显示计数。我将获得我得到的输出和我想要的输出如下所示,如果有人可以提供帮助,那将非常感激。
public class Names {
public static void display(ArrayList<String> names) {
for (int i = 0; i < names.size(); i = i + 1) {
System.out.println(names.get(i));
}
}
public static int find(String s, ArrayList<String> a) {
for (int i = 0; i < a.size(); i = i + 1) {
String str = a.get(i);
if (str.equals(s)) {
return i;
}
}
return -1;
}
public static void capitalize(ArrayList<String> names) {
for (int i = 0; i < names.size(); i = i + 1) {
String name = names.get(i);
if (!name.isEmpty()) {
String firstLetter = "" + name.charAt(0);
names.set(i, firstLetter.toUpperCase() + name.substring(1).toLowerCase());
}
}
}
public static void sort(ArrayList<String> names) {
for (int i = 0; i < names.size() - 1; i = i + 1) {
int Min = i;
for (int j = i + 1; j < names.size(); j = j + 1) {
if (names.get(j).compareTo(names.get(Min)) < 0) {
Min = j;
}
}
String tmp = names.get(i);
names.set(i, names.get(Min));
names.set(Min, tmp);
}
}
public static void getNames(ArrayList<String> fn, ArrayList<String> ln) throws IOException {
Scanner kb = new Scanner(System.in);
System.out.println("What is the input flie?");
String names = kb.next();
File inpFile = new File(names);
Scanner in = new Scanner(inpFile);
while (in.hasNext()) {
String firstName = in.next();
String lastName = in.next();
fn.add(firstName);
ln.add(lastName);
}
}
private static int countOccurence(String name,ArrayList<String> names){
int count = 0;
for(int i =0; i < names.size(); i++){
if(name.equalsIgnoreCase(names.get(i))){
count++;
}
}
return count;
}
public static void main(String[] args) throws IOException {
ArrayList<String> first = new ArrayList<>();
ArrayList<String> last = new ArrayList<>();
getNames(first, last);
capitalize(first);
capitalize(last);
ArrayList<String> allNames = new ArrayList<>();
for (int i = 0; i < first.size(); i++) {
allNames.add(last.get(i) + ", " + first.get(i));
}
System.out.println("*******All Names******");
sort(allNames);
display(allNames);
System.out.println("\n*****First Name Count***");
sort(first);
for(int i =0; i < first.size(); i++){
int count = countOccurence(first.get(i), first);
System.out.println(first.get(i) + " " + count + " times.");
}
System.out.println("\n****Last Name Count****");
sort(last);
for(int i =0; i < last.size(); i++){
int count = countOccurence(last.get(i), last);
System.out.println(last.get(i) + " " + count + " times.");
}
System.out.println("\n****All Names Count****");
sort(allNames);
for(int i =0; i < last.size(); i++){
int count = countOccurence(allNames.get(i), allNames);
System.out.println(allNames.get(i) + " " + count);
}
*****First Name Count***
Adriana 4 times.
Adriana 4 times.
Adriana 4 times.
Adriana 4 times.
Colette 4 times.
Colette 4 times.
Colette 4 times.
Colette 4 times.
Emmanuel 1 times.
Gerri 1 times.
Gretta 1 times.
Kirsten 2 times.
Kirsten 2 times.
Marcia 2 times.
Marcia 2 times.
Neva 1 times.
Shawanda 1 times.
Tijuana 1 times.
****Last Name Count****
Beres 2 times.
Beres 2 times.
Beumer 1 times.
Hutt 2 times.
Hutt 2 times.
Jones 2 times.
Jones 2 times.
Koenig 1 times.
Means 1 times.
Montilla 4 times.
Montilla 4 times.
Montilla 4 times.
Montilla 4 times.
Mossman 1 times.
Sapienza 2 times.
Sapienza 2 times.
Shover 1 times.
Stanfill 1 times.
****All Names Count****
Beres, Kirsten 2
Beres, Kirsten 2
Beumer, Gretta 1
Hutt, Colette 1
Hutt, Shawanda 1
Jones, Colette 1
Jones, Marcia 1
Koenig, Gerri 1
Means, Tijuana 1
Montilla, Adriana 4
Montilla, Adriana 4
Montilla, Adriana 4
Montilla, Adriana 4
Mossman, Emmanuel 1
Sapienza, Colette 2
Sapienza, Colette 2
Shover, Neva 1
Stanfill, Marcia 1
*******名字数*********
Adriana 4
Colette 4
Emmanuel 1Gerri 1
格雷塔1
Kirsten 2
Marcia 2
涅瓦1
Shawanda 1
蒂华纳1
*******姓氏数*********
Beres 2
Beumer 1
Hutt 2
琼斯2
Koenig 1
意味着1
蒙蒂利亚4
Mossman 1
Sapienza 2
Shover 1
Stanfill 1
*******所有名字都算*********
Beres,Kirsten 2
Beumer,Gretta 1
Hutt,Colette 1
Hutt,Shawanda 1
Jones,Colette 1
Jones,Marcia 1
Koenig,Gerri 1
手段,蒂华纳1
Montilla,Adriana 4
Mossman,Emmanuel 1
Sapienza,Colette 2
Shover,Neva 1
Stanfill,Marcia 1
答案 0 :(得分:0)
有两种方法可以解决这个问题:
第二种方式更有用,所以这里有一个例子:
ArrayList<String> tmp = new ArrayList<String>();
for(int i =0; i < first.size(); i++){
if(!tmp.contains(first.get(i))) {
int count = countOccurence(first.get(i), first);
System.out.println(first.get(i) + " " + count + " times.");
tmp.add(first.get(i));
}
}
如果您多次使用tmp变量,请不要忘记清除()它。