Duvida ao ler todas as linhas do txt da um erro

2 respostas
A
Exception in thread "main" java.lang.NullPointerException 1#932#170,00#191#135,00#0,0#0,0#955,0#191#135,00#8675#1#731#405,0 at java.util.regex.Matcher.getTextLength(Matcher.java:1233) at java.util.regex.Matcher.reset(Matcher.java:308) at java.util.regex.Matcher.(Matcher.java:228) at java.util.regex.Pattern.matcher(Pattern.java:1088) at javaapplication13.JavaApplication13.main(JavaApplication13.java:31) Java Result: 1
esse é o erro tenho um txt e quero ler todas as linhas estou tirando das linhas todas as letras e deixando somente os numeros meu codigo a maixo
public static void main(String[] args) throws FileNotFoundException, IOException {
File doc = new File("C://Users//Usuario//Documents//Report.txt");
     BufferedReader ler = new BufferedReader(new FileReader(doc));
            String txt = null;
            int i=0;
   while(!"".equals(ler.readLine())){
       txt=ler.readLine();
       String somenteNumeros = Pattern.compile("[^,/0-9]").matcher(txt).replaceAll("#");
      
       System.out.println(somenteNumeros);
          }
        
       ler.close();}}

2 Respostas

B

Estou achando que você quer algo como o seguinte:

public static void main (String[] args) {
	BufferedReader ler = null;
	File doc = new File ("c:\\users\\usuario\\documents\\report.txt");
	try {
		ler = new BufferedReader (new FileReader (doc));
		Pattern excetoNumeros = Pattern.compile ("[^,/0-9]");
		String linha;
		while ((linha = ler.readLine()) != null) {
			String somenteNumeros = excetoNumeros.matcher(linha).replaceAll ("#");
			System.out.println (somenteNumeros);
		}
		ler = new BufferedReader (new FileReader (doc));
	} catch (IOException ex) {
		ex.printStackTrace();
	} finally {
		if (ler != null) try { ler.close(); } catch (IOException ex) { ex.printStackTrace(); }
	}
}
A

vlw resolvido era apenas o while com problema

<blockquote>

while ((linha = ler.readLine()) != null) {

String somenteNumeros = Pattern.compile("[^,/0-9]").matcher(linha).replaceAll("#");

System.out.println (somenteNumeros);

}
ler.close();</blockquote>

resolvido

Criado 20 de dezembro de 2011
Ultima resposta 20 de dez. de 2011
Respostas 2
Participantes 2