Filelettercounter- 'character not found'
No matter what character I use, I keep getting 'character not found' when I try to use the code to search for it in a txt file.
import java.util.Scanner;//needed for Scanner Class
import java.io.*;//needed for input and output
public class FileLetterCounter
public static void main(String args)
throws IOException //begin main function
int count =0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a file name ");
filename = keyboard.nextLine();
System.out.println("Enter a character ");
str = keyboard.nextLine();
character = str.charAt(0);
File file=new File(filename);
Scanner inputFile=new Scanner(file);
if (line.charAt(i) == character)
if(count <= 0)
System.out.println("Character not found in the file");
System.out.println("Number of times character" +count+" in file");
Re: Filelettercounter- 'character not found'
Is this the statement that prints the message you see?
System.out.println("Character not found in the file")
Try debugging the code by adding a println statement that prints out every line as it is read from the file so you can see what the computer sees. Also print the the value of character.