I have been instructed to write a program that will read a text file, count the
ID: 3638228 • Letter: I
Question
I have been instructed to write a program that will read a text file, count the number of times a letter appears in the text and return a sum in a window pane. The code that I have written has no errors, but will not return the number of times the specified letter appears. Can you please direct me to the correct path, I am not looking for the answer but direction.Thanks
Here is my code right now:
/**
*
* @author BelindaFrederick
* @ version 1.0 2012/2/3
*/
import java.util.Scanner; // Needed for Scanner class
import java.io.*; // Needed for File class
import javax.swing.JOptionPane; // Needed for input box.
/**
* This program is designed to count and display the number of
* a specific character, replace the character, and save as a new
* file.
*/
public class FileChanger
{
public static void main(String[] args) throws IOException
{
int count = 0;
int i;
char character;
String originalFile; // This is the file to be read.
String newFile; // This is the new file to be written to.
// Get the name of the original file.
originalFile = JOptionPane.showInputDialog( "What is the filename " +
"to be processed");
// Open the original file.
FileReader freader = new FileReader(originalFile);
BufferedReader inputFile = new BufferedReader(freader);
// Ask the user for the letter to be counted.
String letter;
letter = JOptionPane.showInputDialog( "What is the letter to be counted? ");
for( i = 0; i < originalFile.length(); i++)
{
letter = originalFile.substring(1,1);
if(letter.equals(inputFile))
{
count++;
}
JOptionPane.showMessageDialog(null, "The letter " + letter + " is found " +
count + " times." );
}
inputFile.close();
}
}
Explanation / Answer
import java.util.Scanner; public class Letter { public static void main(String[] args) { //needed for scanner class Scanner kb = new Scanner(System.in); int charCount = 0; // get users string System.out.println("Please enter a string: "); String userString = kb.nextLine(); // get users character System.out.println("Please enter a character: "); String userChar = kb.nextLine(); // tell the user what the program does System.out.println("****This program will return how many times" + " the character you entered showed up in" + " the string you entered.****"); for(int i= 0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.