package Aula12;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Ex1 {
public static void main(String[] args) {
try {
Scanner input = new Scanner(new FileReader("words.txt"));
while (input.hasNext()) {
String word = input.next();
System.out.println(word);
}
ArrayList<String> texto = new ArrayList<>();
ArrayList<String> verificador = new ArrayList<>();
while (input.hasNext()) {
String word = input.next();
texto.add(word);
}
int palavras = texto.size();
System.out.println("Número total de palavras: " + palavras);
for (String i : texto) {
if (verificador.contains(i)) {
continue;
} else {
verificador.add(i);
}
}
int palavrasdiff = verificador.size();
System.out.println("Número de palavras diferentes: " + palavrasdiff);
input.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
The file "words.txt" is in the src folder inside my Java project but the system can´t find it.
I expected the code to find the file, read it and count how many words it has and how many different words it has too but the system is unable to find the refered file.