Problem with java exercise of reading and writing files

0
  

Write a program that receives a character and a character string as arguments. The program should read the text file whose name has been received as the second argument and display on the screen those lines that contain the character indicated as the first argument.

I'm stuck and I do not know how to raise it in java.

import java.io.*;

class LeeFichero {
    public static void main(String [] arg)
    {
        File archivo = null;
        try {
            archivo = new File("archivo.txt");
            String linea;
            FileReader fr = new FileReader (archivo);
            BufferedReader br = new BufferedReader(fr);
            int i,j,aux=0;
            while((linea=br.readLine())!=null) {
                for(i=0;i<linea.length();i++)
                {
                    if(linea.charAt(i)=='a')
                    {
                        if(i==0) 
                            aux=1;
                        else
                        {
                            if(linea.charAt(i-1)==' ')
                                aux=1;
                        }
                    }
                    if(aux==1)
                    {
                        if(linea.charAt(i)!=' ')
                            System.out.print(linea.charAt(i));
                        else 
                        {
                            aux=0; System.out.println(' ');
                        }
                    }
                }
            }
            fr.close();
        }
        catch(IOException a){
            System.out.println(a);
        }
    }
}

This is my exercise, I have raised it which takes out all the words that begin with a, but what I want to do is to tell me where in which lines is the a how can I modify it

    
asked by Ángel 29.03.2017 в 13:22
source

3 answers

0

To print the line that has the character you need, just put your While in the following way.

while((linea=br.readLine())!=null) {
if(línea.contains(caracter+"")
System.out.println(linea);

} 

The variable character is the character you are looking for in each line.

    
answered by 29.03.2017 в 14:48
0

Complete Solution:

public class AplicacionJava {


public static void main(String[] args) {
    try {
        buscaCaracter('z',"c:\Archivo.txt");
    } catch (IOException ex) {
        //manejar la excepcion
        Logger.getLogger(JavaApplication6.class.getName()).log(Level.SEVERE, null, ex);
    }
}


public static void buscaCaracter(char caracter,String archivo) throws FileNotFoundException, IOException{
        File documento = new File(archivo);
        String linea;
        FileReader fr;

        fr = new FileReader (documento);
        BufferedReader br = new BufferedReader(fr);
        while((linea=br.readLine())!=null) {
            if(linea.contains(caracter+""))
            System.out.println(linea);


        }

}

}

    
answered by 29.03.2017 в 15:27
0

This can help you:

package app;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Paolo Rios Garaundo
 */
public class Executable {
    public static void main(String[] args) {
        try {
            /*
            Contenido de a.txt
                hola como estas?
                este es un mensaje de
                ejemplo, solo para ver
                si se encuentran los caracteres
                especificados.
            */
            final BufferedReader br = new BufferedReader(new FileReader("c:\a.txt"));
            List lineasConTexto = new ArrayList(); // donde el primer indice del arreglo es la line en el fichero y el segundo indice es la posicion del texto en la linea
            int numLinea = 0;
            String ln;

            while((ln = br.readLine()) != null){
                int pos = ln.indexOf("un");
                if (pos > -1) {// si la linea contiene la palabra 'un'
                    lineasConTexto.add(new Integer[] {numLinea, pos});
                }

                numLinea++;
            }

            // se recorre los datos recaudados
            for (Integer[] arr : lineasConTexto){
                System.out.println("Linea: " + arr[0] + " - Posicion en la linea: " + arr[1]);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}
    
answered by 30.03.2017 в 00:01