Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class LeerNumerosPrimos {
- public static Scanner scanner = new Scanner(System.in);
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String contenido;
- String aux = "";
- int numero;
- LeerArchivos documento = new LeerArchivos();
- System.out.println("Ingrese la dirección del archivo: ");
- String direccion = scanner.nextLine();
- System.out.println("Contenido del archivo: ");
- contenido = documento.leerArchivoDeTexto(direccion);
- System.out.println("Números primos dentro del archivo: ");
- for (char c: contenido.toCharArray()) {
- if(c != ' ') {
- aux+=c;
- }else {
- numero = Integer.parseInt(aux);
- if (esPrimo(numero)) {
- System.out.print(numero+" ");
- }
- aux="";
- }
- }
- }
- private static boolean esPrimo(int num) {
- int contador = 0;
- int i = 2;
- while(i<=(Math.sqrt(num))) {
- if (num%i==0)contador++;
- i++;
- }
- if (contador>0) return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement