Question: Letter count - Java Write a program that asks the user for the name of an input file, an output file, and a letter. Write
Letter count - Java
Write a program that asks the user for the name of an input file, an output file, and a letter. Write each line of the input file to the output file, followed by the number of times that the letter appears in that line.
Name this class LetterCount
-------------------The following is my code--------------------
package lettercount; import java.io.*; import java.util.*; /** * * @author k.chen17 */ public class LetterCount {
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("What is the file you read from?"); String infileName=input.nextLine(); System.out.println("What is the file to write to?"); String outfileName=input.nextLine(); System.out.println("What is the character you want to count?"); char c = input.next().charAt(0); //create and read the file Scanner inputFile = null; try{ inputFile = new Scanner(new File(infileName)); } catch (FileNotFoundException ex){ System.out.println("Error opening the file "+ infileName + " for reading."); System.exit(0); } //write formatted data to an underlying Writer PrintWriter outputFile = null; try{ outputFile = new PrintWriter(new File(outfileName)); } catch(FileNotFoundException ex){ System.out.println("Error opening the file "+ infileName + " for reading."); System.exit(0); } while(inputFile.hasNextLine()){ String line = inputFile.nextLine(); //line seperator Scanner lineScanner = new Scanner(line); int charCount = 0; if(lineScanner.hasNext()){ for(int i=0; i -----------------------------------End of Code-------------------------------- However, the console keeps showing "Error opening the file for reading." Could someone correct my code? Thanks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
