Home > Software design >  With a scanner search on ArrayList the content and print the line
With a scanner search on ArrayList the content and print the line

Time:02-03

I have a text file (.txt) and I'm stuck here. I user a BufferReader to read all file and save in a ArrayList then put the ArrayList into a String to remove the , [ ] Now I need to find the word of the scanner ex: (1001) in the ArrayList that the user want, and print the line of this word and the 4 lines after that. After that, edit this 4 lines and save the ArrayList to a file.

Or have something more simple without using ArrayLists? Thank you.

System.out.println("Digite o ID ou 1 para sair: ");
                Scanner sOPFicheiro = new Scanner(System.in);
                opFicheiro = sOPFicheiro.nextInt();
                     if (opFicheiro == 1){
                         System.out.println("A voltar ao menu anterior...");
                         Thread.sleep(1000);
                         editarFicheiro();
                     } else {
                            //Envia para um ArrayList o ficheiro Formandos
                            ArrayList<String> textoFormandos = new ArrayList<String>();
                            BufferedReader ler = new BufferedReader(new FileReader(FichFormandos));
                            String linha;
                        while ((linha = ler.readLine()) != null) {
                            textoFormandos.add(linha   "\n");
                        }
                        ler.close();
                              
                              //Remove , [ ] do ArrayList para enviar para o ficheiro
                              String textoFormandos2 = Arrays.toString(textoFormandos.toArray()).replace("[", "").replace("]", "").replace(",", "");
                         
                     }

File: Txt File

CodePudding user response:

Instead of using an ArrayList use a StringBuilder :

    StringBuilder textoFormandos = new StringBuilder();
    while ...
        textoFormandos.append(linha   "\n");
    ...
    String textoFormandos2 = textoFormandos.toString();

This way you won't need to remove anything. For the rest you need to clear the requirements.

CodePudding user response:

save the ArrayList to a file

Your code lucks a mean to write data into the file.

Also invoking close() without a try/catch is a bad practice because it'll lead to resource leaks in case of exceptions.

For this task you don't need a list, after the match with the given id is found you can write these lines to a file.

To execute this code file "test.txt" must reside under the project folder, file "result.txt" will be created outomatically.

    public static void main(String[] args) {
            try {
                readFile(Path.of("test.txt"), Path.of("result.txt"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    public static void readFile(Path target, Path result) throws InterruptedException {
        Scanner sOPFicheiro = new Scanner(System.in);
        int opFicheiro = sOPFicheiro.nextInt();
        if (opFicheiro == 1) {
            System.out.println("A voltar ao menu anterior...");
            Thread.sleep(1000);
            editarFicheiro();
        } else {

            try (BufferedReader ler = new BufferedReader(new FileReader(target.toFile()));
                 BufferedWriter br = new BufferedWriter(new FileWriter(result.toFile()))) {
                boolean matchIsFound = false;
                String linha;
                while ((linha = ler.readLine()) != null && !matchIsFound) {
                    if (linha.contains(String.valueOf(opFicheiro))) {
                        for (int i = 0; i < 5; i  ) {
                            br.write(linha);
                            if (i != 4) {
                                br.newLine();
                                linha = ler.readLine();
                            }
                        }
                        matchIsFound = true;
                    }
                }
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

user input - 1001

Initial contents of the "test.txt" file:

ID: 999
Name: ________________
Data of birth: _______
NIF: _________________
Telephone: ___________
Fim ID: 999

ID: 1001
Name: Cesar Rodrige da Silva Guimaraes
Data of birth: 16/03/2003
NIF: 1111111111
Telephone: 931111111111
Fim ID: 1001

Contents of the "result.txt" file after executing the code:

ID: 1001
Name: Cesar Rodrige da Silva Guimaraes
Data of birth: 16/03/2003
NIF: 1111111111
Telephone: 931111111111
  •  Tags:  
  • Related