public static void main(String args[]) throws FileNotFoundException {
Scanner LyricsFile = new Scanner(new FileReader("C:/Users/xxxx/Desktop/lyrics.txt"));
ArrayList<String> LyricsList = new ArrayList<String>();
while (LyricsFile.hasNext()) {
String lyricssArray[] = LyricsFile.nextLine().split(" ");
for (String str : lyricssArray) {
LyricsList.add(str.replaceAll(",|\\.", ""));
}
String[] pangalanStrArray = {"Gagamba", "Araw", "Sanga", "Ulan"};
String[] panghalipStrArray = {"sila"};
String[] pangatnigArray = {"At"};
String[] pandiwaArray = {"Umakyat", "Itinaboy", "Dumating"};
String[] pangUriStr = {"palaging", "natuyo", "maliliit", "masaya"};
String[] pangAbayArray = {};
String[] pangUkolArray = {};
String[] pangAwiArray = {};
String[] pangAngkopArray = {"Na"};
String[] pantukoyArray = {"Ang"};
ArrayList<String> pangngalan = new ArrayList<String>();
for (String name : LyricsList) {
for (String compare : pangalanStrArray) {
if (name.equalsIgnoreCase(compare) && !pangngalan.contains(compare.toLowerCase())) {
pangngalan.add(name.toLowerCase());
break;
}
}
}
System.out.println("1.Pangngalan: " + pangngalan);
// break;
}
}
输出如下:
1.Pangngalan: [gagamba, sanga]
1.Pangngalan: [gagamba, sanga, ulan]
1.Pangngalan: [gagamba, sanga, ulan, araw]
1.Pangngalan: [gagamba, sanga, ulan, araw]
但我只需要这个:
1.Pangngalan: [gagamba, sanga, ulan, araw]
请帮助:(它有点奇怪,我不知道如何删除重复:(它工作正常,但我试图再次运行它,它变得那样。我没有&# 39;甚至添加或删除任何东西:(
修改 我试着按照你们的建议去做,但是它给打印行造成了错误,错误说“PANGNGALAN”无法解析为变量&#39;
答案 0 :(得分:0)
你在while循环中有你的print语句。因此它将在每次迭代中打印。所以移动这一行:
System.out.println("1.Pangngalan: "+ pangngalan);
在while循环的括号之外。您还必须在while循环之前定义列表。然后你的代码将如下所示:
public static void main(String args[]) throws FileNotFoundException {
Scanner LyricsFile = new Scanner(new FileReader("C:/Users/xxxx/Desktop/lyrics.txt"));
ArrayList<String> LyricsList = new ArrayList<String>();
ArrayList<String> pangngalan = new ArrayList<String>();
while (LyricsFile.hasNext()) {
String lyricssArray[] = LyricsFile.nextLine().split(" ");
for (String str : lyricssArray) {
LyricsList.add(str.replaceAll(",|\\.", ""));
}
String[] pangalanStrArray = {"Gagamba", "Araw", "Sanga", "Ulan"};
String[] panghalipStrArray = {"sila"};
String[] pangatnigArray = {"At"};
String[] pandiwaArray = {"Umakyat", "Itinaboy", "Dumating"};
String[] pangUriStr = {"palaging", "natuyo", "maliliit", "masaya"};
String[] pangAbayArray = {};
String[] pangUkolArray = {};
String[] pangAwiArray = {};
String[] pangAngkopArray = {"Na"};
String[] pantukoyArray = {"Ang"};
for (String name : LyricsList) {
for (String compare : pangalanStrArray) {
if (name.equalsIgnoreCase(compare) && !pangngalan.contains(compare.toLowerCase())) {
pangngalan.add(name.toLowerCase());
break;
}
}
}
}
System.out.println("1.Pangngalan: " + pangngalan);
}