Ajuda para Entrevistas
Preparação para a Entrevista
Currículo
Procura de Emprego
Recursos
Subscrição
PT
Telegrama
Iniciar sessão
Menu aberto
Ajuda para Entrevistas
Assistente de IA
Extensão do Navegador
Recursos
Descarregar
FAQ / Ajuda
Preparação para a Entrevista
Banco de Perguntas
Banco de Entrevistas
Entrevistas simuladas
Mentores
Recursos
FAQ / Ajuda
Procura de Emprego
Automação da Procura de Emprego
Análise de Mercado
Vagas
Recursos
Blog
FAQ / Documentação
Telegram Blog
Suporte ao Telegram
Jurídico
Reembolso
Termos
Privacidade
Titulares de direitos
Voltar às perguntas
Banco de Perguntas
Java - Desenvolvedor
Pergunta
Senior
import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class badCounter { public static Map<String, Integer> mapOfBadNames = new HashMap<>(); protected void doProcessing(String filePath) throws IOException { try { FileInputStream fis = new FileInputStream(filePath); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); while((br.readLine())!=null){ processWords(br.readLine()); } br.close(); fis.close(); } catch (IOException | NullPointerException ex) { System.out.println(ex.getMessage()); } } private void processWords(String line) { Arrays.stream(line.split(" ")) .map(String::trim) .forEach(word -> { if(mapOfBadNames.containsKey(word)){ mapOfBadNames.put(word, mapOfBadNames.get(word)+1); } else{ mapOfBadNames.put(word, 1); } }); } public void writeResultToFile(String path) throws IOException { try { PrintWriter out = new PrintWriter(path); out.println("word,frequency"); for(Map.Entry<String, Integer> entry : mapOfBadNames.entrySet()){ out.println(entry.getKey() + "," + entry.getValue()); } out.close(); } catch (NullPointerException | IOException ex) { throw new RuntimeException("Error writing to file", ex); } } public boolean isEverythingFine() { return true; } public static void main(String[] args) { badCounter bcn = new badCounter(); try { bcn.doProcessing("bad_input.txt"); bcn.writeResultToFile("result.csv"); bcn.doProcessing("bad_input1.txt"); bcn.writeResultToFile("result1.csv"); } catch (IOException ex) { System.out.println("Something went wrong"); } } }
Marcar como concluído
Anterior
Próximo