In: Computer Science
Exercise 4 – Lists and input
Using a function Write a program that prompts the user to input a sequence of words. The program then displays a list of unique words (words that only occurred once, i.e. no repeated).
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class ListsInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter sequence of words:"); String s = scanner.nextLine(); String[] splits = s.split(" "); List<String> list = new ArrayList<>(); boolean f; for(String x: splits){ f = false; for(int i = 0;i<list.size();i++){ if(x.equals(list.get(i))){ f = true; break; } } if(!f){ list.add(x); } } System.out.println(list); } }