Input and output data in Java and data handling

0

I have a giant list of exercises and I only got these last two to do but I can not get NO. This I / O stop did not enter my head even though I prayed!

  • Make a program that reads a text file, named "input.txt", consisting of integer values and generates a text file, named "output.txt", containing only the prime numbers of the input file. / p>

  • Make a program that reads the text file generated by the previous program, "output.txt", and check and print on screen the largest number in the file and the number of file numbers that are larger than the average values contained in the file.

  • If someone can kindly explain how I do this, I'm grateful! I did all the other exercises and I would like to get the maximum score by doing all of them.

    This is what I have from code 1 to now:

    import java.util.Scanner;
    import java.io.File;
    import java.io.PrintWriter;
    import java.io.FileNotFoundException;
    
    public class trab2{
        public static void main(String [] args){
            try{
                System.out.println("edite o arquivo entrada.txt");
    
                File entrada = new File("entrada.txt");
                Scanner in = new Scanner(entrada);
                PrintWriter saida = new PrintWriter("saida.txt");
    
                boolean ehPrimo = true;
                int contador = 2;
                while(in.hasNextInt()){
                    int valor = in.nextInt();
                    while(contador < valor){                    
                        if(valor % contador == 0){
                            ehPrimo = false;
                            break;
                        }
    
                        contador++;
    
                    }
                        saida.println(valor);
                }
                saida.close();
                in.close();
            }catch (FileNotFoundException e){
                System.out.println("Arquivo não encontrado. Tente novamente.");
            } catch (Exception e){
                System.out.println("Erro de execução: " + e.getMessage());
            }
        }
    }
    

    Of issue two, what I was able to do is this:

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    public class dois{
    public static void main(String [] args){
        int acumulador = 0;
                try{
            File saida = new File("saida.txt");
            Scanner insaida = new Scanner(saida);
            while(insaida.hasNextInt()){            
            int valor = insaida.nextInt();    
            if(valor > acumulador){acumulador = valor;}
            }
    
            System.out.println("O maior valor no arquivo saida.txt é: " +acumulador);
    
        } catch (FileNotFoundException e){
            System.out.println("Arquivo não encontrado. Tente novamente.");
        } catch (Exception e){
            System.out.println("Erro de execução: " + e.getMessage());
        }
    }
    

    }

    What I can not do is the average of the values entered in the file, and how to know how many numbers are in the file.

        
    asked by anonymous 21.09.2015 / 18:48

    1 answer

    7

    For question 39, you had only minor errors in your code, see the comments in the code itself:

    import java.util.Scanner;
    import java.io.File;
    import java.io.PrintWriter;
    import java.io.FileNotFoundException;
    
    public class Teste{
        public static void main(String [] args){
            try{
                System.out.println("edite o arquivo entrada.txt");
    
                File entrada = new File("entrada.txt");
                Scanner in = new Scanner(entrada);
                PrintWriter saida = new PrintWriter("saida.txt");
    
                while(in.hasNextInt()){
                    //esse valor tem que virar true a cada iteração
                    boolean ehPrimo = true; 
    
                    //ele também deve ser resetado para 2 a cada iteração
                    int contador = 2;
    
                    int valor = in.nextInt();
                    //uma otimização simples é testar o contador até a metade do valor
                    //pois nunca um número dividido por outro que seja maior que a sua
                    //metade dará resto 0
                    while(contador <= valor/2){                    
                        if(valor % contador == 0){
                            ehPrimo = false;
                            break;
                        }
                        contador++;
                    }
                    //tem que ter o if aqui para se certificar de que ele passou no teste
                    if(ehPrimo) {           
                        saida.println(valor);
                    }
                }
                saida.close();
                in.close();
            }catch (FileNotFoundException e){
                System.out.println("Arquivo não encontrado. Tente novamente.");
            } catch (Exception e){
                System.out.println("Erro de execução: " + e.getMessage());
            }
        }
    }
    

    For question 40:

      

    the largest number in the file

    You need to define a variable with the smallest possible value, for example:

    int max = Integer.MIN_VALUE;
    

    And iterate over all items in your text file, whenever the current iteration file is greater than the value contained in max , you assign max to this new value:

      

    and the number of file numbers that are greater than the average values contained in the file.

    First of all you need to take the average. I believe that the best itemize item by item and go adding the amount of items covered and the sum of their value. Then you start the iteration again from the beginning, checking how many values are greater than the average.

        
    21.09.2015 / 19:10