Hi, I have a program that allows user to input a string from console. Then the program will delete all the duplicated words. Something like?
if user input: apple plane pen school plane
the system will return: apple plane pen school
I wrote a program to do this? All seems alright, but couldn't get a correct output? can anyone have a look?
Chinese Java Faq Forum
if user input: apple plane pen school plane
the system will return: apple plane pen school
I wrote a program to do this? All seems alright, but couldn't get a correct output? can anyone have a look?
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class DeleteDup {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String line;
ArrayList words = new ArrayList();
String word;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
line = br.readLine();
StringTokenizer st = new StringTokenizer(line);
words.add(st.nextToken());
while(st.hasMoreTokens()){
word=st.nextToken();
for(int i=0;i<words.size();i++){
if(words.get(i).equals(word)==false){
words.add(word);
break;
}
}
}
for(int i=0;i<words.size();i++){
System.out.println(words.get(i));
}
}
}
Chinese Java Faq Forum