Need some java help.......again lol

Status
Not open for further replies.

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 :D
 
Now I'm no expert, but here's what I would do (in some sort of hopefully understandable pseudocode, you can have the fun of coding it fully).
Definitely set up different methods, at the least for expandability. You can very much just do the printouts by calling the method, but it's tough to set it up to give you an OPTION of which you want if you leave it without a method.

Do note that the current setup has the while loop setup to print the number of characters in each line, and theoretically later the number of words in a line, rather than the file.

Code:
Method: countCharacters (returns int numChar)
[LIST=1]
[*]Initiate variable for number of characters (numChar = 0).
[*]WHILE file has next line[LIST=1]
[*]nextLine = the next line
[*]lineLength = nextLine length
[*]numChar = numChar + lineLength
[*]Continue to next line
[/LIST]
[*]return numChar
[/LIST]

Code:
Method: countWords (returns int numWords)
[LIST=1]
[*]Initiate variable for number of words (numWords = 0).
[*]WHILE file has next line[LIST=1]
[*]nextLine = the next line
[*]lineLength = nextLine length
[*]FOR each character in nextLine[LIST=1]
[*]IF character is a space[LIST=1]
[*]numWords = numWords + 1
[/LIST]
[*]Continue to next line
[/LIST]
[/LIST]
[*]return numWords
[/LIST]

Code:
Method: countLines (returns int numLines)
[LIST=1]
[*]Initiate variable for number of lines (numLines = 0).
[*]WHILE file has next line[LIST=1]
[*]numLines = numLines + 1
[*]Continue to next line
[/LIST]
[*]return numLines
[/LIST]

You might see how easy it would be to combine these all into one method to return all three values in one, but that's really specialized and in practice (on more advanced programs) not a good idea. Feel free to do what you want, though.

Any questions, feel free to ask. Hope this was understandable and helpful!
 
Status
Not open for further replies.
Back
Top Bottom