Hello,
I'm trying to build a program that counts the number of paragraphs from a text file.I tried to use a scanner but when i run the program it returns me 0.
I would appreciate any help...
Here is my code:
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import javax.swing.JFileChooser; public class ParaCount { public static void main(String[] args) { JFileChooser chooseFile = new JFileChooser(); if (chooseFile.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){ File selectedFile = chooseFile.getSelectedFile(); Scanner sc=null; try { FileInputStream file = new FileInputStream(selectedFile); DataInputStream dis = new DataInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(dis)); sc = new Scanner(selectedFile); String str=""; int paraCount = 0; while (sc.hasNextLine()){ str = sc.nextLine(); if (str.contains("\n") ){ paraCount++; } } System.out.println("Number of paragrahs:"+paraCount); } catch(IOException e1){ System.out.println(e1); } } } }