Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Implement a program that counts the number of single-letter words in a text file.

Your program should input the name of a text file, and print the number of
single-letter words in that file. The file name is a string, the length of which is between 1 and 60.
The file contains between 1 and 1,000 lines, each of which is between 1 and 60 characters long.
The characters in the file include letters, spaces, and line breaks. The file does not include digits
or punctuation marks, and it also does not include blank lines. A single-letter word in that file
may be a letter surrounded by spaces; a letter in the beginning of a line followed by a space; a
letter in the end of a line preceded by a space; or a single-letter line. In other words, it is a letter
that does not have another letter next to it


ex:We r aliens
We talk in a strange language
We r friendly
ANSWER: 3

What I have tried:

ackage test;

import java.io.*;

public class Test {
    public static void main(String [] args) {

        // The name of the file to open.
        String fileName = "temp.txt";

        // This will reference one line at a time
        
        String line = null;

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = 
                new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }   

            // Always close files.
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                  
            
        }
    }
}
Posted
Updated 10-Apr-18 11:06am
Comments
wseng 9-Apr-18 22:07pm    
what is your problem ?
Member 13710451 10-Apr-18 13:38pm    
code to specify single letter on that sentence
Patrice T 9-Apr-18 22:26pm    
And you have a question ?
Member 13710451 10-Apr-18 13:36pm    
the code for the single letter
Richard MacCutchan 10-Apr-18 3:56am    
You forgot to add the code that looks for single letter tokens.

Think about what you need to do to recognise the tokens that you are looking for. Firstly split the line into separate tokens, see String (Java Platform SE 7 )[^] for the methods that will help. Then find out how many characters in each token, again look at the string class. Finally keep a count of the number that only contain a single letter.
 
Share this answer
 
Comments
Member 13710451 10-Apr-18 14:46pm    
// The name of the file to open.
String fileName = "temp.txt";

// This will reference one line at a time

String line = "We r aliens\n" +"We talk in a strange language\n" + "We r friendly";
String delims = "[ ]+";
String[] tokens = phrase.split(delims);
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);

// while((line = bufferedReader.readLine()) != null) {
// System.out.println(line);
for (int i = 0; i < tokens.length; i++)
System.out.println(tokens[i]);
// }

// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}

i'm having an error
Richard MacCutchan 10-Apr-18 14:53pm    

String line = "We r aliens\n" +"We talk in a strange language\n" + "We r friendly";
String delims = "[ ]+";
String[] tokens = phrase.split(delims);

Why are you doing this, when you are supposed to read your input data from a text file? And why are you using those delimiters when the strings are separated by space and newline characters? And why are you using the variable name phrase which does not exist anywhere?

Don't just write random lines of code and hope something will work. Stop and think about the steps you need to take to solve your problem. Write the steps out in your natural language and go through it to see that it makes sense. Only then should you convert those steps into actual lines of code.
Reading one character at time, with a simple state machine will do the trick.
 
Share this answer
 
Comments
Member 13710451 10-Apr-18 18:57pm    
have no idea
CPallini 11-Apr-18 3:11am    
Having no ideas is a very bad starting point for a developer (you could consider a career in project management).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900