wafflehammer
Fully Optimized
- Messages
- 2,502
- Location
- Logan, WV
Code:
import javax.swing.*;
import java.io.*;
import java.util.*;
public class FileIO
{
public static void main(String[] args)
{
FileReader myReader;
JFileChooser myChooser = new JFileChooser();
if(myChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
File myFile = myChooser.getSelectedFile();
try
{
myReader = new FileReader(myFile);
Scanner myScanner = new Scanner (myReader);
while (myScanner.hasNextLine())
{
/** Prints the number of characters in a
* text file
*/
String textLine = myScanner.nextLine();
System.out.print(textLine.length());
System.out.println();
/** Prints the number of words in a
* text file
*/
/** Prints the numbers of lines in a
* text file
*/
}
myReader.close();
}
catch (FileNotFoundException myEx)
{
System.out.println(myEx.getMessage());
}
catch (IOException myEx)
{
System.out.println(myEx.getMessage());
}
}
}
}
Is my code so far. This program is suppose to print the number of characters, words, and lines in a text file. So far I only have it able to print the number of character's in the file. By messing around with it a bit I had to able to print the number of characters per word..but not the amount of words in the file.
Any tips that can put me on the right path are appreciated