Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Filelettercounter- 'character not found'

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
             String filename;
             String str;
             char character;
             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);
     
             while(inputFile.hasNext())
             {
                 String line=inputFile.nextLine();
                 int len=line.length();
                 for(int i=0;i<len;i++)
                     if (line.charAt(i) == character)
                         count++;
             }
             if(count <= 0)
             System.out.println("Character not found in the file");
             else
                 System.out.println("Number of times character" +count+" in file");
             inputFile.close();
         }
       }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Filelettercounter- 'character not found'

    System.out.println("Character not found in the file")
    Is this the statement that prints the message you see?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. class not found
    By aki in forum Java Applets
    Replies: 1
    Last Post: May 23rd, 2012, 06:50 AM
  2. Return Value Cannot Be Found
    By misscoollike in forum Object Oriented Programming
    Replies: 3
    Last Post: March 12th, 2012, 08:58 PM
  3. application id not found
    By pradeepsetty in forum Java IDEs
    Replies: 3
    Last Post: June 2nd, 2010, 02:37 AM
  4. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM